Updated October 20, 2024
Share:

C# Print PDF Documents

You can easily print a PDF in .NET applications with the help of Visual Basic or C# code. This tutorial will walk you through how to use C# print PDF capabilities to print programmatically.



Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer

Create a PDF and Print

You can send a PDF document directly to a printer silently or create a System.Drawing.Printing.PrintDocument object, which can be worked with and sent to GUI print dialogs.

The following code can be used for both options:

:path=/static-assets/pdf/content-code-examples/how-to/csharp-print-pdf-create-and-print-pdf.cs
using IronPdf;
using System.Threading.Tasks;

// Create a new PDF and print it
ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");

// Print PDF from default printer
await pdf.Print();

// For advanced silent real-world printing options, use PdfDocument.GetPrintDocument
// Remember to add an assembly reference to System.Drawing.dll
System.Drawing.Printing.PrintDocument PrintDocYouCanWorkWith = pdf.GetPrintDocument();

Advanced Printing

IronPDF is quite capable of dealing with advanced Printing features such as finding the printer name or setting it and setting the Printer resolution.

Specify Printer Name

To specify the printer name, all you need to do is to get the current print document object (with the use of the GetPrintDocument method for PDF Documents of the PDF document), then use the PrinterSettings.PrinterName property, as follows:

:path=/static-assets/pdf/content-code-examples/how-to/csharp-print-pdf-specify-printer-name.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Get PrintDocument object
var printDocument = pdf.GetPrintDocument();

// Assign the printer name
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

// Print document
printDocument.Print();

Set Printer Resolution

Resolution refers to the number of pixels being printed, or displayed, depending on your output. You can set the resolution of your printing by making use of the DefaultPageSettings.PrinterResolution property of the PDF document. Here is a very quick demonstration:

:path=/static-assets/pdf/content-code-examples/how-to/csharp-print-pdf-specify-printer-resolution.cs
using IronPdf;
using System.Drawing.Printing;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Get PrintDocument object
var printDocument = pdf.GetPrintDocument();

// Set printer resolution
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
    Kind = PrinterResolutionKind.Custom,
    X = 1200,
    Y = 1200
};

// Print document
printDocument.Print();

As you can see, I have set the resolution to a custom level: 1200 vertical and 1200 horizontal.

PrintToFile Method

The PdfDocument.PrintToFile method allows you to print the PDF to a file. You simply supply the output filepath and specify whether or not you’d like to see a preview. The code below will print to the specified file without including a preview:

:path=/static-assets/pdf/content-code-examples/how-to/csharp-print-pdf-printtofile.cs
using IronPdf;
using System.Threading.Tasks;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

await pdf.PrintToFile("PathToFile", false);

Tracing Printing Processes using C

The beauty of C# in conjunction with IronPDF is that when it comes to keeping track of printed pages or anything printing-related, it is actually quite simple. In the next example, I will demonstrate how to change the printer’s name, resolution as well as how to get a count of pages that were printed.

:path=/static-assets/pdf/content-code-examples/how-to/csharp-print-pdf-trace-printing-process.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Get PrintDocument object
var printDocument = pdf.GetPrintDocument();

// Subscribe to the PrintPage event
var printedPages = 0;
printDocument.PrintPage += (sender, args) => printedPages++;

// Print document
printDocument.Print();
Chaknith Bin

Chaknith Bin

Software Engineer

 LinkedIn

Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.