AnsweredAssumed Answered

Export Page Layout to PDF using ESRI add-in with

Question asked by tselvidge on Mar 19, 2015
Latest reply on Mar 27, 2015 by tselvidge

Note: This message is cross-posted on Stack Exchange (Export Page Layout to PDF using ESRI add-in with - Geographic Information Systems Stack Exchange )


I have been trying to convert a vba script to but I have hit a roadblock and am hoping someone can help me figure out the problem. I want to export the Page Layout of my map to a PDF using an add-in button. The roadblock occurs where I define my PDFCreate class. I keep receiving the error message: "Unable to cast object of type 'CreatePDF_2.PDFCreate' to type ESRI.ArcGIS.Output.IExport". I suspect the error is in my definition and relationship of the document, active view and page layout. I have looked at examples and tried different definitions and procedures but I have been unable to figure out how to fix the problem.


I am using Visual Studio 2010 Professional and ArcMap 10.2.2


Any assistance will be appreciated. Thanks.





Imports ESRI.ArcGIS.Carto

Imports ESRI.ArcGIS.esriSystem

Imports ESRI.ArcGIS.Geometry

Imports ESRI.ArcGIS.Output

Imports System.Windows.Forms

Imports ESRI.ArcGIS.ArcMapUI



Public Class PDFCreate

    Inherits ESRI.ArcGIS.Desktop.AddIns.Button



    Public Sub New()

    End Sub



    Protected Overrides Sub OnClick()


    End Sub



    Protected Overrides Sub OnUpdate()

        Enabled = My.ArcMap.Application IsNot Nothing

    End Sub



    Public Sub PDFCreatemodule()


        '  TODO: Sample code showing how to access button host


        Dim pMxDoc As IMxDocument

        Dim pActiveView As IActiveView

        Dim pPageLayout As IPageLayout

        Dim pExport As ESRI.ArcGIS.Output.IExport

        Dim pExportpdf As ESRI.ArcGIS.Output.IExportPDF

        Dim pPixelEnv As IEnvelope

        Dim lDPI As Long

        Dim tExpRect As tagRECT

        Dim hDC As Long


        'pMxApp = Application

        pMxDoc = My.ArcMap.Application.Document

        pActiveView = pMxDoc.PageLayout

        pPageLayout = pMxDoc.PageLayout

        pActiveView = pPageLayout


        pExport = New PDFCreate

        pExportpdf = pExport


        pPixelEnv = New Envelope


        pExport.ExportFileName = " " & ".pdf"

        pExport.Resolution = lDPI


        pExportpdf.EmbedFonts = True


        pPixelEnv.PutCoords(0, 0, lDPI * PageExtent(pPageLayout).UpperRight.X, _

                                  lDPI * PageExtent(pPageLayout).UpperRight.Y)

        pExport.PixelBounds = pPixelEnv


        ' (device coordinates origin is upper left, ypositive is down)

        tExpRect.left = pExport.PixelBounds.LowerLeft.X

        tExpRect.bottom = pExport.PixelBounds.UpperRight.Y

        tExpRect.right = pExport.PixelBounds.UpperRight.X = pExport.PixelBounds.LowerLeft.Y


        hDC = pExport.StartExporting

        pActiveView.Output(hDC, lDPI, tExpRect, Nothing, Nothing)




    End Sub


    Public Function PageExtent(pPageLayout) As IEnvelope

        Dim dWidth As Double, dHeight As Double

        pPageLayout.Page.QuerySize(dWidth, dHeight)

        Dim pEnv As IEnvelope

        pEnv = New Envelope

        pEnv.PutCoords(0.0#, 0.0#, dWidth, dHeight)

        PageExtent = pEnv

    End Function


End Class