Today, we will learn how to programmatically convert the CAD drawings to PDF format in C#. Previously, in an earlier post, we did the same but in Java. We looked to convert the DWG, DGN, and DWF files into PDF documents with the code example. Let’s do it in C# using the document conversion API for .NET.
The following topics will be covered in this article:
C# API to Convert CAD Drawings
GroupDocs.Conversion for .NET is the advanced conversion API for documents and images within any .NET application. It supports many file formats that include word-processing documents, spreadsheets, presentations, images, CAD drawings, and many more.
This article will be using GroupDocs.Conversion for .NET API for the conversion of CAD drawings to PDF in C#. You may download the DLL or get it installed using NuGet.
PM> Install-Package GroupDocs.Conversion
Convert CAD Drawings (DWG, DWF, DGN) to PDF in C#
The following steps will allow easy conversion of CAD drawings with a lot of options into a personalized PDF file.
- Load CAD drawing.
- Specify layouts and options.
- Convert CAD with options to PDF.
Load CAD Drawings
Load the CAD file using the CadLoadOptions class.
CadLoadOptions loadOptions = new CadLoadOptions();
Specify Layouts and Other options
You can specify certain properties while loading CAD files. These properties include layout names, width, height, and format. Specifying layout names will allow you to convert only the mentioned layout.
Contracts.Func<LoadOptions> getLoadOptions = () => new CadLoadOptions
{
LayoutNames = new \[\]{ "Layout1", "Layout3" },
Width = 1920,
Height = 1080
};
Convert CAD Drawings - DWG, DWF to PDF in C#
Now using the Convert method of the Converter class, DWG or DWF files can be easily converted to PDF format using the set options.
using (Converter converter = new Converter("with\_layers\_and\_layouts.dwf", getLoadOptions))
{
PdfConvertOptions options = new PdfConvertOptions();
converter.Convert("converted.pdf", options);
}
Complete Code
Here is the complete C# code, that you can use to convert DWG or DWF files into PDF by using the steps i.e. Load -> Specify Layout and Options -> Convert.
There are many other customization options for the resultant PDF format that gives control over the output result while converting any document to the PDF format. You may have a look at these advanced options in the following documentation article.
Convert to PDF with Advance Options in .NET
With a minor change, we can convert other CAD files like DGN and DWG files accordingly. We just have to provide the right filename and its format in the above code. For a file format that does not support layouts, we will not set LayoutNames. For such little modifications, you may visit the documentation.
Conclusion
I hope you are now confident with the conversion of CAD files like DWG, DGN, and DWF to PDF in C# using the GroupDocs.Conversion in your .NET as well as Java applications. You can now build your own conversion applications using any platform just like free apps available @ www.groupdocs.app.
You may contact the Free Support Team for any further queries, which is always available to help you on the forum.