Print any document (any extension)

When reading Mark’s blog about printing PDF, it reminded me about a request I got not so long ago, which was quite similar: “I want to be able to easily print any document that is linked in my system, from NAV, … “.

But you know the challenge: if you want to print a PDF, then you have to have some kind of PDF printer (Bullzip, Adobe, whatever…). If you want to print a word document, you need word, or any kind of dll or software that can print the extension.

So I was looking for a generic solution, without any custom dlls, that always works, whenever, however, with any software, as long as you have “any” software installed that can print that “any”-document with any extension .. . And else, it should just give an error.. .

Now, you must have noticed .. if you right-click on a pdf, you get a menu, like this:


This means that the operating system knows it can print the document.

So – in a way – if the operating system know, NAV should be able to know as well .. .

Now, it didn’t take me long to figure out. It’s just quite easy in the .NET Framework. In fact, it seems that when you start a process, you can add “verbs” to the process to specify what you want to do .. . One of these verbs is “Print” :-). So it’s just a matter of starting a process of this file with the verb “print”. And if the “processstarter” finds an application that is associated to this extension, it will use that program to print :-).

Here is the code:


And the vars that I used:

Name DataType Subtype
ProcessStartInfo DotNet System.Diagnostics.ProcessStartInfo.’System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
Process DotNet System.Diagnostics.Process.’System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
ProcessWindowStyle DotNet System.Diagnostics.ProcessWindowStyle.’System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′

*** UPDATE ***

I forgot to mention that these DotNet-vars are supposed to be client-side, as the user should be able to use its default printer … besides, when doing this in a multi-user environment on a server .. just don’t think that’s a good idea 😉

*** END UPDATE ***

Needless to say, but I can simply call it like this:

PrintAnyDocument(‘c:\Temp\Any.pdf’);

And with the TryFunction (which I can safely use here because it’s a pure .Net try/catch), it gives me some nice errorcatching, like:


Enjoy!

5.00 avg. rating (98% score) - 3 votes

Permanent link to this article: http://www.waldo.be/2015/12/21/print-any-document-any-extension/

20 comments

2 pings

Skip to comment form

  1. Thanks, my dear NAV Twin. I will give this a try.

    Glad that after all these years my blog still inspires you.

    One of the challenges with printing PDF is the execution order. If you have a NAV Invoice and two PDFs, than an Invoice and another PDF you want to make sure the PDFs are in line with the invoice to go in the envelope.

    Printing PDF can give headaches, trust me.

    I will try this one. We are still in the process of upgrading to 2016 and January is testing month. No workshops for me. Enjoy x-mas.

    Do you have an out-of-the box .net solution for combining multiple PDF into one? Thats on my to-do list… ouch…

      • waldo on December 21, 2015 at 8:36 am
        Author

      I can tell you this solution works quite well for me ;-).
      For concatenating PDF’s, I guess there is no way around, and you’ll have to use some kind of third party tool. We have been using Amyuni way back – not saying this is THE tool to use, just saying we used it a long time ago when we needed ..

  2. Couple of things come into mind

    1. Where does it print, on the server or on the client
    2. Which tool does it use? The default windows tool?
    3. Does it leave the PDF open after printing?
    4. What if the keyword “Print” is different in another language? Are you going to translate into all possible languages?
    5. When using Adobe, my experience is that it reopens the reader each time, Foxit is smarter than that
    6. Windows Terminal Server/Citrix does not have a PDF reader by default, Only Windows 10 & Windows Server 2016 which is in beta
    7. Which printer does it use, which paper drawer.

    Managing PDF printing is not “easy”. 🙁

      • waldo on December 21, 2015 at 10:18 am
        Author

      As I updated the post – it’s a client-side DotNet, so it should print client-side. That’s how I want it to be. Which tool it uses, doesn’t matter: it’s going to print on the default printer. Default printer means: if necessary, you can first set it up, and then start the print job (like a loop, with multiple document types, like a few .docx, and a few pdf’s ;-)). It will not leave anything open.
      The verb ‘print’ is nog going to be different in different languages.. . It’s like a statement.
      Windows server doesn’t have a pdf printer by default, indeed .. you should something. No problems with Bullzip, Adobe, .. . Or at least none experienced ;-).

      I hate using, deploying, maintaining, … dll’s. If it can be avoided, then I avoid it. Simple. If I would only have to maintain a few customers, sure, no problem, but imagine you have to maintain 300 customers – which means about 3000 users. If I can avoid taking care of client-side dll’s .. then I do ..

  3. great post using the processinfo you could also open any exe file

  4. I had a requirement to print to specific printer based on the Printer Selection in NAV.
    The code below worked for me, hope it can be of use for others.

    The PrinterName paramenter contains the value from PrinterSelection.”Printer Name”

    //Method Name
    PrintAnyDocToSpecificPrinter(FullPath, PrinterName)

    //Method Start
    ProcessStartInfo := ProcessStartInfo.ProcessStartInfo(FullPath);

    WITH ProcessStartInfo DO BEGIN
    Arguments := ‘”‘ + PrinterName + ‘”‘;
    Verb := ‘PrintTo’;
    CreateNoWindow := TRUE;
    WindowStyle := ProcessWindowStyle.Hidden;
    UseShellExecute := TRUE;
    END;

    Process.Start(ProcessStartInfo);
    //Method End

      • Matt on August 27, 2019 at 10:09 pm

      I came to the same solution, wish I had seen this instead of wasting my time googling it myself haha.

    • Edward Bloomfield on March 20, 2017 at 12:45 pm
    • Reply

    Hi

    Thanks for this. I have given this a go however I keep getting the error that this will not print without a helpful error message. When I run it the first time it errors but with an unrelated error:

    ‘Not able to print C:\folder\printthis.txt.

    ERROR: [Some unrelated error elsewhere in NAV from something else I was doing previously like date filters]’

    From then on I keep getting the error

    ‘Not able to print C:\folder\printthis.txt.

    ERROR: Not able to print C:\folder\printthis.txt.

    ERROR: [Some unrelated error elsewhere in NAV from something else I was doing previously like date filters]’

    The unable to print error keeps getting added each time but does not provide a hint why.

    And the error keeps growing. Any tips on why the file won’t print and how to avoid the accumulating error message?

    Many thanks in advance
    Edward

      • waldo on May 19, 2017 at 12:40 pm
        Author

      No clue at all – but are you sure you put your vars client-side?

      • Kuljeet on November 28, 2017 at 6:01 pm

      Hi Edward would you be able to resolve this error, because I am also getting the same error.

    • Jim Brewer on May 17, 2017 at 11:39 pm
    • Reply

    Unfortunately, when I go to look at the .NET Assemblies, I only have 3 that start with System.Diagnostics, and none of them appear to have what I need. I’ve got System.Diagnostics.Contracts, System.Diagnostics.Debug, and System.Diagnostics.Tools. Am I just out of luck here? This is a NAV 2016 environment I’m in. Thanks.

      • waldo on May 19, 2017 at 12:22 pm
        Author

      Well,
      I think you’re using the “mscorlib” library?
      Try to use the “System” library. You’ll find it there!

      • Baran Deniz Gündüz on July 7, 2017 at 10:19 am

      Hello, can you help me to add these .NET Assemblies to NAV? as Jim Brewer mentioned above, i can not find those assemblies that are used. You said “Try to use the “System” library. but i dont know how to use system library instead of mscorlib.

      • Sundari Chilukuri on August 3, 2017 at 6:46 pm

      Baran,
      If you are still waiting for response, check out this link to solve your issue of not finding the .NET assembly:
      https://forum.mibuso.com/discussion/68354/missing-system-diagnostics-assembly
      -Sundari

      • waldo on August 3, 2017 at 8:05 pm
        Author

      Thanks, man! 😉

    • Teming on June 26, 2017 at 3:34 pm
    • Reply

    Do you have any suggestion for the Server-side printing? Thanks very much.

      • Matt on August 27, 2019 at 10:08 pm

      Do the same thing, but don’t run the dotnet client side. Also, make sure the server has something registered to handle PDF files.

  5. Hi, I’ve received the Error message that you showed in the last picture. How can I fix it?

      • waldo on August 5, 2019 at 9:53 am
        Author

      You need to install software that can print pdf files…

    • Denis on August 27, 2019 at 9:39 am
    • Reply

    Hi,can you help me? i didn’t receive error message but didn’t print anything. The printer queue is empty and there aren’t active processes.

  1. […] Continue reading » […]

  2. […] Bron : Waldo’s Blog Lees meer… […]

Leave a Reply to pgiacome Cancel reply

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

%d bloggers like this: