.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Export pdf from VB.net

13 REPLIES 13
Reply
Message 1 of 14
andre.fortin
7590 Views, 13 Replies

Export pdf from VB.net

Hi

 

  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")

     oSS.Select(AcSelect.acSelectionSetAll)

     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

  Doc.Close(False)

Next

oAutocad.Quit()

oAutocad = Nothing

end sub

 

Does anyone have an idea?

 

Thanks

13 REPLIES 13
Message 2 of 14

Hi,

 

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 14
Hallex
in reply to: andre.fortin

See if this helps

    Imports System.IO
     ......................
    <CommandMethod("pdfing")> _
    Public Sub TestPDF()
        SaveToPDF("C:\Test\blah.dwg")
    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

        Try
           
            Dim ftype(0) As Short
            ftype(0) = 410
            Dim fdata(0) As Object
            fdata(0) = "Model"
            oAutocad.ZoomExtents()

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

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

            CreatePDF(oAcadDoc)
     
            If oAcadDoc.Saved Then
                oAcadDoc.Close(False)
            End If
            oAcadDoc = Nothing
         
            oAutocad.Quit()

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

            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
        PlotConfig.RefreshPlotDeviceInfo()
        '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
        PlotConfig.RefreshPlotDeviceInfo()
        ''Acad.ActiveDocument.ActiveLayout.CopyFrom(plotconfig)
        '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")
        Else
            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.
        PtConfigs.Item("PDF").Delete()
        PlotConfig = Nothing
        acDoc.SetVariable("BACKGROUNDPLOT", BackPlot)

    End Sub

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 4 of 14
andre.fortin
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?

 

Thanks

 

Andre

Message 5 of 14
DudiPeer
in reply to: andre.fortin

ho andre
there is a trick you can do if you still interesets
dudi
Message 6 of 14
btmsoftware
in reply to: DudiPeer

Dudi,

 

I'd be interested inthis trick...

Message 7 of 14
ditran
in reply to: btmsoftware

Hi Hallex.

 

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

 

Thanks,

Message 8 of 14
kellyE3ZK8
in reply to: andre.fortin

Hi andre,

 

I still have the same problem of converting the dwg file to pdf.

 

Could you please kindly show your code?

 

Thank you very much!

Tags (1)
Message 9 of 14
kellyE3ZK8
in reply to: kellyE3ZK8

Oh my mistake...

I found my pdf files

They were in "C:\Users\Administrator\AppData\Local\Temp"

Thank Hallex and RBell!

Message 10 of 14
FDauberlieu
in reply to: DudiPeer

Dudi,

 

Any chance you could share this trick with us?

Message 11 of 14
kellyE3ZK8
in reply to: FDauberlieu

I just copy the Hallex's code. It works but I thought it could not work before because I cannot find my pdf files.

 

But Hallex's code actually works! 

 

The pdf files are hidden in somewhere in the computer. You may check it out with "CreatePDF" and see his code the line of  "If PtObj.PlotToFile(Replace(acDoc.FullName, "dwg", "pdf"), PlotConfig.ConfigName) Then".

 

The variable of "FullName" is the path of the pdf files saved. 

Message 12 of 14
btmsoftware
in reply to: kellyE3ZK8

I think the tip was about using AutoCAD as a Windows service, not the PDF generation.

 

If anybody has a trip to use AutoCAD as a windows service, please, share

Message 13 of 14

Hi !

 

this may be the right way for my problem. i want to use the exportpdf-function of autocad via vb.net.

 

but what means the .....

 

 

  Imports System.IO
     ......................
    <CommandMethod("pdfing")> _
    Public Sub TestPDF()
        SaveToPDF("C:\Test\blah.dwg")
    End Sub

 so current i did know how to find the defintion of

 

  • AcadPlotConfigurations
    AcadPlotConfiguration
  • AcPlotScale
  • ...

regards Jan

Message 14 of 14
nghnbstn
in reply to: jan_tappenbeck

Hello ,Add application from the project tab then select AutoCad. 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost