Convert CAD Drawings to PDF in C#

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 document with the code example. Let’s do it in C# using the document conversion API for .NET.

Convert CAD Drawings to PDF in .NET

Following topics will be covered in this article:

C# API to Convert CAD Drawings

Convert Documents and Images using .NET

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 DLLs 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 the CAD Drawing

Now using the Convert method of the Converter class, DWG files can be easily converted to PDF format using the set options.

using (Converter converter = new Converter("with_layers_and_layouts.dwg", getLoadOptions))
{
    PdfConvertOptions options = new PdfConvertOptions();
    converter.Convert("converted.pdf", options);
}

Complete Code

Here is the complete C# code, converting a DWG file 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 advance 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 DWF 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, DWF, and DGN to PDF 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 @ groupdocs.app.

For any further queries, you may contact the Free Support Team, which is always available to help you on the forum.

Related Articles