Quantcast
Channel: Microsoft Dynamics 365 Community
Viewing all articles
Browse latest Browse all 51446

Print any document (any extension)

$
0
0

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:

NameDataTypeSubtype
ProcessStartInfoDotNetSystem.Diagnostics.ProcessStartInfo.’System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
ProcessDotNetSystem.Diagnostics.Process.’System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
ProcessWindowStyleDotNetSystem.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!


Viewing all articles
Browse latest Browse all 51446

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>