Note: This message is cross-posted on geonet (http://ift.tt/1CQvNd2)
I have been trying to convert a vba script to vb.net but I have hit a roadblock and I'm 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.
Code updated on March 27th. The code now works!
Many thanks to artwork21 and Dan Jurgella for their assistance.
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
Here is my code incorporating the coding used in the ESRI active page layout pdf export code snippet. I comment out pExportpdf and instead use ExportPDF in Select Case and change pExportpdf.EmbedFonts to pExport.EmbedFonts. I include Dim ExportPDF as String.
When I use this method, I receive the error message:
Implementing class ‘ESRI.ArcGIS.Output.ExportPDFClass’ for interface 'ESRI.ArcGIS.Output.ExportPDF’ cannot be found.
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
Dim ExportFormat As String
'pMxApp = Application
pMxDoc = My.ArcMap.Application.Document
pActiveView = pMxDoc.PageLayout
pPageLayout = pMxDoc.PageLayout
pActiveView = pPageLayout
pExport = New PDFCreate
'pExportpdf = pExport
Select Case ExportFormat
Case "PDF"
pExport = New ExportPDF
End Select
pPixelEnv = New Envelope
pExport.ExportFileName = " " & ".pdf"
pExport.Resolution = lDPI
pExport.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
This is an error message I am receiving...