Hide Table of Contents

Save File as PDF Example (C#)

This example shows how to save multiple drawing sheets to a PDF file.

//-----------------------------------

// Preconditions:

// 1. Specified drawing document to open exists.

// 2. The folder (c:\test) to which to save

//    the PDF file, exists. If it does not exist,

//    create it or change the path to one that

//    exists on your system.

//

// Postconditions:

// 1. Specified drawing document is opened.

// 2. All but the last drawing sheet are saved to an array.

// 3. The array of drawing sheets are saved to a PDF

//    file called foodprocessor.pdf.

//---------------------------------------

using System.Runtime.InteropServices;

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using System;

namespace SetSheetsMacroCSharp.csproj

{

    partial class SolidWorksMacro

    {

        public void Main()

        {

            ExportPdfData swExportPDFData = default(ExportPdfData);

            ModelDoc2 swModel = default(ModelDoc2);

            ModelDocExtension swModExt = default(ModelDocExtension);

            DrawingDoc swDrawDoc = default(DrawingDoc);

            Sheet swSheet = default(Sheet);

            bool boolstatus = false;

            string filename = null;

            int errors = 0;

            int warnings = 0;

            string[] obj = null;

            DispatchWrapper[] dispWrapArr = null;

 

            // Save PDF file to this folder and filename

            filename = "c:\\test\\foodprocessor.pdf";

 

            // Open drawing document

            swModel = (ModelDoc2)swApp.OpenDoc6("C:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\advdrawings\\foodprocessor.slddrw", (int)swDocumentTypes_e.swDocDRAWING, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);

            swModExt = (ModelDocExtension)swModel.Extension;

            swExportPDFData = (ExportPdfData)swApp.GetExportFileData((int)swExportDataFileType_e.swExportPdfData);

 

            // Get the names of the drawing sheets in the drawing

            // to get the size of the array of drawing sheets

            swDrawDoc = (DrawingDoc)swModel;

            obj = (string[])swDrawDoc.GetSheetNames();

            int count = 0;

            count = obj.Length;

            int i = 0;

            object[] objs = new object[count-1];

            // Active each drawing sheet, except the last drawing sheet for

            // demonstration purposes only, and add each sheet to an array

            // of drawing sheets

            for (i = 0; i < count - 1; i++)

            {

                boolstatus = swDrawDoc.ActivateSheet((obj[i]));

                swSheet = (Sheet)swDrawDoc.GetCurrentSheet();

                objs[i] = swSheet;

            }

 

            // Convert the .NET array of drawing sheets objects to IDispatch

            dispWrapArr = (DispatchWrapper[])ObjectArrayToDispatchWrapperArray((objs));

 

            // Save the drawings sheets to a PDF file

            boolstatus = swExportPDFData.SetSheets((int)swExportDataSheetsToExport_e.swExportData_ExportSpecifiedSheets, (dispWrapArr));

            boolstatus = swModExt.SaveAs(filename, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, swExportPDFData, ref errors, ref warnings);

        }

 

        public DispatchWrapper[] ObjectArrayToDispatchWrapperArray(object[] Objects)

        {

            int ArraySize = 0;

            ArraySize = Objects.GetUpperBound(0);

            DispatchWrapper[] d = new DispatchWrapper[ArraySize + 1];

            int ArrayIndex = 0;

            for (ArrayIndex = 0; ArrayIndex <= ArraySize; ArrayIndex++)

            {

                d[ArrayIndex] = new DispatchWrapper(Objects[ArrayIndex]);

            }

            return d;

        }

 

        /// <summary>

        /// The SldWorks swApp variable is pre-assigned for you.

        /// </summary>

        public SldWorks swApp;

 

    }

}



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 File as PDF Example (C#)
*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) 2012 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.