Skip to Main Content

How to create a command line program in C# that can read a CAD file



Create a command line program in C# that can read a CAD file (DWG, DXF, or DGN format), then use ImageGear .NET v24 to convert this file to a PDF file with either 2D image conversion or 3D images from the original CAD file.




Computer-Aided Design To Portable Document Format

One of my favorite school projects was Build Your Own House. We were given an imaginary mortgage value, graph paper, and the task of building our dream house based on that budget. I agonized over where to place doors, and how to make sure there was enough room for them to open into my private office where I’d have a collection of Transformer and Teenage Ninja Turtles. Using a ruler, I marked off the dimensions of every room, how my kitchen would be, and because I was 10 years old at the time and knew I’d never marry a girl because girls are “yucky” at that age, I’d have just one big room with enough space for a Pac-Man machine in the corner.

Sadly, I never got my own Pac-Man machine. But the principles behind Computer-Aided Design (CAD) files and my old graphs of my house are the same – a way to represent on computer what my ruler and graph paper tried to. With CAD, you can represent everything from machinery to the skyscrapers. Every detail, from the smallest object to the largest structure, can be represented.

Odds are, though, not everyone has access to a CAD viewer. You want to show off that great new condo design to a prospective buyer, or automate the process of updating documentation based on the most current CAD designs. The best option would be to take your CAD files and convert them to PDF format, but without all the tedium of sitting at the computer to open and convert the files by hand.

This demonstration will show how to perform that same task using Accusoft ImageGear v24, which has built-in support for a variety of image files, PDF documents, and CAD file conversion. The sample code will be using C# as our base, but with the ImageGear .NET SDK any supported .NET language will work for you.

The sample code is below, and a code walkthrough will follow.

Get the code!


Prepping the Development Environment

Before we get started, we’ll need to perform a few tasks.

Install Visual Studio

This demonstration uses Visual Studio 2017 Community Edition, but ImageGear .NET will support previous versions.

Install ImageGear .Net SDK

An evaluation copy can be found here. As of this writing, the most current version is .NET v24, which adds support for CAD files as well as a plethora of other file formats.

The ImageGear .NET SDK installs by default under “C:UsersPublicDocumentsAccusoftImageGear.NET v24”. We’ll call this the INSTALL_DIRECTORY through the document. We’ll be referring to this directory for some files we’ll need in a moment.

Create Your Project

This demonstration shows how to construct a command line version. Fire up Visual Studio, create a New Project, and select C#, then “Console App.” Give your project a name – we’re going with “AutoCAD2PDF” here:

Once our project is created, we need some files from our ImageGear .NET SDK. Go into your ImageGear .NET SDK install directory and navigate to “INSTALL_DIRECTORYBinx64” (or “INSTALL_DIRECTORYBinx86 if you’re still working in the 32-bit world). Copy all of these files into your Visual Studio project into the Debug directory, so if you named your project AutoCAD2PDF, you’ll want to put these Accusoft resources files into “AutoCADto2DPDFAutoCADto2DPDFbinDebug”.

One other thing that will help us keep things organized. In the Debug directory, create two folders:

  • Input: Place the CAD files to be converted in here.
  • Output: The program will place the converted files into this directory.
Getting the Code Ready

We need to add in our ImageGear libraries into our project. We’re going to put in the minimum ones to make our program work. In Visual Studio, go to the menu and select Project, then “Add Reference”. Select Browse, then browse to the Debug directory where we copied the resource files and select the following DLL files:

  • ImageGear24.Formats.Common.dll
  • ImageGear24.Formats.Vector.dll
  • ImageGear24.Core.dll
  • ImageGear24.Evaluation.dll

Now it’s time to code.



Architecture of a Conversion

Converting CAD files to PDF using ImageGear requires we know a few things:

  1. What is the CAD file to read from (supported file formats are DWG, DXF, and DGN)?
  2. What will be the name of the file to write to (this program will verify that the file extension is .pdf)?
  3. What version of PDF are we writing to (this program supports version 1.3 up to 1.7, with 1.7 being the default)?
  4. Do we want to export the images as 2D or as 3D objects in our PDF file?

Optional: This step just helps keep track of your code. In the Solution Window, right click on “Program.cs” and rename it to “CAD2PDF.cs”. This is just if you want to keep your various classes organized, so if you add this code to other projects you don’t have a class with a boring name like “Program.”

What’s in a NameSpace?

Let’s set up our name spaces to add support for our various C# objects.  Add the following name spaces into the start of our CAD2PDF.cs file:



We’ll focus on our main class first, where we get our parameters. Our main class is looking for four parameters we defined above: the CAD file to read, the PDF file to write to, what version of PDF (listed as “PDFv1_3”, “PDFv1_4”, and so on to “PDFv1_7”), and whether we want a “2D” or “3D”. So a typical program call will look like this:



Here’s how we set up our main function to track this. If we don’t get these four arguments, the program will exit:



Before we go any further, we’ll want to validate and make sure that everything will run right. Our function ValidateParameters will make sure that our input file exists, that we can write to our output files, and that both files are the proper format as far as file extensions for CAD and PDF:



Next, we want to set up the save options for our CAD to PDF conversion. All of these are stored in our function BuildPDFOptions, which takes our PDF version settings, whether it’s going to be in 2D or 3D, and then puts them together in the ImageGear CADPDFSaveOptions object. It sets up the metadata for our PDF file, the dots per inch settings, and the background color. In this case, we’re setting up the page background to be white. If you need more information on the various CADPDFSaveOptions settings, check out the ImageGear .NET documentation



If everything checks out, we’ll use the real workhorse function, ExportCADtoPDF. This is the meat of the ImageGear experience. We’ll take in our CADPDFSaveOptions, the inputFile and outputFile and run the conversion process:



Once the evaluation settings are complete, the conversion code very quick, so don’t blink or you might miss something. First, we read the CAD file into a ImageGear ImGearCADPage object:



Well, that was simple enough. Now we just use our CADPDFSaveOptions settings, and write to our target file:



“That’s it?,” you may ask. That’s it. There are other options you can pursue using Accusoft, such as which pages to export. But let’s use an example. Here’s a CAD rendering of a gripper arm-cute little thing:




You can get your own copy from AutoDesk here.

So how does it look when we run it through our program and bring it to a 2D PDF?

And here’s the PDF rendering with a white background:


Let’s have some fun. Go back into the BuildPDFOptions function and let’s change the background from white:



To black:



And how does our converted PDF file look now?

The movie TRON would be so proud of this look.

Give the source code a try and play with it, and see if ImageGear meets your image editing and converting needs. Want to know more? Read the official documentation, try out the code samples, or feel free to contact us and see how we can meet your needs.