Posts: 4
Registered: ‎05-24-2012
Message 1 of 7 (2,228 Views)

Export pdf from VB.net

2228 Views, 6 Replies
05-24-2012 06:32 PM



  I'm trying to make an application that would take a drawing(dwg) and convert it to pdf.  Since I can do it directly in autocad 2013, i presume that there should be a function in the API, that i could use to do the same.  So far, i tried with AcadDocument.export(filename, FileExtension, AcadSelectionSet) with no luck. I cannot put PDF as a valid file extension.


Here's my code


Public sub SaveToPDF(ByVal psfilename As String)

Dim oAutocad As AutoCAD.AcadApplication = New AutoCAD.AcadApplication()

Dim oAcadDoc As AutoCAD.AcadDocument = oAutocad.Documents.Open(psfilename, True)

Dim sFolder As String = psfilename.Substring(0, psfilename.LastIndexOf("\") + 1)

Dim sFilename As String = psfilename.Substring(psfilename.LastIndexOf("\") + 1, (psfilename.Length - psfilename.LastIndexOf("\")) - (psfilename.Length - psfilename.LastIndexOf(".") + 1))

Dim oSS As AcadSelectionSet = Nothing

Try  oSS = oAutocad.ActiveDocument.SelectionSets.Add("MySet")


     oAutocad.ActiveDocument.Export(sFolder & sFilename & ".pdf", ".pdf", oSS)

     oSS.Delete() Catch ex As Exception  success = False  oSS.Delete()  

     oSS = Nothing

End Try


For Each Doc In oAutocad.Documents




oAutocad = Nothing

end sub


Does anyone have an idea?



*Expert Elite*
Posts: 9,084
Registered: ‎06-29-2007
Message 2 of 7 (2,214 Views)

Re: Export pdf from VB.net

05-24-2012 11:32 PM in reply to: andre.fortin



you will have to go through the plot-functionality using the "DWG to PDF.pc3"

That makes also more sense for setting the page-size you like to have as result.


- alfred -

Ingenieur Studio HOLLAUS ... www.hollaus.at
*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 7 (2,207 Views)

Re: Export pdf from VB.net

05-25-2012 04:55 AM in reply to: andre.fortin

See if this helps

    Imports System.IO
    <CommandMethod("pdfing")> _
    Public Sub TestPDF()
    End Sub
    Public Sub SaveToPDF(ByVal psfilename As String)

        Dim oAutocad As AcadApplication = New AcadApplication

        oAutocad.Visible = True

        oAutocad.WindowState = AcWindowState.acMax

        Dim success As Boolean = False

        Dim oAcadDoc As AcadDocument = oAutocad.Documents.Open(psfilename, False, "")

        'Dim sFolder As String = Path.GetDirectoryName(psfilename) 'psfilename.Substring(0, psfilename.LastIndexOf("\") + 1)

        'Dim sFilename As String = Path.GetFileNameWithoutExtension(psfilename) 'psfilename.Substring(psfilename.LastIndexOf("\") + 1, (psfilename.Length - psfilename.LastIndexOf("\")) - (psfilename.Length - psfilename.LastIndexOf(".") + 1))

        Dim oSS As AcadSelectionSet = Nothing

            Dim ftype(0) As Short
            ftype(0) = 410
            Dim fdata(0) As Object
            fdata(0) = "Model"

            oSS = oAcadDoc.SelectionSets.Add("MySet")

            oSS.Select(AcSelect.acSelectionSetAll, Nothing, Nothing, ftype, fdata)

            If oAcadDoc.Saved Then
            End If
            oAcadDoc = Nothing

            oAutocad = Nothing
            success = True
        Catch ex As Exception
            MsgBox(ex.Message + vbLf + ex.StackTrace)
            success = False

            MsgBox(vbLf + "Program ended up with result of: " + success.ToString)

        End Try

    End Sub
    Sub CreatePDF(acDoc As AcadDocument) 'by RBell

        Dim PtConfigs As AcadPlotConfigurations
        Dim PlotConfig As AcadPlotConfiguration
        Dim PtObj As AcadPlot
        Dim BackPlot As Object

        'Create a new plot configutrarion with all needed parameters
        PtObj = acDoc.Plot
        PtConfigs = acDoc.PlotConfigurations
        'Add a new plot configuration
        PtConfigs.Add("PDF", False)
        'The plot config you created become active
        PlotConfig = PtConfigs.Item("PDF")
        'Use this method to set the scale
        PlotConfig.StandardScale = AcPlotScale.acScaleToFit
        'Updates the plot
        'Here you specify the pc3 file you want to use
        PlotConfig.ConfigName = "DWG To PDF.pc3"
        'You can select the plot style table here
        PlotConfig.StyleSheet = "acad.ctb"
        'Specifies whether or not to plot using the plot styles
        PlotConfig.PlotWithPlotStyles = True

        'If you are going to create pdf files in a batch mode,
        'I would recommend to turn off the BACKGROUNDPLOT system variable,
        'so autocad will not continue to do anything until finishes
        'the pdf creation
        BackPlot = acDoc.GetVariable("BACKGROUNDPLOT")
        acDoc.SetVariable("BACKGROUNDPLOT", 0)
        'Updates the plot
        'Now you can use the PlotTofile method
        If PtObj.PlotToFile(Replace(acDoc.FullName, "dwg", "pdf"), PlotConfig.ConfigName) Then
            acDoc.Utility.Prompt(vbLf + "PDF Was Created")
            acDoc.Utility.Prompt(vbLf + "PDF Creation Unsuccessful")
        End If
        'If you wish you can delete th plot configuration you created
        'programmatically, and set the 'BACKGROUNDPLOT' system variable
        'to its original status.
        PlotConfig = Nothing
        acDoc.SetVariable("BACKGROUNDPLOT", BackPlot)

    End Sub



Posts: 4
Registered: ‎05-24-2012
Message 4 of 7 (2,197 Views)

Re: Export pdf from VB.net

05-25-2012 10:29 AM in reply to: andre.fortin

Thanks guys


  I've been able to convert my drawing in a pdf.  The next step is turning my application into a windows service.  i'm not able to start autocad.  I see the process comming up in task manager but the next line ain't executed.  Is there a way to open autocad in a windows service application?





New Member
Posts: 1
Registered: ‎03-13-2012
Message 5 of 7 (827 Views)

Re: Export pdf from VB.net

11-21-2013 05:17 AM in reply to: andre.fortin
ho andre
there is a trick you can do if you still interesets
Active Contributor
Posts: 38
Registered: ‎09-06-2007
Message 6 of 7 (794 Views)

Re: Export pdf from VB.net

11-23-2013 12:53 AM in reply to: DudiPeer



I'd be interested inthis trick...

Active Contributor
Posts: 27
Registered: ‎01-28-2013
Message 7 of 7 (774 Views)

Re: Export pdf from VB.net

11-24-2013 05:11 PM in reply to: btmsoftware

Hi Hallex.


Looks great. Is there any ways to export to pdf without open the drawing (using ReadDWGfile(...
) method.



Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.