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.

6 Responses to Convert PDF to DWF in .Net

  1. Radhakrishna says:

    Class not registered(REGDB_E_CLASSNOTREG)) …when trying to use in this .net code … please help us

    • weiss92 says:

      I gave a quick try on how to get this approach running for current version of Autodesk Design Review
      first of all, make sure to launch Design Review on your environment, in order to register it on your machine correctly.

      next steps work best when VS is installed on the same environment where Autodesk Design Review is running:
      – open VS Dev console and navigate to Open a console and browse to C:\Program Files (x86)\Common Files\Autodesk Shared\DWF Common\
      – Run aximp.exe AdView.dll. 3 files are generated: AxExpressViewerDll.dll, ExpressViewerDll.dll, stdole.dll
      aximp.exe should be located e.g. in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools
      – add the generated assemblies as references to your VS project
      – create a form (e.g. called Form1 and initialize the DesignReview control with new AxExpressViewerDll.AxCExpressViewerControl())
      – reuse the control in DesignReviewPublisher class from the previous post

      I assume that should work in order to run this approach on current envionments.
      Let me know if that helped and if you got it running

  2. Radhakrishna says:

    I would like to try the same when vs instance is not installed (limitation in our environment as we run the console as admin) ..basically we are looking to develop a library which converts a dwf file to pdf file using API on the fly that is wrapped on top of library (code snippet shared by you).

  3. Radhakrishna says:

    Just to clarify ….
    I need to copy the aximp.exe from the loc :C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools to C:\Program Files (x86)\Common Files\Autodesk Shared\DWF Common\ ,which will generate the DLL’s and after that it needs to be referenced in VS.net projects.
    Question i have :
    1) Above code snippet shared by you should be able to convert DWF to PDF and vice versa.
    2) Production environment readiness of above shared code by you should not have constraint to regenerate the DLL again, provided that same version of Autodesk Design Review in both lower environments and Production.

    can you please help us to generate the DLL’s without VS.net installation and we need to higher admin rights to execute the command -> Run aximp.exe AdView.dll.

  4. weiss92 says:

    good afternoon

    you don’t need to copy aximp.exe from microsoft sdk’s directory! you can just launch it from that directory if the executable get’s not automatically found when opening a VS Development Console

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s