Convert CAD to PDF - CAD2PDF - C++ 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
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <PDF/Convert.h>
9#include <PDF/CADModule.h>
10#include <PDF/CADConvertOptions.h>
11#include <string>
12#include <iostream>
13#include <stdio.h>
14#include "../../LicenseKey/CPP/LicenseKey.h"
15
16using namespace pdftron;
17using namespace pdftron::SDF;
18using namespace PDF;
19using namespace std;
20
21
22//---------------------------------------------------------------------------------------
23// The following sample illustrates how to convert CAD documents to PDF format
24//
25// The CAD module is an optional PDFNet Add-on that can be used to convert CAD
26// documents into PDF documents
27//
28// The Apryse SDK CAD module can be downloaded from http://www.pdftron.com/
29//---------------------------------------------------------------------------------------
30
31UString inputPath("../../TestFiles/CAD/");
32UString outputPath("../../TestFiles/Output/");
33
34bool IsRVTFile(UString inputFile)
35{
36 bool rvt_input = false;
37 if (inputFile.GetLength() > 2)
38 {
39 if (inputFile.SubStr(inputFile.GetLength() - 3, 3) == "rvt")
40 {
41 rvt_input = true;
42 }
43 }
44 return rvt_input;
45}
46
47int main(int argc, char *argv[])
48{
49 int ret = 0;
50
51 try
52 {
53 // The first step in every application using PDFNet is to initialize the
54 // library and set the path to common PDF resources. The library is usually
55 // initialized only once, but calling Initialize() multiple times is also fine.
56 PDFNet::Initialize(LicenseKey);
57 PDFNet::AddResourceSearchPath("../../../Lib/");
58 if(!CADModule::IsModuleAvailable())
59 {
60 cout << endl;
61 cout << "Unable to run CAD2PDFTest: Apryse SDK CAD module not available." << endl;
62 cout << "---------------------------------------------------------------" << endl;
63 cout << "The CAD module is an optional add-on, available for download" << endl;
64 cout << "at http://www.pdftron.com/. If you have already downloaded this" << endl;
65 cout << "module, ensure that the SDK is able to find the required files" << endl;
66 cout << "using the PDFNet::AddResourceSearchPath() function." << endl << endl;
67 return 0;
68 }
69
70 typedef struct
71 {
72 UString inputFile, outputFile;
73 } TestFile;
74
75 UString inputFileName, outputFileName;
76 if (argv[1])
77 {
78 inputFileName = argv[1];
79 }
80 else
81 {
82 inputFileName = "construction drawings color-28.05.18.dwg";
83 }
84 outputFileName = inputFileName + ".pdf";
85
86 TestFile testFiles[] =
87 {
88 { inputFileName, outputFileName},
89 };
90
91 unsigned int ceTestFiles = sizeof(testFiles) / sizeof(TestFile);
92 for (unsigned int i = 0; i < ceTestFiles; i++)
93 {
94
95 // Convert the rest of the samples
96 if (IsRVTFile(testFiles[i].inputFile))
97 {
98 try
99 {
100 PDFDoc pdfdoc;
101 // Convert rvt file with some user options
102 CADConvertOptions opts;
103 opts.SetPageWidth(800);
104 opts.SetPageHeight(600);
105 opts.SetRasterDPI(150);
106
107 Convert::FromCAD(pdfdoc, inputPath + testFiles[i].inputFile, &opts);
108 pdfdoc.Save(outputPath + testFiles[i].outputFile, SDF::SDFDoc::e_linearized, NULL);
109 }
110 catch (Common::Exception& e)
111 {
112 cout << "Unable to convert file " << testFiles[i].inputFile << endl;
113 cout << e << endl;
114 ret = 1;
115 }
116 catch (...)
117 {
118 cout << "Unknown Exception" << endl;
119 ret = 1;
120 }
121 }
122 else
123 {
124 try
125 {
126 PDFDoc pdfdoc;
127 Convert::FromCAD(pdfdoc, inputPath + testFiles[i].inputFile, NULL);
128 pdfdoc.Save(outputPath + testFiles[i].outputFile, SDF::SDFDoc::e_linearized, NULL);
129 }
130 catch (Common::Exception& e)
131 {
132 cout << "Unable to convert file " << testFiles[i].inputFile << endl;
133 cout << e << endl;
134 ret = 1;
135 }
136 catch (...)
137 {
138 cout << "Unknown Exception" << endl;
139 ret = 1;
140 }
141 }
142 }
143
144 }
145 catch (Common::Exception& e)
146 {
147 cout << e << endl;
148 ret = 1;
149 }
150 catch (...)
151 {
152 cout << "Unknown Exception" << endl;
153 ret = 1;
154 }
155 PDFNet::Terminate();
156 return ret;
157}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales