«

»

Print this Post

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/

15 comments

2 pings

Skip to comment form

  1. Mark Brummel

    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…

    1. waldo

      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. Mark Brummel

    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”. 🙁

    1. waldo

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

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

  4. Bjørn Boeck

    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

  5. Edward Bloomfield

    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

    1. waldo

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

    2. Kuljeet

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

  6. Jim Brewer

    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.

    1. waldo

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

    2. Baran Deniz Gündüz

      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.

    3. Sundari Chilukuri

      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

    4. waldo

      Thanks, man! 😉

  7. Teming

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

  1. Print any document (any extension) - Waldo's Blog - Microsoft Dynamics NAV

    […] Continue reading » […]

  2. Print any document (any extension) | Pardaan.com

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

Leave a Reply

%d bloggers like this: