EDIT: Read about downloading NuGet packages here.
As you might have noticed in my blog post from a couple of days ago I am making an attempt to print an existing PDF from NAV.
This is a common request. For example: print assembly instructions together with a sales packaging document. The assembly instructions are an existing PDF document and the sales document is generated by NAV.
In the classic client I used a command prompt tool for this, but I figured this would be possible using a DotNET component in newer versions such as NAV 2016.
Finding a tool
Step one is to find a tool that lets you do this. I searched in NUGET packages to see if something existed. I decided to try one I found, Spire.PDF. I guess there are more tools, maybe better ones. If you have a better one, please tell me.
Step two is to install the DLL. This is explained in the previous blog.
Writing the C/AL
Last part is to write the C/AL DotNET code that prints the PDF. This starts by finding the constructor.
The easiest way is to find some C# code to copy. This is what I found on StackOverflow.
PdfDocument pdfdocument = new PdfDocument(); pdfdocument.LoadFromFile(pdfPathAndFileName); pdfdocument.PrinterName = "My Printer"; pdfdocument.PrintDocument.PrinterSettings.Copies = 2; pdfdocument.PrintDocument.Print(); pdfdocument.Dispose();
PDFDocument is the constructor, so I need that as my DotNET variable.
After this, I can almost copy and past the C# code to Dynamics NAV
OnRun() PDFDocument := PDFDocument.PdfDocument; PDFDocument.LoadFromFile('E:\untitled.pdf'); PDFDocument.PrinterName := 'HP LaserJet CP 1025nw'; PDFDocument.PrintDocument.PrinterSettings.Copies := 2; PDFDocument.PrintDocument.Print; PDFDocument.Dispose;
The only things I changed was the filename, printername. I replaced = with := and removed the parenthesis.
This works like a charm.
Next challenge is to write some code that combines multiple PDF documents into one big PDF, but that’s another blog for another day.