CAD2PDF

Sample C# code demonstrates how to use the Apryse CAD module for direct, high-quality conversion from DWG, DXF, DGN, DWF, and RVT to PDF. 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
6using System;
7using System.Drawing;
8using System.Drawing.Imaging;
9using System.Runtime.InteropServices;
10
11using pdftron;
12using pdftron.Common;
13using pdftron.PDF;
14using pdftron.SDF;
15
16namespace CAD2PDFTestCS
17{
18 /// <summary>
19 //---------------------------------------------------------------------------------------
20 // The following sample illustrates how to convert CAD documents (such as dwg, dgn, rvt,
21 // dxf, dwf) to pdf
22 //---------------------------------------------------------------------------------------
23 /// </summary>
24 class Class1
25 {
26 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
27 static Class1() {}
28
29 /// <summary>
30 /// Check file extension
31 /// </summary>
32 static bool IsRVTFile(string input_file_name)
33 {
34 bool rvt_input = false;
35 if (input_file_name.Length > 2)
36 {
37 if (input_file_name.Substring(input_file_name.Length - 3, 3) == "rvt")
38 {
39 rvt_input = true;
40 }
41 }
42 return rvt_input;
43 }
44
45 /// <summary>
46 /// The main entry point for the application.
47 /// </summary>
48 static void Main(string[] args)
49 {
50 // The first step in every application using PDFNet is to initialize the
51 // library and set the path to common PDF resources. The library is usually
52 // initialized only once, but calling Initialize() multiple times is also fine.
53 PDFNet.Initialize(PDFTronLicense.Key);
54 PDFNet.AddResourceSearchPath("../../../../../Lib/");
55 if (!CADModule.IsModuleAvailable())
56 {
57 Console.WriteLine();
58 Console.WriteLine("Unable to run CAD2PDFTest: Apryse SDK CAD module not available.");
59 Console.WriteLine("---------------------------------------------------------------");
60 Console.WriteLine("The CAD module is an optional add-on, available for download");
61 Console.WriteLine("at http://www.pdftron.com/. If you have already downloaded this");
62 Console.WriteLine("module, ensure that the SDK is able to find the required files");
63 Console.WriteLine("using the PDFNet::AddResourceSearchPath() function.");
64 Console.WriteLine();
65 }
66
67 // Relative path to the folder containing test files.
68 string input_path = "../../../../TestFiles/CAD/";
69 string output_path = "../../../../TestFiles/Output/";
70
71 string input_file_name = "construction drawings color-28.05.18.dwg";
72 string output_file_name = "construction drawings color-28.05.18.pdf";
73
74 if (args.Length != 0)
75 {
76 input_file_name = args[0];
77 output_file_name = input_file_name + ".pdf";
78 }
79
80 Console.WriteLine("Example cad:");
81 try
82 {
83 using (PDFDoc pdfdoc = new PDFDoc())
84 {
85 if (IsRVTFile(input_file_name))
86 {
87 CADConvertOptions opts = new CADConvertOptions();
88 opts.SetPageWidth(800);
89 opts.SetPageHeight(600);
90 opts.SetRasterDPI(150);
91
92 pdftron.PDF.Convert.FromCAD(pdfdoc, input_path + input_file_name, opts);
93 }
94 else
95 {
96 pdftron.PDF.Convert.FromCAD(pdfdoc, input_path + input_file_name, null);
97 }
98 pdfdoc.Save(output_path + output_file_name, SDFDoc.SaveOptions.e_remove_unused);
99 }
100
101 Console.WriteLine("Done.");
102 }
103 catch (PDFNetException e)
104 {
105 Console.WriteLine(e.Message);
106 }
107 PDFNet.Terminate();
108 }
109 }
110}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales