Convert PDF to DWF in .Net

I was helping a customer to create DWF from PDF files. By the way I found two solutions for this problem. Both are good, and both support a couple of other file-types.

Solution 1:

The first one is to use the “Autodesk DWF Writer” printer, that has to be installed on the machine where you want to convert the pdf files. If you are using powerJobs, it will be the machine where the JobProcessor is running. Download the printer here : http://usa.autodesk.com/dwf-writer/

Next you need an application that reads PDF-files (like Adobe Reader,ec.). Now you can use this to call it via powerShell:

Start-Process -FilePath "test.pdf" –Verb Print
sleep 10
kill -name AcroRd32

This will open AdobeReader, do the printing, and closes the AdopeReader again with kill :(

The problem of this solution is, that you need to install the printer and a tool that opens the PDF file, and the printer HAS TO BE THE DEFAULT PRINTER! The code needs to kill the Adobe Process again, this means it is not working well if there are many of this processes open (It could kill the wrong one). But the benefit is, that you can print a lot of filetypes to DWF (like html,bmp,….). For more details check the autodesk DWF Writer documentation.

Solution 2:

“Autodesk Design Review” (you can install it a.e with your Vault Client) supports by default to open a PDF and save it as DWF. Autodesk provides a small SDK dokumentation for this product, and there is a powerfull control that we can use to automate this work: we are using the DWFViewer.SaveAs function on the control axCExpressViewerControl. You have to embed the control in a WinForms dialog. In my wrapper I disabled the UI (but you have to call the Dialog code aniway).

This is the code to use the AxCExpressViewerControl in a nice way:

public class DesignReviewPublisher : IDisposable
{
   private readonly AxExpressViewerDll.AxCExpressViewerControl _axCExpressViewerControl;

 public DesignReviewPublisher()
 {
   var form = new Form1();
   _axCExpressViewerControl = form.GetInitializedDesignReviewControl();
 }

 public bool Publish(string fromFile, string toFile)
 {
   try
   {
   _axCExpressViewerControl.SourcePath = fromFile;
   _axCExpressViewerControl.SaveAs(toFile);
   return System.IO.File.Exists(toFile);
   }
   catch (AccessViolationException)
   {
     return false;
   }
   catch (Exception)
   {
     return false;
   }
 }

 public void Dispose()
 {
   _axCExpressViewerControl.Dispose();
 }
}

It is derived from IDisposable, so it will dispose control when you dispose the DesignReviewPublisher. The AccessViolation catch block needs that you build your project with .Net 3.5. And because this uses the DesignReview application you need to build your assembly with x86.

Using this api is very nice. Here a C# example:

using (var designReview = new DesignReviewPublisher())
{
  designReview.Publish(@"C:\temp\test1.pdf", @"C:\test1.dwf");
  designReview.Publish(@"C:\temp\test2.png", @"C:\test2.dwf");
}

And this is for powerShell (you can use it also in a powerJobs script if you want):

[System.Reflection.Assembly]::LoadFile('YOUR_DIR\AxInterop.ExpressViewerDll.dll')
[System.Reflection.Assembly]::LoadFile('YOUR_DIR\Interop.ExpressViewerDll.dll')
[System.Reflection.Assembly]::LoadFile('YOUR_DIR\cODwfPublish.dll')
$dwfPublisher = New-Object cODwfPublish.DesignReviewPublisher
$dwfPublisher.Publish("C:\temp\test1.pdf","C:\temp\test1.dwf")
$dwfPublisher.Publish("C:\temp\test2.bmp","C:\temp\test2.dwf")
$dwfPublisher.Dispose()

You can download the assembly from this zip file: cODwfPublish

Have fun with my assembly, hope this helps :)

About weiss92

Software Developer
This entry was posted in powerJobs, PowerShell and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s