Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print various file types programmatically

Tags:

c#

printing

pdf

I am writing an app which performs some tests and produces a number of different reports. These may be any combination of Labels, PDF for end customer, PDF for repair department, XML file etc.

Depending on the report type, I need to send the file to either the file system or to one of a number of different printers (A4, label etc). Ideally there should be no popup windows - just straight to paper.

How can I send a file (PDF, XML) to a printer? I had thought that for XML/Text I could just File.Copy to LPTn but that doesn't seem to work. For PDF I'm guessing that I could call Acrobat with some parameters which cause the PDF to be printed.

The printers I use are mapped to LPTn. Is there a better way to do this and to store the definitions in the app? i.e. Labels go to MyLabelPrinter and A4 PDFs got to MyA4Printer.

Has anyone done this?

like image 712
paul Avatar asked Dec 20 '22 17:12

paul


2 Answers

ProcessStartInfo info = new ProcessStartInfo("[path to your file]");
info.Verb = "PrintTo";
info.Arguments = "\"[printer name]\"";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
like image 198
Michal Klouda Avatar answered Dec 30 '22 11:12

Michal Klouda


Take a look at this webpage. You should find the info you are looking at for PDF. For example, it will look like that:

    ProcessStartInfo infoOnProcess = new ProcessStartInfo("C:/example.pdf");
    info.Verb = "PrintTo";
    //Put a if there, if you want to change printer depending to file extension
    info.Arguments = "\"HP-example-Printer\"";
    info.CreateNoWindow = true;
    info.WindowStyle = ProcessWindowStyle.Hidden;
    Process.Start(infoOnProcess);
like image 39
TrizZz Avatar answered Dec 30 '22 11:12

TrizZz