How to send a document to a printer and print it?

Here's the basic assumption:

My user clicks on some of the gizmos and the PDF file spits out on his desktop. Is there a way to queue this file to the printer and print it to a locally connected printer?

string filePath = "filepathisalreadysethere"; SendToPrinter(filePath); //Something like this? 

He will do this process many times. For each student in the class, he must print a small report card. Therefore, I create a PDF file for each student, and I would like to automate the printing process instead of creating PDF files, printing, creating PDF files, printing, creating PDF files, printing.

Any suggestions on how to approach this? I work on Windows XP with Windows Forms.NET 4.

I have found this https://stackoverflow.com/a/4646262/2124128 of a question that offers an accepted answer:

Once you have created your files, you can print them through the command line (you can use the Command class found in the System.Diagnostics namespace for that)

How to do it?

+47
c # printing pdf winforms
May 23 '11 at 22:22
source share
9 answers

You can tell Acrobat Reader to print the file using (as mentioned here) the verb 'print'. After that, you will also need to close the Acrobat Reader program:

 private void SendToPrinter() { ProcessStartInfo info = new ProcessStartInfo(); info.Verb = "print"; info.FileName = @"c:\output.pdf"; info.CreateNoWindow = true; info.WindowStyle = ProcessWindowStyle.Hidden; Process p = new Process(); p.StartInfo = info; p.Start(); p.WaitForInputIdle(); System.Threading.Thread.Sleep(3000); if (false == p.CloseMainWindow()) p.Kill(); } 

This opens Acrobat Reader and tells it to send the PDF to the default printer, and then shuts down Acrobat after three seconds.

If you are ready to send other products with your application, you can use GhostScript (free) or a printer on the PDF command line, for example http://www.commandlinepdf.com/ (commercial).

Note. the sample code opens a PDF file in an application registered to print PDF files, which is Adobe Acrobat Reader for most computers. However, it is possible that they are using another PDF viewer, such as Foxit ( http://www.foxitsoftware.com/pdf/reader/ ). The sample code should still work.

+51
May 24 '11 at 5:40 a.m.
source share
โ€” -

I know the tag says Windows Forms ... but if someone is interested in the WPF application method, System.Printing works like a charm.

 var file = File.ReadAllBytes(pdfFilePath); var printQueue = LocalPrintServer.GetDefaultPrintQueue(); using (var job = printQueue.AddJob()) using (var stream = job.JobStream) { stream.Write(file, 0, file.Length); } 

Remember to include the System.Printing link if it is not already included. Now this method does not work well with ASP.NET or Windows Service . It should not be used with Windows Forms since it has System.Drawing.Printing . I have no problem with my PDF printing using the code above.

However, I should mention that if your printer does not support Direct Print for PDF, you are out of luck with this method.

+24
Dec 11 '14 at 18:01
source share

Adding a new answer to this question, since the question of printing PDF in .net has been around for a long time, and most of the answers come before the Google Pdfium library, which now has a .net wrapper. For me, I myself studied this problem and tried to make hacked decisions, such as spawning Acrobat or other PDF readers, or working in commercial libraries that are expensive and do not have very compatible licensing conditions. But the Google Pdfium library and the PdfiumViewer.net wrapper are Open Source, so this is a great solution for many developers, including me. PdfiumViewer is licensed under the Apache 2.0 license.

Here you can get the NuGet package:

https://www.nuget.org/packages/PdfiumViewer/

and here you can find the source code:

https://github.com/pvginkel/PdfiumViewer

Here is a simple code that will quietly print any number of copies of a PDF file from it filename. You can also download PDFs from a stream (as is usually done), and you can easily understand this by looking at the code or examples. There is also a PDF representation of WinForm so you can also display PDF files in a view or preview them. For us, I just needed the ability to quietly print a PDF file to a specific printer on demand.

 public bool PrintPDF( string printer, string paperName, string filename, int copies) { try { // Create the printer settings for our printer var printerSettings = new PrinterSettings { PrinterName = printer, Copies = (short)copies, }; // Create our page settings for the paper size selected var pageSettings = new PageSettings(printerSettings) { Margins = new Margins(0, 0, 0, 0), }; foreach (PaperSize paperSize in printerSettings.PaperSizes) { if (paperSize.PaperName == paperName) { pageSettings.PaperSize = paperSize; break; } } // Now print the PDF document using (var document = PdfDocument.Load(filename)) { using (var printDocument = document.CreatePrintDocument()) { printDocument.PrinterSettings = printerSettings; printDocument.DefaultPageSettings = pageSettings; printDocument.PrintController = new StandardPrintController(); printDocument.Print(); } } return true; } catch { return false; } } 
+20
Jan 19 '17 at 20:34 on
source share

This is a slightly modified solution. The process will be killed if it is idle for at least 1 second. Perhaps you should add time in X seconds and call a function from a separate thread.

 private void SendToPrinter() { ProcessStartInfo info = new ProcessStartInfo(); info.Verb = "print"; info.FileName = @"c:\output.pdf"; info.CreateNoWindow = true; info.WindowStyle = ProcessWindowStyle.Hidden; Process p = new Process(); p.StartInfo = info; p.Start(); long ticks = -1; while (ticks != p.TotalProcessorTime.Ticks) { ticks = p.TotalProcessorTime.Ticks; Thread.Sleep(1000); } if (false == p.CloseMainWindow()) p.Kill(); } 
+9
Sep 12 '14 at 11:09
source share

this is a late answer, but you can also use the File.Copy method in the System.IO namespace to send the file to the printer:

 System.IO.File.Copy(filename, printerName); 

It works great

+4
Oct. 21 '15 at 13:11
source share

System.Diagnostics.Process.Start can be used to print a document. Set UseShellExecute to True and set Verb to "print".

+3
May 23 '11 at 22:29
source share

You can try with GhostScript, as in this post:

How to print PDF to a network printer by default using the GhostScript shell command (gswin32c.exe)

+2
May 23 '11 at 23:00
source share

I know that Edwin answered this above, but his only one prints one document. I use this code to print all files from a given directory.

 public void PrintAllFiles() { System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(); info.Verb = "print"; System.Diagnostics.Process p = new System.Diagnostics.Process(); //Load Files in Selected Folder string[] allFiles = System.IO.Directory.GetFiles(Directory); foreach (string file in allFiles) { info.FileName = @file; info.CreateNoWindow = true; info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.StartInfo = info; p.Start(); } //p.Kill(); Can Create A Kill Statement Here... but I found I don't need one MessageBox.Show("Print Complete"); } 

It purely cycles through each file in a given Directory variable โ†’ for me it was @ "C: \ Users \ Owner \ Documents \ SalesVaultTesting \" and prints these files on your printer by default .

+2
Jun 01 '16 at 2:06
source share

A simple way:

 var pi=new ProcessStartInfo("C:\file.docx"); pi.UseShellExecute = true; pi.Verb = "print"; var process = System.Diagnostics.Process.Start(pi); 
0
Dec 26 '14 at 15:33
source share



All Articles