All Resources
Code SnippetAutomation2026-03-10
Batch Export Drawings to PDF
Iterate through a folder of SolidWorks drawing files (.slddrw) and export each as a PDF, preserving sheet sizes and custom properties.
csharp
public void BatchExportPdf(string folderPath)
{
var swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
foreach (var file in Directory.GetFiles(folderPath, "*.slddrw"))
{
int errors = 0, warnings = 0;
var doc = swApp.OpenDoc6(file,
(int)swDocumentTypes_e.swDocDRAWING,
(int)swOpenDocOptions_e.swOpenDocOptions_Silent,
"", ref errors, ref warnings);
var pdfPath = Path.ChangeExtension(file, ".pdf");
doc.Extension.SaveAs3(pdfPath,
(int)swSaveAsVersion_e.swSaveAsCurrentVersion,
(int)swSaveAsOptions_e.swSaveAsOptions_Silent,
null, ref errors, ref warnings);
swApp.CloseDoc(doc.GetTitle());
}
}How It Works
This method connects to a running SolidWorks instance, scans a folder for drawing files, and exports each one to PDF:
- Connects to SolidWorks via COM using Marshal.GetActiveObject
- Opens each .slddrw file silently (no UI prompts)
- Saves as PDF using the SaveAs3 method with silent options
- Closes the document to free memory before processing the next file
When to Use This
- Release packages: Generating PDF drawing sets for manufacturing or client delivery
- Nightly builds: Running as a scheduled task to keep PDFs in sync with latest drawing revisions
- ECN processing: Bulk-exporting affected drawings after an engineering change notice
Tips
- Add error handling around OpenDoc6 — files may fail to open if references are missing
- Use swExportPdfData for advanced PDF options (page range, DPI, watermarks)
- For multi-sheet drawings, all sheets export by default — use the Sheet API to control which sheets are included
- Consider running SolidWorks in background mode for better performance on large batches
SolidWorks APIC#PDFBatch