How to print a PDF file in C#
By FoxLearn 2/19/2025 2:23:35 AM 1.24K
Working with PDFs in C# isn't as straightforward as it is in other programming languages. Unfortunately, many PDF libraries are commercial, requiring a paid license. While some libraries, like Spire PDF, offer a free version, it comes with limitations such as supporting only up to 10 pages per PDF and adding a watermark indicating that it's for testing purposes.
If you're looking to print a PDF from your WinForms application without relying on a paid API, we’ll show you two effective workarounds to help you print PDFs with ease.
1. Using Adobe Acrobat
If you want to use Adobe Acrobat Reader for printing PDF files in C# assumes that the user already has Acrobat Reader installed on their computer.
// pdf c# print to public void PrintPdf(string fileName) { using (PrintDialog printDialog = new PrintDialog()) { if (printDialog.ShowDialog() == DialogResult.OK) { ProcessStartInfo printProcessInfo = new ProcessStartInfo() { Verb = "print", CreateNoWindow = true, FileName = fileName, WindowStyle = ProcessWindowStyle.Hidden }; Process printProcess = new Process(); printProcess.StartInfo = printProcessInfo; printProcess.Start(); printProcess.WaitForInputIdle(); Thread.Sleep(1000); if (false == printProcess.CloseMainWindow()) { printProcess.Kill(); } } } }
This code executes the print command in a hidden command line and triggers the system's Print Dialog. In certain versions of Windows, Acrobat Reader may briefly open but will automatically close after you click "OK," and the file will then be printed.
2. Using the RawPrint package
If you prefer not to use Acrobat Reader for printing PDF files in C#, you can utilize the RawPrint library. RawPrint allows you to send files directly to a Windows printer without relying on the printer driver.
First, you need to install the RawPrint library from NuGet.
Open your project in Visual Studio, then right-click on your project in Solution Explorer, select "Manage NuGet Packages...", search for RawPrint
, and install it.
Printing a PDF file in C# using the RawPrint library involves sending raw commands directly to the printer. To use the RawPrint methods, you must specify the printer by its name. Make sure you have the printer's name ready before sending a file to print.
// c# print pdf using its RAW data directly to the printer public void PrintPdf(string fileName, string printerName) { FileInfo fileInfo = new FileInfo(fileName); // Create an instance of the Printer IPrinter printer = new Printer(); // Send the PDF file to the printer printer.PrintRawFile(printer, fileName, fileInfo.Name); }
To get all printers in your computer, you can use the following c# code.
foreach (string printerName in System.Drawing.Printing.PrinterSettings.InstalledPrinters) { Console.WriteLine(printerName); }
To print a PDF from a file, simply use the PrintRawFile method from an instance of RawPrint. The method requires three arguments: the first is the name of the printer you want to use, the second is the absolute path to the PDF file (including the filename) you wish to print, and the third is the same file name. The final argument is used solely to display the file name in the print queue.