Sunday, January 14, 2018

Print the document to a specific printer

printer-2178754_1920

In one of my previous posts, I have discussed how to download and print a remote file. Here is the blog link

http://www.navisionworld.com/2017/09/how-to-print-remote-file-from-nav.html

The solution in the above post will print the file to the default printer and recently I was working on a modification where I have to print the file to a specific printer instead of the default printer and couple of readers of my blog post also asked how to do this, so I would like to share today the solution for it.

I was checking online to modify my old solution to print to a specific printer and few suggested to me to use the arguments property of the ProcessStartInfo and also the below MSDN example suggests the same, but unfortunately, I was not successful using that property.

https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.verbs(v=vs.110).aspx

image

To solve this issue I have found another solution which is to use the Print Document and in fact, I think this is an easy solution when compared to the other solution in my previous post.

Below is the function for it.

PROCEDURE PrintFile@1000000018(FilePath@1000000002 : Text[250]);

VAR

PrinterName@1240060002 : Text;

PrintDocument@1240060003 : DotNet "'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.System.Drawing.Printing.PrintDocument";

PrinterSettings@1240060004 : DotNet "'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.System.Drawing.Printing.PrinterSettings";

BEGIN

PrinterName := 'MF8200C Series';

PrintDocument := PrintDocument.PrintDocument;

PrintDocument.DocumentName := FilePath;

PrinterSettings := PrinterSettings.PrinterSettings;

PrinterSettings.PrinterName := PrinterName;

PrintDocument.PrinterSettings := PrinterSettings;

PrintDocument.Print;

MESSAGE('Complete');

END;

If you have any other tips or suggestions, please do share them in the comments below.
Share:

4 comments:

Arndt Loermann said...

Hey Suresh Kulla, thank you for your post. we try it out with txt files and pdf file, but the output is only a white paper with some black points in the header and footer area of the output.

any idea for solution or what we can check to print the files correctly ?

thank you in advance
Arndt Loermann

Suresh Kulla said...

Arndt,

I ran into same issue for some printers, I will review and let you know if i find a solution.

Thanks
Suresh

Anthony Paul said...

amazing !!! good work. keep it up.
find Best printer from here.

Tizer said...

Hi mate,
Your code for this help me a lot.

Can you tell me why I can't access my printer? Says "A call to System.Drawing.Printing.PrintDocument.Print failed with this message: Settings to access printer 'xxxxx' are not valid.

There is anything that you can tell me in order to fix it?