Convert DWG or DXF to PDF in C#
The GdPictureDocumentConverter class is a one-step operation for converting AutoCAD Drawing (DWG) and Drawing Exchange Format (DXF) files into PDF or PDF/A. You can also choose the PDF conformance level you require. DWG support includes files using the AC1032 format (AutoCAD 2018 and later).
The example below uses DXF input, but DWG is supported as well by loading a .dwg file with the matching CAD document format.
Here’s the code for converting files to PDF:
// We assume GdPicture has been correctly installed and unlocked.using (GdPictureDocumentConverter oConverter = new GdPictureDocumentConverter()){ // Select your source document and its document format (DXF). GdPictureStatus status = oConverter.LoadFromFile("input.dxf", GdPicture14.DocumentFormat.DocumentFormatDXF); if (status == GdPictureStatus.OK) { MessageBox.Show("The file has been loaded successfully.", "Conversion to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information); // Select the conformance of the resulting PDF document. status = oConverter.SaveAsPDF("output.pdf", PdfConformance.PDF); if (status == GdPictureStatus.OK) { MessageBox.Show("The file has been saved successfully.", "Conversion to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("The file has failed to save. Status: " + status.ToString(), "Conversion to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("The file has failed to load. Status: " + status.ToString(), "Conversion to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error); }}'We assume GdPicture has been correctly installed and unlocked.Using oConverter As GdPictureDocumentConverter = New GdPictureDocumentConverter() 'Select your source document and its document format (DXF). Dim status As GdPictureStatus = oConverter.LoadFromFile("input.dxf", GdPicture14.DocumentFormat.DocumentFormatDXF) If status = GdPictureStatus.OK Then MessageBox.Show("The file has been loaded successfully.", "Conversion to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information) 'Select the conformance of the resulting PDF document. status = oConverter.SaveAsPDF("output.pdf", PdfConformance.PDF) If status = GdPictureStatus.OK Then MessageBox.Show("The file has been saved successfully.", "Conversion to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information) Else MessageBox.Show("The file has failed to save. Status: " + status.ToString(), "Conversion to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error) End If Else MessageBox.Show("The file has failed to load. Status: " + status.ToString(), "Conversion to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error) End IfEnd Using