In my previous post I demonstrated an option to add a background to a PDF file after it has been created. The external library iTextSharp that I used in this example does the job in an excellent way. However, there is one disadvantage that I noticed afterwards. The software from iTextSharp is published under the GNU AFFERO GENERAL PUBLIC LICENSE (AGPL). According to the explanation of this license on this page you have to buy a commercial license if you do not disclose the source code of your own application. Since pricing of iText is a serious amount of money, I decided to try another PDF library that is really free of use.
So why did I not blog about this library in my first post? The answer is quite simple, when I compared these two products I didn’t pay attention to the differences of the public license. And the latest release date if iTextSharp was Febrary 14th, 2014 while PDFsharp was published back in 2012. So I thought that iTextSharp was more alive, and besides that it appeared that they had a more simple way of adding a background to pages.
However, after investigating the possibilities with PDFsharp it turned out that it wasn’t that difficult at all. So I decided to post a second example with this free to use library. It is up to you to choose between open license or commercial license.
And of course this also proves the flexibility we have today with Microsoft Dynamics NAV to utilize .Net and offer functionalities to our customers that we couldn’t dream of in the past!
Back to PDFsharp…
The library can be downloaded here: http://sourceforge.net/projects/pdfsharp/files/. Browse through the files to get the latest version.
There are two packages available (currently version 1.32 is the latest version, but you should always check if a newer version is available):
PDFSharp-MigraDocFoundation-1_32.zip: the complete source code
PDFsharp-MigraDocFoundation-Assemblies-1_32.zip: the compiled assemblies
I recommend to download the compiled assemblies because that will give you a jump start. Here is a direct download link: http://sourceforge.net/projects/pdfsharp/files/pdfsharp/PDFsharp%201.32/PDFsharp-MigraDocFoundation-Assemblies-1_32.zip/download
Unzip the file, this will give you a couple of dll files in two folders: GDI+ and WPF. Copy the PdfSharp.dll file from the GDI+ folder to your Add-ins folder of the Dynamics NAV server and Development Environment.
And here is the example from the previous page, modified to use PDFsharp.
The variables we need:
And the code:
Download the sourcecode here: Codeunit 50001 Test PDF Report Background 2