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


How to check for a saved printer and clear the use printer paper settings option (ArcObjects .NET 10.4 SDK)

How to check for a saved printer and clear the use printer paper settings option


Summary
If a map document is saved with the Use Printer Paper Settings check box selected, printer information is saved with the map document's page layout. The sample code in this topic shows how to detect whether a printer is saved with a given map document and how to disable the use printer paper settings, if the saved printer is invalid.

Checking for a saved printer and clearing the use printer paper settings

Any check for the map document's printer name must be contained in an error handling statement, because if the printer is invalid, the check generates an error. Do the following steps to check for printer settings:
  1. Retrieve the name of the printer from the map document. If it does not exist, you know that the printer saved with the map document is no longer valid. This can occur if a map document is sent to another user, or if a printer is uninstalled from the system. See the following code example:
[C#]
/* Try and get the name of the printer. If it doesn't exist, set bNeedPrinter to
    true.
 * This has to be in a try/catch block because the reference to m_MapDoc.Printer.Paper.PrinterName
 * will throw an exception if the map was saved with use printer paper settings options and the defined 
 * printer is not on the system.
 */
try
{
    /* Print the current printer's name. */
    System.Console.WriteLine("Map Document's current printer is " +
        m_MapDoc.Printer.Paper.PrinterName);
    bNeedPrinter=false;
}

catch 
{
    /* If an exception is thrown, the printer is invalid. */
    System.Console.WriteLine("Map Document's current printer is invalid.");
    bNeedPrinter=true;
}
[VB.NET]
' Try and get the name of the printer. If it doesn't exist, then set bNeedPrinter to true.
' This has to be in a try/catch block because the reference to m_MapDoc.Printer.Paper.PrinterName
' will throw an exception if the map was saved with use printer paper settings option and the defined
' printer is not on the system.
Try
' Print the current printer's name.
System.Console.WriteLine("Map Document's current printer is " + m_MapDoc.Printer.Paper.PrinterName)
bNeedPrinter=False
Catch
' If an exception is thrown, the printer is invalid.
System.Console.WriteLine("Map Document's current printer is invalid.")
bNeedPrinter=True
End Try
  1. If the printer is invalid, set the map's page size explicitly. If it is determined that the printer name is invalid, either the map was saved with the use printer paper settings cleared or the printer the map was saved with is no longer available. To turn off the use printer paper settings, change the PageLayout.Page FormID to the correct size. This can be one of the esriPageFormID constants or esriPageFormCUSTOM. If esriPageFormCUSTOM is used, the page's PutCustomSize method should be used to place the appropriate dimensions into the page layout. See the following code example:
[C#]
if (bNeedPrinter)
{
    /* If the document's printer is invalid, assign the page size explicitly.
     * This is necessary because the document's printer cannot provide a page size. 
     */
    if (docPage.StretchGraphicsWithPage == true)
    {
        /* If the Scale Graphics to Page Size check box is selected, clear it. */
        docPage.StretchGraphicsWithPage=false;
    }
    /* This line clears the Use Printer Paper Settings check box. */
    docPage.FormID=esriPageFormID.esriPageFormCUSTOM;
    /* Place the correct custom size into the page. */
    docPage.PutCustomSize(dPageWidth, dPageHeight);
}
[VB.NET]
If (bNeedPrinter) Then
    ' If the document's printer is invalid, assign the page size explicitly.
    ' This is necessary because the document's printer cannot provide a page size.
    If (docPage.StretchGraphicsWithPage=True) Then
        'If the Scale Graphics to Page Size check box is selected, clear it.
        docPage.StretchGraphicsWithPage=False
    End If
    ' This line clears the Use Printer Paper Settings check box.
    docPage.FormID=esriPageFormID.esriPageFormCUSTOM
    ' Place the correct custom size into the page. */
    docPage.PutCustomSize(dPageWidth, dPageHeight)
End If






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
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