Convert CAD to PDF - CAD2PDF - Java Sample Code

Sample code demonstrates how to use the Apryse CAD module for direct, high-quality conversion from DWG, DXF, DGN, DWF, and RVT to PDF. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB.

To run this sample, you will need:

  1. Get started with Server SDK in your language/framework
  2. Download the CAD Module

Learn more about our Server SDK and PDF Conversion Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.common.PDFNetException;
7import com.pdftron.pdf.*;
8import com.pdftron.sdf.Obj;
9import com.pdftron.sdf.ObjSet;
10import com.pdftron.sdf.SDFDoc;
11
12//---------------------------------------------------------------------------------------
13// The following sample illustrates how to convert CAD documents to PDF format using
14// the CAD2PDF class.
15//
16// 'pdftron.PDF.CAD2PDF' is an optional PDFNet Add-On utility class that can be
17// used to convert CAD documents into PDF documents by using an external module (cad2pdf).
18//
19// cad2pdf modules can be downloaded from http://www.pdftron.com/pdfnet/downloads.html.
20//---------------------------------------------------------------------------------------
21public class CAD2PDFTest {
22
23 public static boolean IsRVTFile(String input_file_name)
24 {
25 boolean rvt_input = false;
26
27 if (input_file_name.length() > 2)
28 {
29 if (input_file_name.substring(input_file_name.length() - 3, input_file_name.length()).equals("rvt"))
30 {
31 rvt_input = true;
32 }
33 }
34 return rvt_input;
35 }
36
37 public static void main(String[] args) {
38 String input_file_name = "construction drawings color-28.05.18.dwg";
39 String output_file_name = input_file_name + ".pdf";
40 if (args.length != 0)
41 {
42 input_file_name = args[0];
43 output_file_name = input_file_name + ".pdf";
44 }
45 PDFNet.initialize(PDFTronLicense.Key());
46 try
47 {
48 PDFNet.addResourceSearchPath("../../../Lib/");
49 if(!CADModule.isModuleAvailable())
50 {
51 System.out.println();
52 System.out.println("Unable to run CAD2PDFTest: Apryse SDK CAD module not available.");
53 System.out.println("---------------------------------------------------------------");
54 System.out.println("The CAD module is an optional add-on, available for download");
55 System.out.println("at http://www.pdftron.com/. If you have already downloaded this");
56 System.out.println("module, ensure that the SDK is able to find the required files");
57 System.out.println("using the PDFNet::AddResourceSearchPath() function." );
58 System.out.println();
59 }
60 } catch (PDFNetException e) {
61 System.out.println("CAD module not available, error:");
62 e.printStackTrace();
63 System.out.println(e);
64 }
65
66
67 // Relative path to the folder containing test files.
68 String input_path = "../../TestFiles/CAD/";
69 String output_path = "../../TestFiles/Output/";
70 String outputFile;
71 boolean printerInstalled = false;
72
73 try (PDFDoc doc = new PDFDoc()) {
74 if (IsRVTFile(input_file_name))
75 {
76 CADConvertOptions opts = new CADConvertOptions();
77 opts.setPageHeight(800);
78 opts.setPageWidth(300);
79 Convert.fromCAD(doc, input_path + input_file_name, opts);
80 }
81 else
82 {
83 Convert.fromCAD(doc, input_path + input_file_name, null);
84 }
85 outputFile = output_path + output_file_name;
86 doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null);
87 } catch (PDFNetException e) {
88 System.out.println("Unable to convert DWG document, error:");
89 e.printStackTrace();
90 System.out.println(e);
91 }
92 PDFNet.terminate();
93 }
94}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales