One of the improvements with RDLC reporting in Microsoft Dynamics NAV is the possibility to save a report directly to PDF instead of using a so called PDF printer driver. This can be done by calling the REPORT.SAVEASPDF function (see here on MSDN).
This is very useful when you want to email documents as attachment, for example electronic invoicing.
Many companies are using stationery or pre-printed paper with their logo and address details. I don’t know what your experience is with this, but I get the question on a regular basis if it is possible to create the PDF exactly the same as it would look like when printed on stationery paper. One solution could be to create two versions of a report so that you can place all the graphics in the background of the RDLC. Or, if you want to be a little bit smarter, you can try to dynamically print or hide these graphics. Or you can choose to use a PDF printer driver that can add backgrounds during the printing process. An example can be found here: http://www.bullzip.com/products/pdf/doc/examples/dynamics-nav-invoices/info.php
But whatever solution you choose, there are always disadvantages:
- Creating two versions of reports cost a serious amount of time, and upgrades are more expensive.
- Mixing the RDLC with dynamics background graphics is a complex task and has to be performed on any report that must be printed on stationery paper. And how do you know in C/AL code or RDLC layout that the report is printing to PDF instead of a real printer? There is no C/AL code option like CurrReport.PREVIEW, for example CurrReport.PRINTTARGET = PDF or something like that.
- Using an external printer driver is an opportunity, but you have to install it on any client computer that print reports. We have been using it in the past, and it is not a ‘set and forget’ installation.
So what about another option that works on any report, without modifying the report object or installing any external drivers on the client computer?
Here it is…
PDF files are not locked after they have been created. There are possibilities to manipulate PDF files, for example add text, graphics, stamps or background to the pages. So my solution is, in one sentence, open the PDF file afterwards and add a stationery background to each page.
How can this be achieved in Dynamics NAV? The answer is: with .Net interoperability and a good PDF library. A library that does this job in an excellent way is called iTextSharp and can be found here: http://sourceforge.net/projects/itextsharp/ (But read also the next post about an alternative because of license restrictions.)
Here is an example how to use this library and add a background to the Sales Invoice report. Of course this example can be extended for generic use for different reports.
First of all you have to install the iTextSharp library. That is pretty straightforward. Download the files of from the link above on Sourceforge. In the downloaded zip file, there is a file itextsharp-dll-core.zip. Unzip this one and copy the file itextsharp.dll to the Add-ins folder of your Dynamics NAV server. Also copy this file to the Add-ins folder of your Development Environment (or Classic Client if you want to)
Now we create a Codeunit with the following variables:
And here is the code that generates the PDF and adds a background to each page:
Download this Codeunit here: Codeunit 50000 Test PDF Report Background
Imagine what other possibilities you have with this. To mention a few:
- Concatenate PDF files, e.g. add delivery terms to an invoice.
- Add images or stamps
- Create a PDF form with fill-out fields
Hope you enjoy it!