For the complete documentation index, see llms.txt. This page is also available as Markdown.
Conversion

Convert CAD to PDF - CAD2PDF

Requirements

These packages are required to use these features in production. Trial keys have unlimited access to all features

Server SDKPackage: CAD ConversionModule: CADLive demo

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 convert CAD to PDF with Apryse SDK with this sample, you will need:

  1. Get started with Server SDK in your language/framework

  2. Add the sample code provided below

Learn more about our Server SDK and PDF Conversion Library. For a version of this in Node.js, including our WebViewer UI, check out the CAD Viewer Showcase Demo Code Sample

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.     
//---------------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

using pdftron;
using pdftron.Common;
using pdftron.PDF;
using pdftron.SDF;

namespace CAD2PDFTestCS
{
    /// <summary>
    //---------------------------------------------------------------------------------------
    // The following sample illustrates how to convert CAD documents (such as dwg, dgn, rvt, 
    // dxf, dwf) to pdf 
    //---------------------------------------------------------------------------------------
    /// </summary>
    class Class1
    {
        private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
        static Class1() {}

        /// <summary>
        /// Check file extension
        /// </summary>
        static bool IsRVTFile(string input_file_name)
        {
            bool rvt_input = false;
            if (input_file_name.Length > 2)
            {
                if (input_file_name.Substring(input_file_name.Length - 3, 3) == "rvt")
                {
                    rvt_input = true;
                }
            }
            return rvt_input;
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            // The first step in every application using PDFNet is to initialize the 
            // library and set the path to common PDF resources. The library is usually 
            // initialized only once, but calling Initialize() multiple times is also fine.
            PDFNet.Initialize(PDFTronLicense.Key);
            PDFNet.AddResourceSearchPath("../../../../../Lib/");
            if (!CADModule.IsModuleAvailable())
            {
                Console.WriteLine();
                Console.WriteLine("Unable to run CAD2PDFTest: Apryse SDK CAD module not available.");
                Console.WriteLine("---------------------------------------------------------------");
                Console.WriteLine("The CAD module is an optional add-on, available for download");
                Console.WriteLine("at http://www.pdftron.com/. If you have already downloaded this");
                Console.WriteLine("module, ensure that the SDK is able to find the required files");
                Console.WriteLine("using the PDFNet::AddResourceSearchPath() function.");
                Console.WriteLine();
            }

            // Relative path to the folder containing test files.
            string input_path =  "../../../../TestFiles/CAD/";
            string output_path = "../../../../TestFiles/Output/";

            string input_file_name = "construction drawings color-28.05.18.dwg";
            string output_file_name = "construction drawings color-28.05.18.pdf";

            if (args.Length != 0)
            {
                input_file_name = args[0];
                output_file_name = input_file_name + ".pdf";
            }

            Console.WriteLine("Example cad:");
            try
            {
                using (PDFDoc pdfdoc = new PDFDoc())
                {
                    if (IsRVTFile(input_file_name))
                    {
                        CADConvertOptions opts = new CADConvertOptions();
                        opts.SetPageWidth(800);
                        opts.SetPageHeight(600);
                        opts.SetRasterDPI(150);

                        pdftron.PDF.Convert.FromCAD(pdfdoc, input_path + input_file_name, opts);
                    }
                    else
                    {
                        pdftron.PDF.Convert.FromCAD(pdfdoc, input_path + input_file_name, null);
                    }
                    pdfdoc.Save(output_path + output_file_name, SDFDoc.SaveOptions.e_remove_unused);
                }

                Console.WriteLine("Done.");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
            PDFNet.Terminate();
        }
    }
}