jeudi 26 mars 2015

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


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 26th. User input for save file option not working. I am trying to design it so that the user will see a windows form and can specify the file path and file name. People will be using the button on a regular basis with the same map and saving to the same folder but with a different layout view. Hence the name needs to be specified at the time of creation. Creating the button this way (instead of using the already available export option) will eliminate 2 extra steps for the users.



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

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 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
'Dim dr As System.Windows.Forms.DialogResult
saveFileDialog1.ShowDialog()

'If (dr = System.Windows.Forms.DialogResult.OK)
' { string pdfFlFullName = strOutPut.FileName
' pdfFlPath = System.IO.Path.GetDirectoryName(pdfFlFullName)
' }
'Else
' pdfFlPath = @"C:\temp"

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...


enter image description here





1 commentaire:

  1. Best VR Football Games - YouTube
    Best VR Football Games · 50. Racket Fury · 49. X-Men · 48. X-Men 2: mp3 juice Clone Wars · 47. Racket Fury · 46. The Clans · 45.

    RépondreSupprimer