2

Note: This message is cross-posted on geonet (https://geonet.esri.com/thread/127249)

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


The code now works!

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

enter image description here

  • 1
    Have you seen this code snippet, Export active view, help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/… – artwork21 Mar 23 '15 at 13:48
  • I have seen that code snippet. I tried to incorporate it into my code but I could not get it to work. I ran into a problem using: . Select Case ExportFormat Case "PDF" docExport = New ExportPDF . My project did not accept “ExportPDF” as an Interface like it was doing for the ESRI snippet. My project was treating it as a class that did not have an appropriate definition. Once again I received an error related to unable to cast object to type. – MapinTX Mar 23 '15 at 13:56
  • ExportFormat is a string parameter of the function, you need to provide this. – artwork21 Mar 23 '15 at 14:00
  • When I use 'Dim Export Format As String' or 'ByVal ExportFormat As String' and replace docExport with pExport , I receive the error message: Implementing class ‘ESRI.ArcGIS.Output.ExportPDFClass’ for interface ‘ESRI.ArcGIS.Output.ExportPDF’ cannot be found. – MapinTX Mar 23 '15 at 14:11
  • Can you add the Select Case lines in your code, not seeing it. Are you providing a value for the ExportFormat variable? – artwork21 Mar 23 '15 at 14:14
2

Take out the PageExtent function, and get the tagRECT bounds from the frame to export, the output resolution, and the screen resolution (bound * (300/96)). This should work to export your page layout:

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 ExportPDF
    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 strOutPut As String = "your file path here.pdf"
        Dim res = 96

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

        pExport = New ESRI.ArcGIS.Output.ExportPDF
        pPixelEnv = New Envelope
        pExport.ExportFileName = strOutPut
        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

        hDC = pExport.StartExporting
        pActiveView.Output(hDC, lDPI, tExpRect, Nothing, Nothing)
        pExport.FinishExporting()
    End Sub
End Class
  • Even with the explicit reference I still receive the "Implementing class" error message. I receive it on 'New ExportPDF' and on 'New ESRI.ArcGIS.Output.ExportPDF'. I have checked again and I am importing and Referencing ESRI.ArcGIS.Output. I just tried taking away the import and reference and adding them again but I still have the error. And for some reason I am still receiving the "unable to cast object" error solely on line 47 even if there is no code present at that line or if all the code for my PDF Createmodule is below line 47. – MapinTX Mar 24 '15 at 18:18
  • Edited my answer to show the exact changes I made to your code as posted above (your 2nd code block). With these changes do you still get invalid cast exception? – Dan Jurgella Mar 24 '15 at 19:05
  • I made the changes you specified but I am still receiving the both the "Implementing Class" error for the ExportPDF and the "Invalid Cast exception" error at line 47. – MapinTX Mar 24 '15 at 19:27
  • You might want to edit your first post to reflect the exact code you are using at this moment, since there might be a line or two we are missing. Other than that, I'm sort of out of ideas at the moment. – Dan Jurgella Mar 24 '15 at 19:34
  • I have updated my 1st code to reflect the changes that were made from your suggestions. – MapinTX Mar 24 '15 at 19:54
1

The snippet I mentioned in my comment, http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/000100000026000000.htm, is pulling in the ExportFormat value through the function argument. When the function is called from a different method it is providing that value (e.g. "PDF" or "JPG"), which you are not using the function arguments. You need to assign that value to the variable before the Select Case statement:

e.g

    Dim pExport = IExport
    'pExport = New PDFCreate <-------------  not needed
    ExportFormat = "PDF"

    Select Case ExportFormat
        Case "PDF"
            pExport = New ExportPDF
  • I have included the ExportFormat = "PDF" into my code as you showed in your example but I am still receiving the "Implementing class ‘ESRI.ArcGIS.Output.ExportPDFClass’ for interface 'ESRI.ArcGIS.Output.ExportPDF’ cannot be found" error message. – MapinTX Mar 23 '15 at 16:49
  • I would suggest to add in a try/catch logic to see what line number you are getting the error. Do you have all the required dependencies imported? – artwork21 Mar 23 '15 at 16:55
  • As far as I know I have imported all the required dependencies. When I use breakpoints and debug I am receiving the "Unable to Cast..." message right before the Try Catch snippet for 'pExport = New PDF Create. I am also receiving the "Implementing class..." error for pExport = New ExportPDF. – MapinTX Mar 23 '15 at 17:38
  • I have included a screen shot of the messages at the bottom of my original post. – MapinTX Mar 23 '15 at 17:48
  • I do not think you need this statment, pExport = New PDFCreate – artwork21 Mar 23 '15 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.