How to print a PDF with C#

Viewed 82323

I´ve trying to solve this problem for nearly 2 days. There are a lot of more or fewer good solutions on the net, but not a single one fits my task perfectly.

Task:

  • Print a PDF programmatically
  • Do it with a fixed printer
  • Don´t let the user do more than one Button_Click
  • Do it silent - the more, the better
  • Do it client side

First Solutions:

Do it with a Forms.WebBrowser

If we have Adobe Reader installed, there is a plugin to show PDF´s in the webbrowser. With this solution we have a nice preview and with webbrowserControlName.Print() we can trigger the control to print its content.

Problem - we still have a PrintDialog.


Start the AcroRd32.exe with start arguments

The following CMD command let us use Adobe Reader to print our PDF.

InsertPathTo..\AcroRd32.exe /t "C:\sample.pdf" "\printerNetwork\printerName"

Problems - we need the absolute path to AcroRd32.exe | there is an Adobe Reader Window opening and it has to be opened until the print task is ready.


Use windows presets

Process process = new Process();

process.StartInfo.FileName = pathToPdf; 
process.StartInfo.Verb = "printto";
process.StartInfo.Arguments = "\"" + printerName + "\""; 
process.Start();

process.WaitForInputIdle();
process.Kill();

Problem - there is still an Adobe Reader window popping up, but after the printing is done it closes itself usually.

Solution - convince the client to use Foxit Reader (don´t use last two lines of code).


Convert PDF pages to Drawing.Image

I´ve no idea how to do it with code, but when I get this to work the rest is just a piece of cake. Printing.PrintDocument can fulfill all demands.


Anyone an idea to get some Drawing.Image´s out of those PDF´s or another approach how to do it?

Best Regards, Max

12 Answers

I tried many things and the one that worked best for me was launching a SumatraPDF from the command line:

// Launch SumatraPDF Reader to print
String arguments = "-print-to-default -silent \"" + fileName + "\"";
System.Diagnostics.Process.Start("SumatraPDF.exe", arguments);

There are so many advantages to this:

  1. SumatraPDF is much much faster than Adobe Acrobat Reader.
  2. The UI doesn't load. It just prints.
  3. You can use SumatraPDF as a standalone application so you can include it with your application so you can use your own pa. Note that I did not read the license agreement; you should probably check it out yourself.

My company offers Docotic.Pdf library that can render and print PDF documents. The article behind the link contains detailed information about the following topics:

  • printing PDFs in Windows Forms or WPF application directly
  • printing PDFs via an intermediate image
  • rendering PDFs on a Graphics

There are links to sample code, too.

I work for the company, so please read the article and try suggested solutions yourselves.

As of July 2018, there is still no answer for the OP. There is no free way to 1) silently print your pdf for a 2) closed source project.

1) You can most certainly use a process i.e. Adobe Acrobat or Foxit Reader

2) Free solutions have a GPL (GNU's General Public License). This means you must open your source code if giving the software, even for free, to anyone outside your company.

As the OP says, if you can get a PDF to Drawing.Image, you can print it with .NET methods. Sadly, software to do this also requires payment or a GPL.

Related