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.

Advertisements
This entry was posted in Dynamics NAV. Bookmark the permalink.

16 Responses to Print Existing PDF in Dynamics NAV using DotNET

  1. Pingback: Print Existing PDF in Dynamics NAV using DotNET - Mark Brummel - Author of Microsoft Dynamics NAV 2013 Application Design

  2. 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

  3. D says:

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

    Like

  4. D says:

    Ah and for combining the PDF: use PDFtk. I’ve written a .NET wrapper for it. It merges, splits, dumps & adds watermarks and whatnot.

    Like

  5. 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

  6. Pingback: Print any document (any extension) » waldo's blog

  7. Pingback: Print any document (any extension) - Microsoft Dynamics NAV Community

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. Markus says:

    Hi,

    I receive allways an error:

    A call to system.Drawing.Printing.PrintDocument.Print failed this message: Settings to access printer ‘Testprinter’ are not valid.

    PdfDocument := PdfDocument.PdfDocument;
    PdfDocument.LoadFromFile(‘D:\TestDocument.pdf’);
    PdfDocument.PrinterName := ‘Testprinter’;
    PdfDocument.PrintDocument.PrinterSettings.Copies := 2;
    PdfDocument.PrintDocument.Print;
    PdfDocument.Dispose;

    Has anybody an idea?
    I try it with spire.net free spire.net and the dotnet versions 3.5, 4 and 4-client.

    Like

Leave a Reply

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 )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s