Convert DWG and DXF Files to PDF using Java

CAD file formats are used by the designers or architects to create drawings in various industries such as construction, automobile, hardware, and etc. DWG and DXF are the most widely used CAD formats, however, you need a dedicated software to view the content in DWG and DXF files. In order to tackle this limitation, you can convert the CAD drawings to PDF format. In this article, you will learn how to automate CAD to PDF conversion and convert DWG and DXF files to PDF using Java.

Java CAD to PDF Converter API

Aspose.CAD for Java is a powerful CAD file manipulation API that lets you convert DWG and DXF files to PDF with high fidelity. You can either download the API’s JAR or install it within your Maven-based applications using the following configurations.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-cad</artifactId>
    <version>20.10</version>
    <classifier>jdk16</classifier>
</dependency>

DWG or DXF to PDF Java Conversion

The following are the steps to convert a DWG or DXF file to PDF using Aspose.CAD for Java.

The following code sample shows how to convert DWG to PDF using Java.

Java DWG or DXF to PDF – Set Canvas Size

Aspose.CAD for Java also allows you to set the canvas size when converting CAD to PDF. Thus, you can specify the size of the pages in the converted PDF document. The following are the steps to perform this operation.

The following code sample shows how to set canvas size in DWG or DXF to PDF conversion in Java.

Auto-Scaling in CAD to PDF in Java

You can also perform auto-scaling of the pages in CAD to PDF conversion. This feature becomes useful when the layers in the CAD drawing have different dimensions. The auto-scaling feature scales the layers according to the unified page size in the PDF. The following are the steps to perform auto-scaling.

The following code sample shows how to convert DWG to PDF with auto-scaling.

Java CAD to PDF – Set Background and Drawing Color

By default, the CAD drawings are rendered in black and white colors in the PDF. However, you can override this feature and set your desired background and drawing colors. The following are the steps to achieve this.

The following code sample shows how to modify colors in DWG or DXF to PDF conversion using Java.

Convert Specific Layers of CAD to PDF using Java

CAD drawings usually consist of multiple layers and in certain cases, you may need to convert only the selected layers of the drawing to PDF. For this, you can provide a list of layers to be converted using the CadRasterizationOptions.setLayers() method. The following code sample shows how to convert a specific layer in CAD to PDF conversion.

Conclusion

In this article, you have learned how to convert CAD’s DWG and DXF files to PDF using Java. Furthermore, you have seen various scenarios in which you can customize the CAD to PDF conversion as desired. You can explore more about Aspose’s CAD API using documentation.

See Also