Hide Table of Contents

Save Current View to PDF Example (VBA)

This example shows how to export the current view of the drawing to a PDF file.

'----------------------------------------------------------------------------
' Preconditions:
' 1. Create a VBA macro in a software product in which VBA is
'    embedded.
' 2. Copy and paste this example into the Visual Basic IDE.
' 3. Add a reference to the DraftSight type library,
'    install_dir\bin\dsAutomation.dll.
' 4. Open the Immediate window.
' 5. Start DraftSight.
' 6. Ensure the specified document to open exists.
' 7. Run the macro.
'
' Postconditions:
' 1. Specified document is opened.
' 2. The current view of the drawing is exported to the specified PDF file.
'---------------------------------------------------------------------------
Option Explicit

Sub main()
        Dim dsApp As DraftSight.Application
        'Connect to DraftSight application
        Set dsApp = GetObject(, "DraftSight.Application")
        'Abort any command currently running in DraftSight
        'to avoid nested commands
        dsApp.AbortRunningCommand
        'Open document
        Dim dsDoc As DraftSight.Document
        Dim docName As String
        docName = "c:\ProgramData\Dassault Systemes\DraftSight\Examples\B-44563.DWG"
        Set dsDoc = dsApp.OpenDocument2(docName, dsDocumentOpenOption_e.dsDocumentOpen_Default, dsEncoding_e.dsEncoding_Default)
        If dsDoc Is Nothing Then
            MsgBox ("Specified document did not open. Check path and file name.")
            Return
        End If

        'Export current view of drawing to a PDF
        Dim dsExportDocument As DraftSight.DocumentExporter
        Dim exportFileName As String
        Dim ExportSettings As DraftSight.ExportSettings
        Dim status As Boolean
 

         'Export all sheets
        Dim sheetsList As Variant
        sheetsList = dsDoc.GetSheets2()
        ReDim sheetArray(UBound(sheetsList)) As String

        Dim i As Long
        Dim sheet As DraftSight.sheet

        For i = 0 To UBound(sheetsList)
            Set sheet = sheetsList(i)
            sheetArray(i) = sheet.Name
        Next


        Set dsExportDocument = dsDoc.GetDocumentExporter()
        Set ExportSettings = dsExportDocument.CreateExportSettings
 

         ExportSettings.Sheets = sheetArray
        ExportSettings.SetPrintMargins 30, 30, 30, 30
        ExportSettings.PrintStyleTable = "monochrome.ctb"
        ExportSettings.EnableCustomBitmapResolution = True
        ExportSettings.CustomBitmapResolution = 151
        ExportSettings.EnableLayersInPDFFile = True
        ExportSettings.TrueTypeFontsType = dsTrueTypeFontsType_e.dsTrueTypeFontsType_Embedded

        Dim paperSizes(0) As String
        ExportSettings.GetAvailablePaperSizes (paperSizes)
        ExportSettings.PaperSize = paperSizes(0)
        Dim top As Double, bottom As Double, left As Double, right As Double
        ExportSettings.GetPrintMargins top, bottom, left, right
        Debug.Print "Enable custom bitmap resolution? " & ExportSettings.EnableCustomBitmapResolution
        Debug.Print "  Custom bitmap resolution: " & ExportSettings.CustomBitmapResolution
        Debug.Print "Enable Layers in PDF file? " & ExportSettings.EnableLayersInPDFFile
        Debug.Print "PrintStyle table: " & ExportSettings.PrintStyleTable
        Debug.Print "TrueType font as defined in dsTrueTypeFontsType_e: " & ExportSettings.TrueTypeFontsType
       

        exportFileName = "C:\temp\B-44563.PDF"
        status = dsExportDocument.ExportToPdf2(exportFileName, ExportSettings)
        Debug.Print ""
        If status Then
            Debug.Print "Drawing exported as " & exportFileName & "."
        Else
            Debug.Print "Export of drawing to PDF failed!"
        End If
End Sub

           


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Save Current View to PDF Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2023 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.