How to Print PDF Files 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.
How to Print PDF Files in C#
- Download the IronPDF Library for printing PDF
- Prepare the input PDF file for printing
- Use the
Print
method to send the PDF files to the default printer - Specify the printer name to send it to a different printer
- Use the
GetPrintDocument
method for advanced printer settings
Get started with IronPDF
Start using IronPDF in your project today with a free trial.
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
:path=/static-assets/pdf/content-code-examples/how-to/print-pdf-print.cs
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Test printing</h1>");
// Send the document to "Microsoft Print to PDF" printer
pdf.Print("Microsoft Print to PDF");
Imports IronPdf
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Test printing</h1>")
' Send the document to "Microsoft Print to PDF" printer
pdf.Print("Microsoft Print to PDF")
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
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Test printing</h1>");
// Set custom DPI
pdf.Print(300);
// Specify printing resolution
pdf.Print(10, 10, "Microsoft Print to PDF");
Imports IronPdf
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Test printing</h1>")
' Set custom DPI
pdf.Print(300)
' Specify printing resolution
pdf.Print(10, 10, "Microsoft Print to PDF")
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;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Test printing</h1>");
// Print to file
pdf.PrintToFile("");
Imports IronPdf
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Test printing</h1>")
' Print to file
pdf.PrintToFile("")
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
using IronPdf;
using System.Drawing.Printing;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Testing</h1>");
PrinterSettings settings = new PrinterSettings() {
PrinterName = "Microsoft Print to PDF",
// Number of Copy
Copies = 2,
// Page range to print
FromPage = 2,
ToPage = 4,
};
PrintDocument document = pdf.GetPrintDocument(settings);
// Print
document.Print();
Imports IronPdf
Imports System.Drawing.Printing
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Testing</h1>")
Private settings As New PrinterSettings() With {
.PrinterName = "Microsoft Print to PDF",
.Copies = 2,
.FromPage = 2,
.ToPage = 4
}
Private document As PrintDocument = pdf.GetPrintDocument(settings)
' Print
document.Print()
- 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.