How to print the active view with ArcPress


This sample demonstrates how to print a map programmatically via ArcPress for ArcGIS. This sample will only work on installations of ArcGIS Desktop with the ArcPress for ArcGIS extension installed.

How to use

  1. Paste this code into the Visual Basic editor and select Run Sub from the Run menu.
[VBA]
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Declare Function CreateIC Lib "gdi32" Alias "CreateICA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, _
    ByVal lpOutput As String, ByVal lpInitData As Long) As Long


Public Sub PrintActiveViewArcPress()
    
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pMxApp As IMxApplication
    Set pMxApp = ThisDocument.Parent
    Dim iNumPages As Integer, lCurrentPageNum As Long
    Dim sMxdPath As String, sMxdName As String
    Dim pCancel As ITrackCancel
    Set pCancel = New CancelTracker
    Dim pPrinterBounds As IEnvelope
    Set pPrinterBounds = New Envelope
    Dim deviceRECT As tagRECT
    
    
    ' Create a new ArcPressPrinter printer object.
    
    Dim pPrinter As IPrinter
    Set pPrinter = New ArcPressPrinter
    Dim pArcPressPrinter As IArcPressPrinter
    Set pArcPressPrinter = pPrinter
    
    
    ' Set up the printer properties using the Paper object.  When we set pPaper
    '  to point to the Application's Paper object, the property values will
    '  reflect the Application's current settings.  We can force alternative
    '  values here.
    Set pPrinter.Paper = pMxApp.Paper
    pPrinter.Paper.PrinterName = "\\tuna\poodle"
    
    ' Orientation 1=Portrait  2=Landscape
    pPrinter.Paper.Orientation = 2
    
    ' Get the MXD name and assign it to the SpoolFileName property. This name
    '  will show up in the print status window, and is the name that will be
    '  recorded by any print spool logging software.
    sMxdPath = Application.Templates.Item(Application.Templates.Count - 1)
    sMxdName = Split(sMxdPath, "\")(UBound(Split(sMxdPath, "\")))
    pPrinter.SpoolFileName = sMxdName
    
    ' Select the ArcPress driver for the ArcPress engine to use.
    '  Because IArcPressPrinter::SelectedDriverID takes an IUID instead of an
    '  interface pointer, we must set up a UID object and assign it to the ArcPressPrinter.
    '  The Printer object will create its own driver object based on the UID you
    '  assign.  To set up the UID, assign the ProgID of the driver class.
    '  ArcPress drivers live in the esriPrintSharedOutput library.
    '
    ' You can select one of six standard drivers:
    '    RTLTrueColorRasterPrintDriver
    '    RTLCMYKHalftoneRasterPrintDriver
    '    RTLMonochromeHalftoneRasterPrintDriver
    '    HPPCL3GUIRasterPrintDriver
    '    PCLCMYKHalftoneRasterPrintDriver
    '    PCLMonochromeHalftoneRasterPrintDriver
    Dim pArcPressDriverUid As UID
    Set pArcPressDriverUid = New UID
    pArcPressDriverUid.Value = "esriPrintSharedOutput.HPPCL3GUIRasterPrintDriver"
    pArcPressPrinter.SelectedDriverId = pArcPressDriverUid
    Set pArcPressDriverUid = Nothing
    pPrinter.Resolution = 300
    '  The lines above demonstrate manual control of driver choice and resolution.
    '  To use the default values for these settings, simply leave the Resolution
    '  property at its original value, and tell the ArcPressPrinter object to
    '  auto-select the driver with the following call:
    '     pArcPressPrinter.AutoSelectDriverByName pPrinter.Name
    
    
    
    ' Set up an informational hDC, so we can use the Win32 GetDeviceCaps fuction to
    '  get Printer's Physical Printable Area x and y margins
    Dim hInfoDC As Long
    hInfoDC = CreateIC(vbNullString, pPrinter.Paper.PrinterName, vbNullString, 0)
    
    ' Find out how many printer pages the output will cover.  If iNumPages greater
    '  than 1 then we're going to tile the pagelayout onto multiple pages.
    '  This will only be the case if pPage.PageToPrinterMapping = esriPageMappingTile
    If TypeOf pMxDoc.ActiveView Is IPageLayout Then
        pMxDoc.PageLayout.Page.PrinterPageCount pPrinter, 0, iNumPages
    Else
        iNumPages = 1
    End If
    
    For lCurrentPageNum = 1 To iNumPages
        pMxDoc.PageLayout.Page.GetDeviceBounds pPrinter, lCurrentPageNum, 0, pPrinter.Resolution, pPrinterBounds
        
        'Transfer PrinterBounds envelope, offsetting by PHYSICALOFFSETX
        ' the Win32 constant for PHYSICALOFFSETX is 112
        ' the Win32 constant for PHYSICALOFFSETY is 113
        With deviceRECT
            .bottom = Round(pPrinterBounds.YMax - GetDeviceCaps(hInfoDC, 113))
            .Left = Round(pPrinterBounds.XMin - GetDeviceCaps(hInfoDC, 112))
            .Right = Round(pPrinterBounds.XMax - GetDeviceCaps(hInfoDC, 112))
            .Top = Round(pPrinterBounds.YMin - GetDeviceCaps(hInfoDC, 113))
        End With
        
        'Transfer offsetted PrinterBounds envelope back to the deviceRECT
        pPrinterBounds.PutCoords 0, 0, deviceRECT.Right - deviceRECT.Left, deviceRECT.bottom - deviceRECT.Top
        
        Dim pVisibleBounds As IEnvelope
        If TypeOf pMxDoc.ActiveView Is IPageLayout Then
            Set pVisibleBounds = New Envelope
            pMxDoc.PageLayout.Page.GetPageBounds pPrinter, lCurrentPageNum, 0, pVisibleBounds
        End If
        
        pMxDoc.ActiveView.Output pPrinter.StartPrinting(pPrinterBounds, 0), pPrinter.Resolution, deviceRECT, pVisibleBounds, pCancel
        pPrinter.FinishPrinting
    Next
    
    Set pPrinter = Nothing
    Set pArcPressPrinter = Nothing
    Set pPrinterBounds = Nothing
    Set pVisibleBounds = Nothing
    Set pCancel = Nothing
    
End Sub