PDFTron PDFNet for .Net Now Available From Nuget.org

Recently we added PDFNet to the Nuget gallery. For our .Net users this is a great new way to keep up on the latest PDFNet releases.

www.nuget.org/packages/PDFNet/

This package includes all of our .Net releases, for .Net 2.0 – 3.5 and .Net 4.0 +, and the corresponding 32 and 64 bit assemblies. When you install the package it of course picks the appropriate version and sets everything up for you.

A common pain that our .Net users have had, was using both our 32 and 64 bit assemblies in an AnyCPU project. While an installer would normally take care of this, it was still frustrating for developers. To address this issue, our Nuget package includes a new assembly, PDFNetLoader, that at runtime will pick the correct version of PDFNet. The source code for the loader is available on GitHub, and the latest binaries here.

If you install the nuget package, then all of this is setup for you, and before calling any PDFNet methods, simply insert the following line of code, in the main class of your project.

private static pdftron.PDFNetLoader loader = pdftron.PDFNetLoader.Instance();

You can also change the load path for the PDFNet assemblies, by appending the code above with

Path("path_to_pdfnet_folder_containing_32_and_64_bit_PDFNet_libs")

For example

private static pdftron.PDFNetLoader loader = pdftron.PDFNetLoader.Instance().Path(@"..\res");

Technical Details

On the other hand, if you are already using PDFNet, or don’t want to use the Nuget package, here are the manual steps to setup your project to use PDFNet and AnyCPU.

First let us assume your output dir (where your exe is created) is ‘bin’.

  1. Create a folder in ‘bin’ called ‘PDFNet’
  2. In ‘PDFNet’ create two folders, one called ‘x64’, the other ‘x86’
  3. Place the PDFNet.dll for x86 and x64 in the respective folders above.
  4. Download the latest PDFNetLoader.dll : github.com/PDFTron/PDFNetLoader/releases
  5. In your Visual Studio project add PDFNetLoader.dll as a reference.
  6. In your Visual Studio project add bin/PDFNet/x86/PDFNet.dll as a reference.
  7. Right click the PDFNet.dll (not PDFNetLoader.dll) in your references, and select Properties.
  8. Change the “Copy Local” property to “False”
  9. Somewhere in your code, as early as possible, and before any PDFNet methods are called, add the following line.
private static pdftron.PDFNetLoader loader = pdftron.PDFNetLoader.Instance();
You are now ready to run your project in AnyCPU.

The project references to PDFNet and PDFNetLoader allow you to code normally. At runtime though, the following will happen.

  1. The PDFNetLoader.Instance() method is triggered, which registers itself as an event handler for AppDomain.AssemblyResolve.
  2. JIT encounters a method that uses PDFNet and tries to load PDFNet.dll, but because of the “local copy:false” this fails.
  3. PDFNetLoader gets the AssemblyResolve event, detects if system is 32 or 64 bit, and loads the corresponding PDFNet.dll.

We hope our .Net users find this useful, and as always you can ask questions in PDFNet Forum on Stackoverflow.

2 thoughts on “PDFTron PDFNet for .Net Now Available From Nuget.org

  1. gc

    I’ve followed the instructions on this page, but the loader does not load the proper lib. Why should I need to specify a path? (I’ve tried that.) That’s like saying:

    “Install this.”
    “Ok, done. I put it in the blue box.”
    “Great, now load it.”
    “I can’t find it.”
    “… imbecile”

    Reply
  2. Ryan Post author

    This blog post goes into a lot more detail about what purpose PDFNetLoader serves, how it works, and when you (don’t) need it.
    https://blog.pdftron.com/2014/12/01/pdfnet-for-net-and-anycpu/

    PDFTron also provides full pre-sale support, so please feel free to send the full error message to support at pdftron.com.

    Also, note you can switch from AnyCPU to x86 or x64 in Visual Studio Configuration Manager, and explicitly pick the corresponding PDFNet.dll, making sure the property CopyLocal=true, and then PDFNetLoader is never used.

    As for your question, PDFNetLoader is there so that at runtime your project can load 32 or 64 bit PDFNet, to do this .Net itself needs to fail to find PDFNet.dll (from normal assembly loading locations), and asks PDFNetLoader, which knows the non-standard path.

    Again, PDFNetLoader is not required, if you know you are running on 64 or 32bit OS, or see the other blog post.

    Reply

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