Export Page Layout to PDF using ESRI add-in with vb.net

3668
1
Jump to solution
03-19-2015 06:46 AM
Highlighted
Occasional Contributor III

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

I have been trying to convert a vba script to vb.net 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.

Tiffany

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

        PDFCreatemodule()

    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

        tExpRect.top = pExport.PixelBounds.LowerLeft.Y

        hDC = pExport.StartExporting

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

        System.Windows.Forms.Application.DoEvents()

        pExport.FinishExporting()

    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

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Occasional Contributor III

No responses here but just in case this helps someone out in the future, here is the now working code.

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 PDFCreation
  
Inherits ESRI.ArcGIS.Desktop.AddIns.Button

  
Protected Overrides Sub OnClick()
  
PDFModule()
  
End Sub

  
Protected Overrides Sub OnUpdate()
  
Enabled = My.ArcMap.Application IsNot Nothing
  
End Sub

  
Public Sub PDFModule()
  
Dim pMxDoc As IMxDocument
  
Dim pActiveView As IActiveView
  
Dim pPageLayout As IPageLayout
  
Dim pExport As IExport
  
Dim pPixelEnv As IEnvelope
  
Dim lDPI As Long
  
Dim tExpRect As tagRECT
  
Dim hDC As Long
  
Dim res = 96

  pMxDoc
= My.ArcMap.Application.Document
  pPageLayout
= pMxDoc.PageLayout
  pActiveView
= pPageLayout

  pExport
= New ESRI.ArcGIS.Output.ExportPDF
  pPixelEnv
= New Envelope

  lDPI
= 300
  pExport
.Resolution = lDPI

  tExpRect
.left = 0
  tExpRect
.top = 0
  tExpRect
.bottom = My.ArcMap.Document.ActiveView.ExportFrame.bottom * (lDPI / res)
  tExpRect
.right = My.ArcMap.Document.ActiveView.ExportFrame.right * (lDPI / res)

  pPixelEnv
.PutCoords(tExpRect.left, tExpRect.top, tExpRect.right, tExpRect.bottom)
  pExport
.PixelBounds = pPixelEnv

  
Dim pdfName As String = ""
  
Dim saveFileDialog1 As New SaveFileDialog
  saveFileDialog1
.Filter = "PDF File|*.pdf"
  saveFileDialog1
.Title = "Save A PDF File"
  saveFileDialog1
.FileName = pdfName
  saveFileDialog1
.ShowDialog()

  pExport
.ExportFileName = saveFileDialog1.FileName
  hDC
= pExport.StartExporting
  pActiveView
.Output(hDC, lDPI, tExpRect, Nothing, Nothing)
  pExport
.FinishExporting()

  
MessageBox.Show("Finished Exporting Map")

  pExport
.Cleanup()

  
End Sub
End Class

View solution in original post

Reply
0 Kudos
1 Reply
Highlighted
Occasional Contributor III

No responses here but just in case this helps someone out in the future, here is the now working code.

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 PDFCreation
  
Inherits ESRI.ArcGIS.Desktop.AddIns.Button

  
Protected Overrides Sub OnClick()
  
PDFModule()
  
End Sub

  
Protected Overrides Sub OnUpdate()
  
Enabled = My.ArcMap.Application IsNot Nothing
  
End Sub

  
Public Sub PDFModule()
  
Dim pMxDoc As IMxDocument
  
Dim pActiveView As IActiveView
  
Dim pPageLayout As IPageLayout
  
Dim pExport As IExport
  
Dim pPixelEnv As IEnvelope
  
Dim lDPI As Long
  
Dim tExpRect As tagRECT
  
Dim hDC As Long
  
Dim res = 96

  pMxDoc
= My.ArcMap.Application.Document
  pPageLayout
= pMxDoc.PageLayout
  pActiveView
= pPageLayout

  pExport
= New ESRI.ArcGIS.Output.ExportPDF
  pPixelEnv
= New Envelope

  lDPI
= 300
  pExport
.Resolution = lDPI

  tExpRect
.left = 0
  tExpRect
.top = 0
  tExpRect
.bottom = My.ArcMap.Document.ActiveView.ExportFrame.bottom * (lDPI / res)
  tExpRect
.right = My.ArcMap.Document.ActiveView.ExportFrame.right * (lDPI / res)

  pPixelEnv
.PutCoords(tExpRect.left, tExpRect.top, tExpRect.right, tExpRect.bottom)
  pExport
.PixelBounds = pPixelEnv

  
Dim pdfName As String = ""
  
Dim saveFileDialog1 As New SaveFileDialog
  saveFileDialog1
.Filter = "PDF File|*.pdf"
  saveFileDialog1
.Title = "Save A PDF File"
  saveFileDialog1
.FileName = pdfName
  saveFileDialog1
.ShowDialog()

  pExport
.ExportFileName = saveFileDialog1.FileName
  hDC
= pExport.StartExporting
  pActiveView
.Output(hDC, lDPI, tExpRect, Nothing, Nothing)
  pExport
.FinishExporting()

  
MessageBox.Show("Finished Exporting Map")

  pExport
.Cleanup()

  
End Sub
End Class

View solution in original post

Reply
0 Kudos