Convert CAD Drawings to PDF in Java

CAD stands for Computer-Aided Design. It is used to create 2D and 3D architectural designs, computer animations, special effects in movies, technical drawings, and much more. PDF is one of the most well-known file formats that is famous due to its portability. Here comes the need to convert CAD files to PDF format when such technical drawings are to be transferred to a normal user who is not equipped with the technical software that supports CAD drawings. This article will help programmers to add the feature to convert different CAD formats like DWG, DGN, or DWF into PDF in Java applications.

Convert CAD Drawings to PDF in Java

Following topics will be covered in this article:

CAD Drawings Conversion Java Library

Convert Documents and Images using Java

GroupDocs.Conversion for Java is the document and image conversion library for Java-based applications. It supports too many file formats for conversion from one format to another, which include:

  • Word-processing documents
  • Spreadsheets
  • Presentations
  • Images
  • Portable documents
  • Web pages
  • Photoshop formats
  • Microsoft Project files
  • Emails
  • Vector graphics by Microsoft Visio
  • CAD drawings
  • Page descriptive languages.

In the examples below, I will be using this API for the conversion of CAD drawings to PDF in Java. It will be better if you download the library and prepare the development environment beforehand. You may get the API from the downloads section or by following configuration in your maven-based Java applications.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-conversion</artifactId>
        <version>20.10</version> 
</dependency>

Converting CAD Drawings (DWG, DWF, DGN) to PDF in Java

These steps will allow you to easily convert the CAD drawings with lot of options into customized PDF file.

  • Load CAD drawing
  • Specify layouts
  • Specify options
  • Convert CAD with options to PDF

Load CAD Drawings

Load the CAD file using the CadLoadOptions class.

CadLoadOptions loadOptions =  new CadLoadOptions();

Specify Layouts

You can specify certain layouts using setLayoutNames method to convert only the mentioned layouts.

loadOptions.setLayoutNames(new  String[]{ "Layout1", "Layout3" });

Specify Options

You may specify some more options like setting the width, height, and format using the CadLoadOptions object.

loadOptions.setWidth(1920);
loadOptions.setHeight(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.

Converter converter = new Converter("cad-with-layers-and-layouts.dwg", loadOptions);
PdfConvertOptions options = new PdfConvertOptions();
converter.convert("converted.pdf", options);

Complete Code

Here is the complete java code that demonstrates by combining the above steps and converts DWG file into PDF.

With a little change in code, you can also convert DGN and DWF files according to your needs. For a file format that does not support layouts, we will not use the setLayoutNames method for setting layouts. For such little modifications, you may visit documentation.

Conclusion

I hope you are now confident with the conversion of CAD files to PDF using the GroupDocs.Conversion in your Java applications. In case of any confusion, you may contact the Free Support Team, which always feels good to help you on the forum.

See Also