How to Print PDF Files in C#

Sending a PDF to a printer from .NET C# code automates the printing process, allowing you to integrate printing functionality into your applications, reducing manual efforts, and ensuring consistency in PDF file production. It provides precise control over the printing process.

IronPDF offers the option to quickly print programmatically to a physical printer in one method call, allowing you to print multiple PDF files. The printer resolution can also be specified with configurable horizontal and vertical DPI. Use the method that accepts both Microsoft PrinterSettings and PrintController for further control over the PDF printing process.


Get started with IronPDF

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

First Step:
green arrow pointer


Print PDF File Example

The Print method is available through the PdfDocument object, allowing you to print both newly created and existing PDF files. By default, the method uses the system's default printer, but you can specify a different printer by passing its name as a string to the Print method.

Please note
All printing features are only supported on Windows.

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

// Create an instance of ChromePdfRenderer, which is used to render HTML content into PDF format.
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Use the renderer to render a simple HTML string into a PDF document.
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Test Printing</h1>");

// Print the PDF document using the specified printer.
// In this case, it prints to "Microsoft Print to PDF", which will save the document as a file instead of actually printing it on paper.
pdf.Print("Microsoft Print to PDF");
Imports IronPdf

' Create an instance of ChromePdfRenderer, which is used to render HTML content into PDF format.
Private renderer As New ChromePdfRenderer()

' Use the renderer to render a simple HTML string into a PDF document.
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Test Printing</h1>")

' Print the PDF document using the specified printer.
' In this case, it prints to "Microsoft Print to PDF", which will save the document as a file instead of actually printing it on paper.
pdf.Print("Microsoft Print to PDF")
$vbLabelText   $csharpLabel
Print queue

Printer Resolution

You can specify the resolution of the printed PDF by providing the desired DPI value to the Print method, which will apply the same DPI for both horizontal and vertical dimensions. To use different DPI values, pass two parameters to the method: the first for horizontal (x) DPI and the second for vertical (y) DPI.

:path=/static-assets/pdf/content-code-examples/how-to/print-pdf-dpi.cs
// Importing the IronPdf library which is used for generating, editing, and printing PDFs in .NET
using IronPdf;

// Create a new instance of ChromePdfRenderer from IronPdf
// This is used to render HTML content into PDF format
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PDF document by rendering HTML as PDF.
// In this example, simple HTML with an <h1> header is converted to a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Test printing</h1>");

// Print the PDF with custom DPI settings
// Correct syntax usage requires specifying printer name even if setting DPI
// Print the document with a resolution of 300 DPI to the specified printer
// Make sure that the specified printer name is available on your system, or replace it with an available printer name
pdf.Print(300, printerName: "Microsoft Print to PDF");
' Importing the IronPdf library which is used for generating, editing, and printing PDFs in .NET
Imports IronPdf

' Create a new instance of ChromePdfRenderer from IronPdf
' This is used to render HTML content into PDF format
Private renderer As New ChromePdfRenderer()

' Create a PDF document by rendering HTML as PDF.
' In this example, simple HTML with an <h1> header is converted to a PDF document
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Test printing</h1>")

' Print the PDF with custom DPI settings
' Correct syntax usage requires specifying printer name even if setting DPI
' Print the document with a resolution of 300 DPI to the specified printer
' Make sure that the specified printer name is available on your system, or replace it with an available printer name
pdf.Print(300, printerName:= "Microsoft Print to PDF")
$vbLabelText   $csharpLabel

Let's see how to rasterize and print a PDF file in the next example.


Print to File

The PrintToFile method efficiently rasterizes PDF documents by converting them into bitmap (pixel-based) images and saving them as a PDF file. This process is handled by the Windows built-in printer, such as "Microsoft Print to PDF." Notably, this method saves the PDF to disk without sending it to a physical printer.

:path=/static-assets/pdf/content-code-examples/how-to/print-pdf-print-to-file.cs
using IronPdf; // This namespace is needed to use IronPdf functionalities

// Create an instance of ChromePdfRenderer, which is used to render PDFs
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render a simple HTML string as a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Test printing</h1>");

// Specify the file path where you want to save the PDF
string filePath = "output.pdf";

// Print the PDF document to the specified file path
// Ensure filePath is not empty to avoid exceptions
if (!string.IsNullOrWhiteSpace(filePath))
{
    // Save the rendered PDF to the specified file path
    pdf.SaveAs(filePath);
    Console.WriteLine($"PDF has been saved to {filePath}");
}
else
{
    // Inform the user when the file path is invalid
    Console.WriteLine("File path is not specified. Please provide a valid file path.");
}
Imports IronPdf ' This namespace is needed to use IronPdf functionalities

' Create an instance of ChromePdfRenderer, which is used to render PDFs
Private renderer As New ChromePdfRenderer()

' Render a simple HTML string as a PDF document
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Test printing</h1>")

' Specify the file path where you want to save the PDF
Private filePath As String = "output.pdf"

' Print the PDF document to the specified file path
' Ensure filePath is not empty to avoid exceptions
If Not String.IsNullOrWhiteSpace(filePath) Then
	' Save the rendered PDF to the specified file path
	pdf.SaveAs(filePath)
	Console.WriteLine($"PDF has been saved to {filePath}")
Else
	' Inform the user when the file path is invalid
	Console.WriteLine("File path is not specified. Please provide a valid file path.")
End If
$vbLabelText   $csharpLabel

Explore Print PDF Document Settings

To customize printing options, use the GetPrintDocument method, which accepts Microsoft PrinterSettings and PrintController objects. This method returns the current print document object. The PrinterSettings allows you to configure options such as page range and printer selection, while PrintController enables control over the printing process, including handling exceptions, progress reporting, print dialogs, print previews, and other related tasks. Detailed descriptions of PrinterSettings options are provided below the code example.

:path=/static-assets/pdf/content-code-examples/how-to/print-pdf-printer-setting.cs
/* 
- The code has been encapsulated within a `Main` method inside a `Program` class to provide a valid entry point for the application.
- Exception handling with a try-catch block has been added to catch and report potential errors during the PDF generation and printing process.
- Improved the comments for clarity and explained the purpose of key functions and settings in the code.
- Given that the user intends to print to "Microsoft Print to PDF", it is essential to have such a printer installed and available on the hosting machine for this script to work.
*/
' 
'- The code has been encapsulated within a `Main` method inside a `Program` class to provide a valid entry point for the application.
'- Exception handling with a try-catch block has been added to catch and report potential errors during the PDF generation and printing process.
'- Improved the comments for clarity and explained the purpose of key functions and settings in the code.
'- Given that the user intends to print to "Microsoft Print to PDF", it is essential to have such a printer installed and available on the hosting machine for this script to work.
'
$vbLabelText   $csharpLabel
  • CanDuplex: Indicates whether the printer supports duplex (double-sided) printing. If true, printing on both sides of the paper is possible; otherwise, it cannot.
  • Collate: Specifies whether multiple PDF files or copies of a PDF document should be collated (organized in order) when printed. When true, the printer collates the copies; when false, it does not.
  • Copies: Sets the number of copies of the PDF document to print. It determines how many identical copies of the document will be printed.
  • DefaultPageSettings: Represents the default page settings for the printer, including paper size, margins, and orientation.
  • Duplex: Specifies the duplex (double-sided) printing mode to use. Options include Duplex.Default, Duplex.Simplex (single-sided), Duplex.Horizontal, and Duplex.Vertical.
  • InstalledPrinters: Provides a collection of installed printer names on the system. You can iterate through this collection to get the names of available printers.
  • IsDefaultPrinter: Indicates whether the printer specified in PrinterName is set as the default printer on the system.
  • IsPlotter: Determines whether the printer is a plotter. Plotter printers are often used for large-format printing, such as for architectural or engineering drawings.
  • IsValid: Indicates whether the printer settings are valid and can be used for printing PDF files.
  • LandscapeAngle: Specifies the angle (rotation) of landscape orientation for the printer, usually 90 degrees for portrait.
  • MaximumCopies: Represents the maximum number of copies that can be specified for printing PDF.
  • MaximumPage: Specifies the maximum page number that can be set for printing or conversion.
  • MinimumPage: Specifies the minimum page number that can be set for printing or conversion.
  • PaperSizes: Provides a collection of supported paper sizes for the printer. You can query this collection to determine available paper sizes.
  • PaperSources: Offers a collection of paper sources or trays available for the printer. This can be useful when selecting the paper source for printing PDF files.
  • PrinterName: Specifies the name of the printer to use for printing or conversion.
  • PrinterResolutions: Provides a collection of available printer resolutions, allowing you to choose the print quality.
  • PrintFileName: Gets or sets the file name when printing to a file using PrintToFile.
  • PrintRange: Specifies the range of PDF pages to print, such as all pages, a specific range, or a selection. Use this to print specific pages.
  • FromPage: Specifies the starting page number for printing or conversion. Printing will begin from this page.
  • ToPage: Specifies the ending page number for printing or conversion. Printing will stop after reaching this page.
  • PrintToFile: Indicates whether to print to a file instead of a physical printer. When true, you can specify the file path using PrintFileName.
  • SupportsColor: Indicates whether the printer supports color printing. If true, print in color is supported; otherwise, it is limited to black and white (monochrome) printing.

Lastly, to configure the default printer to print PDFs, you may go to the "Printers & Scanners" section of the machine settings.

Frequently Asked Questions

What is the library used for printing PDF files programmatically in .NET?

IronPDF is a .NET library that allows developers to work with PDF documents, including printing PDF files programmatically from .NET C# applications.

How can I print a PDF file in C#?

To print a PDF file using IronPDF in C#, you can use the `Print` method available through the `PdfDocument` object. This method can send the PDF to the system's default printer or to a specified printer by name.

Can I specify printer settings when printing a PDF?

Yes, you can specify printer settings by using the `GetPrintDocument` method in IronPDF. This method accepts Microsoft `PrinterSettings` and `PrintController` objects to customize options such as page range and printer selection.

Is it possible to print with specific DPI settings?

Yes, IronPDF allows you to print with specific DPI settings by passing the desired DPI values to the `Print` method. You can set both horizontal and vertical DPI for more precise printing control.

What is the method for saving PDF documents as images?

The `PrintToFile` method in IronPDF converts PDF documents into bitmap images and saves them as a PDF file. This method does not send the document to a physical printer but saves it to disk, using Windows built-in printers like 'Microsoft Print to PDF'.

Does the library support printing on macOS or Linux?

Currently, the printing features of IronPDF are only supported on Windows platforms.

Can the library handle duplex printing?

Yes, IronPDF can handle duplex (double-sided) printing if the printer supports it. This can be configured through the `PrinterSettings` object.

Is it possible to print multiple copies of a PDF document?

Yes, you can specify the number of copies to print using the `Copies` property in the `PrinterSettings` object in IronPDF.

How do I set up the library in my project?

IronPDF can be installed via NuGet Package Manager in your .NET project. You can download it from the NuGet website or directly from the package manager console in Visual Studio.

Can I print specific page ranges of a PDF document?

Yes, you can specify the page range to print by using the `PrintRange`, `FromPage`, and `ToPage` properties within the `PrinterSettings` object in IronPDF.

Chaknith related to Explore Print PDF Document Settings
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.