Convert AutoCAD Files to PDF in C#
Goal:
Create a command line sample program to submit an
AutoCAD le to a PrizmDoc server and have it converted
to PDF
Requirements:
Development IDE (Visual Studio 2017 used in this
example)
Programming Skill:
●C# intermediate level
www.accusoft.com
There are people who have only worked in ASCII les and swear that Vim or emacs is the only
way to do things. I don’t begrudge them that. Sometimes nano is just the way to go.
But for the rest of the world, text just isn’t going to cut it when you’re trying to share blueprints
of a building, or how the components of a complex machine go together. For those, we want
something like AutoCAD to get those details down.
When we’re ready to share it with the world, we should get it into PDF so everyone can marvel
at our lovely lines and drawings. To make that conversion, not all our engineers will have
Adobe Acrobat or other programs that can convert les to PDF.
Rather than making them install custom software on their systems, we can create a program
that uses the power of PrizmDoc Cloud to send the AutoCAD le to PrizmDoc, have it
converted, and then download the converted version back onto our systems.
This sample software will demonstrate how to perform all of this by leveraging the REST API
functionality from PrizmDoc, using the C# programming language.
For those who are already well versed in software, the sample code is in the link below. For a
full walkthrough of the code and how it works, a detailed explanation follows.
From Architecture to PDF
www.accusoft.com
Setting up the system
Before we do anything, we’re going to need access to a PrizmDoc Server instance. Instead of
setting up PrizmDoc Enterprise and running it on a local server, the easiest thing to do is to use
PrizmDoc Cloud. You can sign up for a PrizmDoc Cloud account at https://cloud.accusoft.com.
Once you’ve logged in to your account, make a copy of your API key. You’’l need it for our project.
www.accusoft.com
Since we’re using standard REST API calls, setting up our software development platform will
focus on making sure it can communicate via HTTP and send/receive JSON data.
Start by launching Visual Studio. This example uses Visual Studio 2017, but any version that
supports HTTP communications and the NuGet package manager will work. Create a new C#
project. We’ll call it PrizmDoc_CAD2PDF_CS, which may be the greatest name of a program
since LighteningLizard2000.
To add JSON functionality, we use the Newtonsoft.Json package. The easiest way to do that
is to click on the menu bar and select Tools, then NuGet Package Manager, then Manage
NuGet Packages for Solution.
Click on Browse, and either Newtonsoft.Json into the search eld. Odds are it’ll be available
from the start. Click it, then select the project, and click Install:
www.accusoft.com
Before we code, there’s a few les we want to add. Right click on the project in the Solution
Explorer, Add, then New Empty File. Name it “cong.json”:
www.accusoft.com
We’ll store our APIKey here as a JSON le with the following code, and include the PrizmDoc
API URL as well. This way if we decide to use a private server later instead of the PrizmDoc
Cloud, we can change that here:
Place the API Key we’ll be using in place of “YOUR API HERE” keeping the quotes around it. In
the Solution Explorer, make sure the Copy to Output Directory is set to Copy if Newer.
Code 1
www.accusoft.com
One last thing before we code. We’ll need an AutoCAD le to work with. In the sample code
provided above is a le titled GRIPPER.dwg. This is this lovely rendering of a mechanical arm
we’ll be using as our conversion test:
www.accusoft.com
Our preparations are complete. Time to get to coding!
Code Walk-Through
Converting les using PrizmDoc’s REST API follows a four-step process:
●Upload File: The le to be converted is uploaded to the PrizmDoc server. It will be stored
temporarily as a Work File and assigned an identier. A Work File is part of the PrizmDoc
API:
The work le service is a temporary storage system which allows you to upload le input
and download le output. Each work le has a unique leID which can be used to pass that
le as input to a process or viewing session. It can also be used to download the raw bytes
of the le.
Request Convert: JSON data is uploaded to the PrizmDoc server specifying the Work File
to be converted, what to convert the Work File to, and other options.
Get Convert Status: This retrieves the status of the conversion as JSON data. When
complete, this JSON data will include the Work File ID of the converted le.
Download File: The converted le is downloaded.
To work our way through the process, the code demonstration will make use of asynchronous
functions. This way we aren’t dependent on having the entire program wait for the
communication process to complete, and our future code can shoot o the task and proceed
when there is a response.
With that, let’s go through each step.
Add this le to our project. To make sure it gets copied to our Debug directory, click on the
GRIPPER.dwg le in the Solution Explorer. In File Properties, set the Copy to Output Directory
to Copy if newer:
www.accusoft.com
Before conversion
Before we start the conversion, we’ll want our program to load the API Key. Our method reads
the le and parses the JSON data:
Code 2
www.accusoft.com
Upload the work le
Before PrizmDoc can convert a le, it needs to have the le. So we’ll upload it.
To handle our conversions, our sample code uses a class we’re calling PrizmDocConverter. It
will handle the HTTP communications with the PrizmDoc server and read and write the les.
Since the communication is mostly in JSON objects, we’ll need the JSON parsing library. So
make sure these namespaces are available in our project:
Code 3
To perform the conversion, we have our method WorkFileUpload. It takes in the string name of
the le we’re uploading and creates a HTTP connection to the PrizmDoc server:
Code 4
We need to provide the PrizmDoc server with HTTP POST headers to authenticate ourselves.
The options can be found at https://www.accusoft.com/PrizmDoc/documentation/latest/
webframe.html#work-les.html:
www.accusoft.com
Code 5
From here, our code just uploads the le. When it is complete, the PrizmDoc server returns a
JSON object as a string, which we’ll return as a JSON object:
Code 6
www.accusoft.com
Code 7
Of this information, the leId and the anityToken are the two important pieces we’ll need
in future function calls. To proceed with the next steps, we’ll need to store the leId and the
anityToken. Now that the le is uploaded, and PrizmDoc has let us know our information, we
can proceed with the conversion request.
Code 8
Request the conversion
To convert our uploaded le, we’ll craft a JSON object for the PrizmDoc server via the method
WorkFileConvert. We will feed it the anityToken and leId we retrieved from the upload
process, and the format to convert it to. In this case, our method will default to pdf.
Let’s take a look at what a typical upload response looks like:
www.accusoft.com
For the conversion process, we need to provide the following headers:
●Content-Type: This will be application/json since we’re providing a JSON le
Acs-api-key: The API key to authenticate ourselves.
Accusoft-Anity-Token: The Anity Token retrieved during the upload process.
The JSON le we’ll be uploading is based on the PrizmDoc conversion format. The details are
available in .
Here’s an example of the JSON data we’ll be using:
Code 9
One thing to notice is that multiple les can have a conversion request issued with one
conversion request, and each one will be given its own processId. This will be used to track the
conversion process. Until the process is complete, we can’t download the converted le.
Retrieve conversion status
It takes time to convert a le. PrizmDoc is fast, but still can take time depending on the size of
the le to be converted. To check on where the conversion is at, we’ll use our ConvertStatus
method:
www.accusoft.com
Code 10
To get the Convert Status, only two headers are required:
●the API key
the anityToken we retrieved earlier to track the process:
What we want to do is track the process. So every 2 seconds, we’ll send another Convert
Status request. If it’s complete, then we’ll proceed. If there’s an error, then we’ll stop the
process:
www.accusoft.com
Code 11
As it processes, the PrizmDoc server will return a JSON object with the current status and
percent complete. Here’s an example of how it looks as it’s processing:
Code 12
www.accusoft.com
Once the state eld is set to complete, we’ll want to get the leId of the converted le. Note
that we can convert a document into multiple les, like converting each page into a PNG
image. Because of this, we’ll get back the output as an array and each output will have its own
leId to download later. For this example, we’re just converting our single AutoCAD le into a
single PDF File. Here’s our completed conversion JSON string:
Code 13
With our new leId, we can retrieve our completed le. Pay attention - this part goes fast.
www.accusoft.com
Download the converted le
To download any work le from PrizmDoc, we just reuse the original Work File request, only
we specify the leId we’ll be downloading. We’ll use the same API key and anity token from
before, and then save the le retrieved from PrizmDoc:
Code 15
www.accusoft.com
There we have it. Without any special code or software on the client side, we can let PrizmDoc
handle the conversion requests. This example just covers how to convert an AutoCAD le to
PDF, but PrizmDoc supports a whole series of le types to convert from, and can convert to
dierent les from PDF to images.
Feel free to try this code, and save yourself and your company time handling documents with
PrizmDoc. Let us know if you any questions or concerns in the comments below, or contact us
if you get stuck or have ideas of your own about using this powerful API toolkit.
And that’s it! Our sample program is made to take in two arguments - the name of the le
we’re reading from and the name of the le to write to, so we can call it this way: