How to export the layout to tiles


This sample demonstrates use of the VisibileBounds parameter of the ActiveView::Output method. When executed, the code will export the full extent of the Page Layout to a series of JPEG files measuring 640 x 480 apiece. This code can be modified to work with data view, or to print to tiles instead of exporting.

How to use

  1. Create a folder in C:\ named "ExportTiles", paste this code into the Visual Basic editor, and select Run Sub from the Run menu.
[VBA]
Public Sub ExportLayoutToTiles()
    Dim pMxDoc As IMxDocument
    Dim pActiveView As IActiveView
    Dim pExport As IExport
    Dim pPixelBoundsEnv As IEnvelope
    Dim exportRECT As tagRECT
    Dim hDC As Long
    Dim dPageHeight As Double
    Dim dPageWidth As Double
    Dim iTileCount As Integer
    Dim iTileYNum As Integer
    Dim iTileXNum As Integer
    Dim dTileYIncrement As Double
    Dim dTileXIncrement As Double
    Dim dLastYPos As Double
    Dim dLastXPos As Double
    Dim iTileWidthPixels As Integer
    Dim iTileHeightPixels As Integer
    Dim pExportTileEnv As IEnvelope
    
    Set pMxDoc = Application.Document
    Set pActiveView = pMxDoc.PageLayout
    
    'Assign values to define the hight and width of one tile.
    iTileWidthPixels = 640
    iTileHeightPixels = 480
    
    'Set up our exporter object.  Note that we are assigning windows screen resolution to the
    ' resolution property, and define the exportRECT to use the tile height and width we assigned
    ' above.
    Set pExport = New ExportJPEG
    pExport.Resolution = 96
    With exportRECT
        .Left = 0
        .Top = 0
        .Right = iTileWidthPixels
        .bottom = iTileHeightPixels
    End With
    
    'Set up the PixelBounds envelope.  Remember, the pixel bounds deals with the bounds of the
    ' output file.  It has nothing to do with the coordinates of the Page Layout.
    Set pPixelBoundsEnv = New Envelope
    pPixelBoundsEnv.PutCoords exportRECT.Left, exportRECT.Top, exportRECT.Right, exportRECT.bottom
    pExport.PixelBounds = pPixelBoundsEnv
    
    'Create the export tile envelope.  This envelope defines the extent to be exported.  The envelope
    ' values are in Page Layout units, usually inches or centimeters.
    Set pExportTileEnv = New Envelope
    
    'Use the QuerySize method to populate the PageWidth and PageHeight variables.
    pMxDoc.PageLayout.Page.QuerySize dPageWidth, dPageHeight
    'Use PageWidth and PageHeight to calculate x and y increment values.  We will use these
    ' increments to position the export tile envlope on the page.  Note that the caculation
    ' is utilising coordinates from two different coordinate spaces:  dPageHeight is in
    ' page units (i.e. inches) and ExportFrame and iTileHeightPixels are in device units
    ' (i.e. pixels).  The ExportFrame.bottom represents the height in pixels of the entire
    ' page exported at screen resolution.  Our calculation asks how many times will a
    ' 640 x 480 image vertically tile across our Page Layout?  dPageHeight is then divided
    ' by this number to tell us how many vertical inches to increment after each export.
    dTileYIncrement = dPageHeight / (pActiveView.ExportFrame.bottom / iTileHeightPixels)
    dTileXIncrement = dPageWidth / (pActiveView.ExportFrame.Right / iTileWidthPixels)
    
    'Perform the export operation inside two nested for loops.  The inner loop increments
    ' the position of the export tile along the y-axis, the outer loop traverses along the
    ' x.
    For iTileXNum = 0 To Fix(pActiveView.ExportFrame.Right / iTileWidthPixels)
        For iTileYNum = 0 To Fix(pActiveView.ExportFrame.bottom / iTileHeightPixels)
            'Name the export file, dynamically creating a file name that is tagged with the x and y
            ' iterations.  The format() function will force leading zeroes when the increment values
            ' are cast to strings.  Be sure that the ExportTiles folder exists at the path defined
            ' here.
            pExport.ExportFileName = "C:\ExportTiles\ExportTile_" & Format(iTileXNum, "000") & "_" & Format(iTileYNum, "000") & ".jpg"
            
            'Assign values to the export tile envelope, adding the increment values for x and y to
            ' move the envelope to its new postion on the page.  Remember, all values here are
            ' in page units.
            pExportTileEnv.PutCoords dLastXPos, dLastYPos, dLastXPos + dTileXIncrement, dLastYPos + dTileYIncrement
            
            'Do the export operation
            hDC = pExport.StartExporting
            'When we call the Output method, we assign pExportTileEnv to the VisibleBounds
            ' parameter.  This is causes the draw engine to render the extent defined
            ' by our envelope, instead of rendering the entire page.
            pActiveView.Output hDC, pExport.Resolution, exportRECT, pExportTileEnv, Nothing
            pExport.FinishExporting
            pExport.Cleanup
            
            ' increment our various counters and the value of our page position variables.
            iTileCount = iTileCount + 1
            dLastYPos = dLastYPos + dTileYIncrement
        Next iTileYNum
        dLastYPos = 0
        dLastXPos = dLastXPos + dTileXIncrement
    Next iTileXNum
End Sub