This document is archived and information here might be outdated.  Recommended version.


How to enumerate through a printer's available page sizes (ArcObjects .NET 10.5 SDK)

How to enumerate through a printer's available page sizes


Summary
The form sizes (paper sizes) available from a given IPrinter object can be enumerated to find a size that matches a desired form size. This topic illustrates that task and shows how to programmatically select and deselect the Use printer paper settings option for a given map document.

Enumerating a printer's available page sizes

To enumerate a printer's available page sizes, follow these steps:
  1. Initialize a new paper and printer variable for the printer whose forms you're enumerating. See the following code example:
[C#]
/* Assign the new printer to the docPaper variable, */
docPaper.PrinterName=sNewPrinterName;
/* then assign docPaper to the .paper property of docPrinter. */
docPrinter.Paper=docPaper;
[VB.NET]
'Assign the new printer to the docPaper variable,
docPaper.PrinterName=sNewPrinterName
' then assign docPaper to the .paper property of docPrinter.
docPrinter.Paper=docPaper
  1. Use the IEnumNamedID object to enumerate the form names available in the printer.

    In the following code example, the form names are compared against the MapDocument's current form, the form is replaced, and the Use printer paper settings option is selected:
[C#]
IEnumNamedID eFormEnum; //An enumerator to enumerate the form names.


/* Loop through the forms available to the printer. */
eFormEnum=docPaper.Forms;
eFormEnum.Reset();
int iCounter=0;

/*Create a new paper class and query paper sizes for each of the printer's
 * form IDs. Compare this to the page size for m_MapDoc. If you find a match,
 * set the printer and form ID for this printer to the correct size, then recheck
 * the Use printer paper settings check box. Otherwise, set the map page
 * size and uncheck Use printer paper settings.
 */

/* Get the next form in the list. */
iCounter=eFormEnum.Next(out sFormName);

while (sFormName != null)
{
    /* Reinitialize docNewPaper.*/
    docNewPaper=new PaperClass();
    docNewPaper.PrinterName=sNewPrinterName;
    docNewPaper.Orientation=m_MapDoc.PageLayout.Page.Orientation;
    /* Set the form ID of docNewPaper so you can get the size. */
    docNewPaper.FormID=(short)iCounter;
    /* Get the new printer page size so you can see if an acceptable page size is available. */
    docNewPaper.QueryPaperSize(out dNewPageWidth, out dNewPageHeight);
    if ((dPageHeight == (Math.Round(dNewPageHeight))) && (dPageWidth == (Math.Round
        (dNewPageWidth))))
    {
        /*
         * If you find a match for the map page size, set this printer page size and check the 
         * Use printer paper settings check box by changing the form ID to "esripagesizeSameAsPrinter".
         */
        System.Console.WriteLine("Found match on printer " + sNewPrinterName + 
            ": Form size " + sFormName);
        bFormFound=true;
        /* First, assign the newly created paper to docPrinter.Paper. */
        m_MapDoc.Printer.Paper=docNewPaper;
        /* Then place the custom size into the .PageLayout.Page. */
        m_MapDoc.PageLayout.Page.PutCustomSize(dPageWidth, dPageHeight);
        /* Now that the pagelayout.page is set, choose the page.FormID, */
        m_MapDoc.PageLayout.Page.FormID=esriPageFormID.esriPageFormSameAsPrinter;
        /* and signal to the active view that the printer has changed to effect the changes. */
        m_MapDoc.ActiveView.PrinterChanged(m_MapDoc.Printer);
    }
    iCounter=eFormEnum.Next(out sFormName);
}
[VB.NET]
Dim eFormEnum As IEnumNamedID 'An enumerator to enumerate the form names.

' Loop through the forms available to the printer to find a matching form size.
eFormEnum=docPaper.Forms
eFormEnum.Reset()
Dim iCounter As Integer=0
iCounter=eFormEnum.Next(sFormName)
While (sFormName IsNot Nothing)
    'Create a temporary paper and assign the printer, orientation, and form ID to it to get
    'its page size and compare it to the desired page size.
    docNewPaper=New Paper()
    docNewPaper.PrinterName=sNewPrinterName
    docNewPaper.Orientation=m_MapDoc.PageLayout.Page.Orientation
    'Set the form ID to the current counter.
    docNewPaper.FormID=iCounter
    'Get the paper size of this form ID.
    docNewPaper.QueryPaperSize(dNewPageWidth, dNewPageHeight)
    
    If ((dPageHeight=Math.Round(dNewPageHeight)) And (dPageWidth=Math.Round(dNewPageWidth))) Then
        ' If you find a match for the map page size, set this printer page size and check the
        ' Use printer paper settings check box by changing the form ID to "esripagesizeSameAsPrinter".
        System.Console.WriteLine("Found match on printer " + sNewPrinterName + ": Form size " + sFormName)
        bFormFound=True
        ' First, assign the newly created paper to docPrinter.Paper. */
        m_MapDoc.Printer.Paper=docNewPaper
        ' Then place the custom size into the .PageLayout.Page. */
        m_MapDoc.PageLayout.Page.PutCustomSize(dPageWidth, dPageHeight)
        ' Now that the pagelayout.page is set, choose the page.FormID,*/
        m_MapDoc.PageLayout.Page.FormID=esriPageFormID.esriPageFormSameAsPrinter
        ' and signal to the active view that the printer has changed to effect the changes. */
        m_MapDoc.ActiveView.PrinterChanged(m_MapDoc.Printer)
    End If
    iCounter=eFormEnum.Next(sFormName)
End While






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):
Additional Requirements
  • docPaper—An IPaper variable
  • docNewPaper—An IPaper variable
  • sNewPrinterName—The new printer's name in a string variable
  • sFormName—A string variable
  • m_MapDoc—A global MapDocument variable that is initialized to the current MapDocument

Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine