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′ |
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!