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();
Imports IronPdf
Imports System.Threading.Tasks

' Create a new PDF and print it
Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = 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
Dim PrintDocYouCanWorkWith As System.Drawing.Printing.PrintDocument = pdf.GetPrintDocument()
$vbLabelText   $csharpLabel

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();
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Get PrintDocument object
Private printDocument = pdf.GetPrintDocument()

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

' Print document
printDocument.Print()
$vbLabelText   $csharpLabel

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();
Imports IronPdf
Imports System.Drawing.Printing

Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Get PrintDocument object
Private printDocument = pdf.GetPrintDocument()

' Set printer resolution
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
	.Kind = PrinterResolutionKind.Custom,
	.X = 1200,
	.Y = 1200
}

' Print document
printDocument.Print()
$vbLabelText   $csharpLabel

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);
Imports IronPdf
Imports System.Threading.Tasks

Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

Await pdf.PrintToFile("PathToFile", False)
$vbLabelText   $csharpLabel

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();
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Get PrintDocument object
Private printDocument = pdf.GetPrintDocument()

' Subscribe to the PrintPage event
Private printedPages = 0
Private printDocument.PrintPage += Sub(sender, args) printedPages++

' Print document
printDocument.Print()
$vbLabelText   $csharpLabel

Frequently Asked Questions

How can I print a PDF file using C#?

To print a PDF file using C#, you can use the IronPDF library. Download the library, choose a PDF file, select a printer, set the resolution, and check the output from your printer.

What is the basic example code to print a PDF in C#?

Here is a basic example: Load a PDF document with PdfDocument.FromFile, create a PrintDocument object using pdf.GetPrintDocument(), and then call printDocument.Print() to print the document.

How do I specify a printer name in C# for printing a PDF?

To specify a printer name, use the PrinterSettings.PrinterName property on the PrintDocument object obtained from your PDF document.

Can I set the printer resolution when printing a PDF with C#?

Yes, you can set the printer resolution using the DefaultPageSettings.PrinterResolution property of the PrintDocument object.

Is it possible to print a PDF to a file instead of a printer?

Yes, you can use the PdfDocument.PrintToFile method to print a PDF to a file. You need to specify the output file path and whether you want a preview.

How can I track the printing process of a PDF in C#?

You can track the printing process by hooking into the PrintPage event of the PrintDocument object. This allows you to monitor each page being printed.

What advanced printing features are supported?

IronPDF supports advanced printing features such as setting printer names, customizing printer resolution, and printing directly to files.

Chaknith Bin
Software Engineer
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.