How to print RDLC report to PDF on stationery paper (2)

22 Mar

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.

This other PDF library is called PDFsharp and according to their licensing model it is free to use, even for commercial products.

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:

SNAGHTML15784643

 

And the code:

SNAGHTML1578bcac

 

Download the sourcecode here: Codeunit 50001 Test PDF Report Background 2

6 thoughts on “How to print RDLC report to PDF on stationery paper (2)

  1. Hi Arend Jan

    Thanks for this post. I had to crop a PDF for a label printer, and your example was great for inspiration & pointers.

    The code to crop a PDF using PDFSharp:

    CropServerPDF(OldServerFilename : Text;NewServerFilename : Text)

    PdfDocument := PdfReader.Open(OldServerFilename);

    XOffsetMM := 26;
    YOffsetMM := 12;
    HeightMM := 210;
    WidthMM := 100;

    PdfXRect := PdfXRect.XRect(XOffsetMM/25.4*72,-YOffsetMM/25.4*72,WidthMM/25.4*72,HeightMM/25.4*72);
    PdfRectangle := PdfRectangle.PdfRectangle(PdfXRect);

    FOR i := 0 TO PdfDocument.PageCount – 1 DO
    BEGIN
    PdfPage := PdfDocument.Pages.Item(i);
    PdfPage.CropBox(PdfRectangle);
    END;

    PdfDocument.Save(NewServerFilename);

  2. Hi Arend Jan,

    One of our developers tested the solution (NAV 2013 – build 35782) you posted but we ran into this error:
    “Cannot handle iref streams. The current implementation of PDFsharp cannot handle this PDF feature introduced with Acrobat 6.”

    After some research, it seems like PDFSharp is not able to work with the PDF’s created by NAV 2013.
    http://forum.pdfsharp.net/viewtopic.php?f=2&t=693 (this problem is only known since 2009, but no updates / support for this issue from PDFSharp)

    Didn’t you ran into this problem?

  3. Pingback: How to print RDLC report to PDF on stationery paper | Microsoft Dynamics NAV Thoughts

  4. Hi Arend Jan,

    It still works great in NAV2013R2, even though no new updates are found of this assembly. PDFSharp mentions a beta with version 1.50, but nothing has been found so far.

  5. Hi Arend

    I’m trying to use part of this approach for split pdf files but when I try to open the pdf I got this message
    Microsoft Dynamics NAV

    A call to PdfSharp.Pdf.IO.PdfReader.Open failed with this message: Request for the permission of type ‘System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.

    I’m using NAV 2015 and Win 2012.

    Thanks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.