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 25th. Querysize statement not working.
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geometry
Imports System.Windows.Forms
Imports ESRI.ArcGIS.Output
Imports ESRI.ArcGIS.ArcMapUI
Public Class PDFCreation
Inherits ESRI.ArcGIS.Desktop.AddIns.Button
Public Sub New()
End Sub
Protected Overrides Sub OnClick()
'
' TODO: Sample code showing how to access button host
'
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 pExport2 As IExportPDF
Dim pPixelEnv As IEnvelope
Dim lDPI As Long
Dim tExpRect As tagRECT
Dim hDC As Long
Dim strOutPut As String = ""
pMxDoc = My.ArcMap.Application.Document
pActiveView = pMxDoc.PageLayout
pPageLayout = pMxDoc.PageLayout
pActiveView = pPageLayout
pExport = New ESRI.ArcGIS.Output.ExportPDF
pExport2 = pExport
pPixelEnv = New Envelope
pExport.ExportFileName = strOutPut
'pExport.ExportFileName = " " & ".pdf"
lDPI = 300
pExport.Resolution = lDPI
pExport2.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
Dim dHeight As Double
Dim pSize As IPage
pSize = pPageLayout.Page
pSize.QuerySize(dWidth, dHeight)
Dim pEnv As IEnvelope
pEnv = New Envelope
pEnv.PutCoords(0.0#, 0.0#, dWidth, dHeight)
PageExtent = pEnv
End Function
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...
Aucun commentaire:
Enregistrer un commentaire