How to enumerate through a printer's available page sizes


SummaryThe form sizes (paper sizes) available from a given IPrinter object can be enumerated to find a size that matches a desired form size. This document illustrates that task and demonstrates how to uncheck and check the Use printer paper settings option for a given map document programmatically.

Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime

Additional Requirements
  • The code in this task requires that your project include references to the esriSystem, Output, and Carto assemblies.

Enumerating through a printer's available page sizes

The following code example shows the necessary steps to enumerate through a printer's available page sizes:
 
 

[C#]
IEnumNamedID eFormEnum; //An enumerator to enumerate the form names.

/* Assign the new printer to the docPaper variable, */
docPaper.PrinterName = sNewPrinterName;
/* then assign docPaper to the .paper property of docPrinter */
docPrinter.Paper = docPaper;

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

/*Create a new paperclass and query paper sizes for each of the printer's
* form IDs. Compare this to the pagesize 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 found. */
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.

'Assign the new printer to the docPaper variable,
docPaper.PrinterName = sNewPrinterName
' then assign docPaper to the .paper property of docPrinter
docPrinter.Paper = docPaper
' and 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 affect the changes. */
m_MapDoc.ActiveView.PrinterChanged(m_MapDoc.Printer)
End If
iCounter = eFormEnum.Next(sFormName)
End While