How to select a printer paper tray


Summary The Output assembly's Paper object implements a TrayID property that allows developers to define the printer paper source to be used by the printer. The Paper TrayID is a wrapper around the print driver software, and the Windows printing application programming interface (API) does not allow assignment of arbitrary tray values to this property. Thus, developers are responsible for enumerating supported trays from the printer driver to obtain valid values for use in the TrayID property. Each printer driver can implement a unique list of paper trays; therefore, it is critical to interrogate supported trays for the current printer before attempting to change the tray ID. This topic shows how to enumerate paper trays and assign a valid tray to the paper tray ID.

Enumerating through a printer's available paper trays

The following code example shows the necessary steps to search for and assign a specific paper source for use by the print device at output time:
[C#]
IPaper paper = new PaperClass();
paper.PrinterName = "\\\\typo\\ptolemy";

// Create a hashtable to store values returned by the tray's enumerator. 
Hashtable trays = new Hashtable();
IEnumNamedID trayEnum = paper.Trays;
short targetTrayID = 0;
int trayIdTemp;
string trayNameTemp;

//trayEnum returns a TrayID number as the method's return value and assigns a
// tray name string to a string variable passed in by reference (trayNameTemp).
trayEnum.Reset();
trayIdTemp = trayEnum.Next(out trayNameTemp);

//Continue to loop through the tray enumerator and store all returned values in the tray's hashtable, storing the
// tray IDs as hashtable keys, and the tray names as hashtable values.
// Once complete, the hashtable contains valid tray IDs and names for all trays supported by the printer driver.
while (trayIdTemp > 0)
{
    trays.Add(trayIdTemp, trayNameTemp);
    trayIdTemp = trayEnum.Next(out trayNameTemp);
}

//Store the current tray ID in case the find routine does not locate the tray name expected.
targetTrayID = paper.TrayID;

//Loop through the hashtable, searching for a specific tray name. Once found, assign the tray ID
// to a variable.
foreach (DictionaryEntry de in trays)
{
    Console.WriteLine("{0}: {1}", de.Key, de.Value);
    if (de.Value.ToString().Contains("Manual"))
    {
        targetTrayID = (short)(int)de.Key;
        break;
    }
}

//Assign the matched tray ID to the pPaper.TrayID property. Because the ID was returned
// by the tray's enumerator, you can be confident it is a valid TrayID for the current print
// driver.
paper.TrayID = targetTrayID;
[VB.NET]
Dim paper As IPaper = New Paper
paper.PrinterName = "\\typo\ptolemy"

' Create a hashtable to store values returned by the tray's enumerator.
Dim trays As New Hashtable
Dim trayEnum As IEnumNamedID
trayEnum = paper.Trays
Dim targetTrayID As Short = 0
Dim trayIdTemp As Integer
Dim trayNameTemp As String = ""

'trayEnum returns a TrayID number as the method's return value and assigns a
' tray name string to a string variable passed in by reference (trayNameTemp).
trayEnum.Reset()
trayIdTemp = trayEnum.Next(trayNameTemp)

'Continue to loop through the tray enumerator and store all returned values in the tray hashtable, storing the
' tray IDs as hashtable keys and the tray names as hashtable values.
' Once complete, the hashtable contains valid tray IDs and names for all trays supported by the printer driver.
Do While trayIdTemp > 0
    trays.Add(trayIdTemp, trayNameTemp)
    trayIdTemp = trayEnum.Next(trayNameTemp)
Loop

'Store the current tray ID in case the find routine does not locate the tray name expected.
targetTrayID = paper.TrayID

'Loop through the hashtable searching for a specific tray name. Once found, assign the tray ID
' to a variable.
Dim de As DictionaryEntry
For Each de In trays
    Console.WriteLine("{0}: {1}", de.Key, de.Value)
    If de.Value.ToString().Contains("Manual") Then
        targetTrayID = de.Key
        Exit For
    End If
Next

'Assign the matched tray ID to the pPaper.TrayID property. Because the ID was returned
' by the tray's enumerator, you can be confident it is a valid TrayID for the current print
' driver.
paper.TrayID = targetTrayID






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime