Print Existing PDF in Dynamics NAV using DotNET

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.

Installing

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.

PrintPDFConstructor

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.

9 Comments

  1. We currently do that using the Windows Process API with DotNet vars, but we still have a problem with Adobe Reader, that remains open after the file is sent to the printer (works fine with Foxit Reader).

    Maybe I will try Spire.NET someday, thanks for the info!

    Like

  2. D says:

    Or even better: Use SumatraPDF. No add-ins, just an external Program with a lot of handy commandline switches.

    Like

  3. Rob Hansen says:

    Keep in mind there are add-ons to do this including combining multiple PDFs and emailing them. Not trying to flaunt our wares, but if something will take a lot of time to custom develop, see what’s already out there for NAV as a comparison. 🙂

    Like

  4. Yair K. says:

    We had a similar requirement**, and took a different path. Our solution was to use Google’s pdfium library, render the entire pdf to image, and embed the resulting image in the SSRS reports. That’s because the reports could be sent to various destinations (printer, email, word or even shown to screen and then sent to printer) and we wanted the instructions to be available everywhere.

    ** On Dynamics AX not NAV, but there’s little specific to AX there – NET interop was used to render the image, and SSRS reporting is common to both platforms.

    Like

  5. aceX says:

    Hi there,

    I’d like to say thanks for this post, and to write something for future readers (to help them). Spire.PDF is NuGet that you don’t need to install via Visual Studio or PowerShell. All you need is to download from internet and copy Spire.PDF.dll and Spire.Licence.dll in C:\Program Files (x86)\Microsoft Dynamics NAV\90\RoleTailored Client\Add-ins and also in C:\Program Files\Microsoft Dynamics NAV\90\Service\Add-ins
    I’ve lost more than hour until figure it 🙂

    Regards

    Like

  6. Arne Damkjer says:

    I found BioPDF http://www.biopdf.com (free version as BullzipPDF – http://www.bullzip.com ) handy. It not only contains a .NET version but also an automation for older NAV versions.

    Liked by 1 person

  7. Selma Maria Babu says:

    Hi
    i am using spire pdf in nav for pdf editor; i facing an error please help me.

    this is my code:

    PDFDocument := PDFDocument.PdfDocument;
    PDFDocument.LoadFromFile(FileNameReport);
    PDFPages := PDFDocument.Pages;

    PDFPage := PDFPages.Item(0);
    PDFCanvas := PDFPage.Canvas;

    PDFGraphicsstate := PDFCanvas.Save;
    PDFPen := PDFPens.Black;

    PDFFontFamiliy := PDFFontFamiliy.Courier;
    PDFFontObj := PDFFontObj.PdfFont(PDFFontFamiliy,11);//##

    PDFRGBCOlor :=PDFRGBCOlor.PdfRGBColor(10);
    PDFBrush := PDFBrush.PdfSolidBrush(PDFRGBCOlor);
    RofText := STRSUBSTNO(‘%1 %2 %3’,PurchInvHeader.”No.”,PurchInvHeader.”Vendor Invoice No.”,PurchInvHeader.”Avis No.”);
    PDFCanvas.DrawString(RofText,PDFFontObj,PDFPen,70,30);
    PDFCanvas.Restore(PDFGraphicsstate);

    PDFDocBase.Save(outputfile);
    PDFDocBase.Close(TRUE);

    The error is ……………………….

    Microsoft Dynamics NAV
    —————————

    This message is for C/AL programmers: A call to Spire.Pdf.Graphics.PdfFont failed with this message: Object reference not set to an instance of an object.
    —————————
    OK

    Like

    1. selmababu says:

      Hi

      PDFFontObj := PDFFontObj.PdfFont(PDFFontFamiliy,11);

      error in this line . PDFfont …

      Like

  8. selmababu says:

    Hi

    i am using spire Pdf to create pdf doc in Nav

    my code is

    PDFDocument := PDFDocument.PdfDocument;
    PDFDocument.LoadFromFile(FileNameReport);
    PDFPages := PDFDocument.Pages;

    PDFPage := PDFPages.Item(0);
    PDFCanvas := PDFPage.Canvas;

    PDFGraphicsstate := PDFCanvas.Save;
    PDFPen := PDFPens.Black;

    PDFFontFamiliy := PDFFontFamiliy.Courier;
    PDFFontObj := PDFFontObj.PdfFont(PDFFontFamiliy,11);//##

    PDFRGBCOlor :=PDFRGBCOlor.PdfRGBColor(10);
    PDFBrush := PDFBrush.PdfSolidBrush(PDFRGBCOlor);
    RofText := STRSUBSTNO(‘%1 %2 %3’,PurchInvHeader.”No.”,PurchInvHeader.”Vendor Invoice No.”,PurchInvHeader.”Avis No.”);
    PDFCanvas.DrawString(RofText,PDFFontObj,PDFPen,70,30);
    PDFCanvas.Restore(PDFGraphicsstate);

    PDFDocBase.Save(outputfile);
    PDFDocBase.Close(TRUE);

    when i run this code i am facing an error ……..

    Microsoft Dynamics NAV
    —————————

    This message is for C/AL programmers: A call to Spire.Pdf.Graphics.PdfFont failed with this message: Object reference not set to an instance of an object.
    —————————
    OK

    Please help me to fix this error…..

    Like

Leave a Comment

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 )

Connecting to %s

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