Computer-Aided Design (CAD) is widely used by designers to create various designs for buildings, bridges, automobiles, and more. However, CAD formats like DWG and DXF require specific software or an online CAD viewer for viewing. To enhance accessibility, converting these DWG/DXF files to PDF format allows for easy viewing anywhere, without any software dependencies. In this article, you’ll discover how to convert CAD’s DWG or DXF files to PDF using C#.
Table of Contents
- C# CAD to PDF Converter API
- Convert DWG/DXF to PDF using C#
- Set Canvas Size in DWG/DXF to PDF using C#
- Perform Auto Scaling in CAD to PDF in C#
- Modify Background and Drawing Color in PDF using C#
- Convert Specific Layers of CAD to PDF using C#
C# CAD to PDF Converter - Free Download
Aspose.CAD for .NET is a robust CAD to PDF converter API that enables you to effortlessly convert DWG and DXF files to PDF format. Additionally, it allows for the conversion of CAD drawings to raster images. You can easily install the API via NuGet or download it as a DLL from the Downloads section.
PM> Install-Package Aspose.CAD
DWG or DXF to PDF C# Conversion
Here are the steps to convert a DWG/DXF file to PDF format using the Aspose.CAD for .NET API:
- Load the AutoCAD DWG/DXF file using the Image class.
- Create an instance of the PdfOptions class.
- Save the AutoCAD drawing as a PDF using the Image.Save(String, ImageOptionsBase) method.
The following code sample illustrates how to convert DWG to PDF using C#.
C# DWG/DXF to PDF - Set Canvas Size
Aspose.CAD for .NET also allows you to specify the page dimensions (height and width) in the converted PDF document. Follow these steps to customize the page size during the CAD to PDF conversion:
- Load the AutoCAD DWG/DXF file using the Image class.
- Create an instance of the CadRasterizationOptions class.
- Set the CadRasterizationOptions.PageWidth and CadRasterizationOptions.PageHeight properties.
- Create an instance of the PdfOptions class.
- Assign the PdfOptions.VectorRasterizationOptions property with the CadRasterizationOptions object.
- Convert DWG or DXF to PDF using the Image.Save(String, ImageOptionsBase) method.
The following code sample demonstrates how to convert DXF to PDF with a specified canvas size using C#.
C# CAD to PDF - Auto Scaling
When dealing with CAD drawings that consist of multiple layers, each layer may have different dimensions. In such cases, you may want to apply a unified scaling in the converted PDF file. Here’s how to perform auto-scaling during the AutoCAD to PDF conversion:
- Load the DWG/DXF file using the Image class.
- Create an instance of the CadRasterizationOptions class.
- Set the CadRasterizationOptions.AutomaticLayoutsScaling to true.
- Create an instance of the PdfOptions class and initialize the PdfOptions.VectorRasterizationOptions property with the CadRasterizationOptions object.
- Convert the CAD drawing to PDF using the Image.Save(String, ImageOptionsBase) method.
The following code sample illustrates how to convert a CAD drawing to PDF with auto-scaling using C#.
Modify Background and Drawing Color in Converted PDF
Aspose.CAD for .NET also allows you to customize the color scheme of the CAD drawings in the converted PDF document. Follow these steps to specify the background and drawing colors:
- Load the DWG/DXF file using the Image class.
- Create an instance of the CadRasterizationOptions class.
- Set the background color using the CadRasterizationOptions.BackgroundColor property.
- Set the drawing color using the CadRasterizationOptions.DrawColor property.
- Create an instance of the PdfOptions class and initialize the PdfOptions.VectorRasterizationOptions property with the CadRasterizationOptions object.
- Convert DWG/DXF to PDF using the Image.Save(String, ImageOptionsBase) method.
Here’s a code sample that demonstrates how to convert a DWG/DXF drawing to PDF with customized background and drawing colors.
Convert Specific Layers of CAD to PDF using C#
If your AutoCAD drawing contains multiple layers, you can select specific layers to include in the converted PDF document. To do this, provide a list of the layer names to the API using the CadRasterizationOptions.Layers property. The following code sample demonstrates how to convert a specific layer of the AutoCAD drawing to PDF using C#.
Conclusion
In this article, you have learned how to convert CAD’s DWG or DXF files to PDF using C#. You also discovered how to customize the CAD to PDF conversion process in various scenarios using the Aspose.CAD for .NET API. For further exploration, you can refer to the documentation.