USING IRONPRINT

C# Print PDF Programatically (Code Example Tutorial)

Published August 31, 2022
Share:

There are multiple use cases where a print-to-PDF file feature is required in an application. This is a task that can sometimes seem difficult to achieve, but in reality, this is not the case. There are multiple tools and 3rd party libraries available for .NET applications that provide these functionalities. All have pros and cons, some are paid, some have performance issues, some are difficult to use, and some do not provide the functionality required.

IronPDF is best for these purposes and is often recommended to handle PDF printing. It is free for development, easy to use, and provides high performance. It is compatible with all .NET Frameworks and languages including C#, VB.NET and F#. Furthermore, it is also compatible with any type of .NET application such as Web Applications (Forms or .NET Core), Azure Functions, and for sure Windows Forms or Console Applications.

Let's take a look at some example code snippets for printing a PDF file.

Install IronPDF

First, install the IronPDF NuGet Package. Go to NuGet Package Manager > Manage NuGet Packages for Solution, as shown below.

C# Print PDF Programmatically (Code Example Tutorial), Figure 1: Navigate to the NuGet Package Manager Navigate to the NuGet Package Manager

Click on Browse, and search for IronPDF as shown below.

C# Print PDF Programmatically (Code Example Tutorial), Figure 2: Search for IronPdf package in the NuGet Package Manager Search for IronPdf package in the NuGet Package Manager

Press the "Install" button. The IronPDF NuGet Package will be installed.

Let's create a PDF document to print it later on.

Generate a PDF Document

In this section, IronPDF provides an easy way to generate a PDF document using a URL.

The following code snippet will create a PDF file from the URL.

IronPdf.ChromePdfRenderer renderer = new IronPdf.ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/PDF");
IronPdf.ChromePdfRenderer renderer = new IronPdf.ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/PDF");
Dim renderer As New IronPdf.ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/PDF")
VB   C#

Print to File

The PrintToFile method is provided by IronPDF. Pass the file path and name in the argument. The following code demonstrates its usage.

pdf.PrintToFile("D:/My Folder/ironpdf.pdf");
pdf.PrintToFile("D:/My Folder/ironpdf.pdf");
pdf.PrintToFile("D:/My Folder/ironpdf.pdf")
VB   C#

This statement will print a PDF file to the specified path. This works similarly to the Microsoft print-to-PDF function.

Print to Default Printer

Using the PrintToFile function will directly print the document using the default printer unless specified, otherwise by passing the printer name as a parameter to it. The following code demonstrates its use.

pdf.PrintToFile("D:/My Folder/ironpdf.pdf","printer name");
pdf.PrintToFile("D:/My Folder/ironpdf.pdf","printer name");
pdf.PrintToFile("D:/My Folder/ironpdf.pdf","printer name")
VB   C#

Show GUI print dialogs

To choose the printer settings using GUI print dialogue, IronPDF's capabilities can also show the print GUI dialogue to the user.

pdf.PrintToFile("D:/My Folder/ironpdf.pdf", showPreview: true);
pdf.PrintToFile("D:/My Folder/ironpdf.pdf", showPreview: true);
pdf.PrintToFile("D:/My Folder/ironpdf.pdf", showPreview:= True)
VB   C#

Just pass the true value to the argument of the print function. This is false by default. IronPdf shows how easy it is to achieve those printing tasks. pdf.PrintToFile function prints to the default printer.

Number of Copies to be Printed

IronPDF also offers settings to specify how many copies of a given document are needed to print. The following sample code demonstrates this.

pdf.GetPrintDocument().PrinterSettings.Copies = 3;
pdf.GetPrintDocument().PrinterSettings.Copies = 3;
pdf.GetPrintDocument().PrinterSettings.Copies = 3
VB   C#

Here, pdf is a current print document object.

Print PDF documents: from Page Property

Let's suppose that users do not want to print a complete document, and instead just want to print from a particular page. IronPDF provides the PrinterSettings.FromPage property for this purpose. Simply assign a page number to this property. Let's take a look at a quick demonstration in C#.

pdf.GetPrintDocument().PrinterSettings.FromPage = 5;
pdf.GetPrintDocument().PrinterSettings.FromPage = 5;
pdf.GetPrintDocument().PrinterSettings.FromPage = 5
VB   C#

Print PDF documents: to Page Property

To specify the last page to be printed, the PrinterSettings.ToPage property can be used for this purpose. Simply assign a page number to this property. The following code demonstrates its use:

pdf.GetPrintDocument().PrinterSettings.ToPage = 8;
pdf.GetPrintDocument().PrinterSettings.ToPage = 8;
pdf.GetPrintDocument().PrinterSettings.ToPage = 8
VB   C#

The above line of code will set the last page of the document to print.

Set the Printer Collate Property

IronPDF also provides advanced printing features, and "collate" is one of these.

Collate when printing means that if you are printing more than one copy of a multipage document, the copies will print all pages of each copy before printing the second copy. This property can be set as true or false.

pdf.GetPrintDocument().PrinterSettings.Collate = false;
pdf.GetPrintDocument().PrinterSettings.Collate = false;
pdf.GetPrintDocument().PrinterSettings.Collate = False
VB   C#

For example, if the Collate property is set to false, it will print all required copies of the page before printing the next page. In other words, setting this property to false will provide the reverse function of the collate property.

Get Default Page Settings

To get the default page settings of the printer, use the following code.

var defaultSetting = pdf.GetPrintDocument().PrinterSettings.DefaultPageSettings;
var defaultSetting = pdf.GetPrintDocument().PrinterSettings.DefaultPageSettings;
Dim defaultSetting = pdf.GetPrintDocument().PrinterSettings.DefaultPageSettings
VB   C#

This default setting can be used later in the program.

Double-Sided Printing

IronPDF provides the PrinterSettings.CanDuplex property which returns true or false tells us whether this printer can support duplex printing or not. The following code example demonstrates its use.

bool isDuplex = pdf.GetPrintDocument().PrinterSettings.CanDuplex;
bool isDuplex = pdf.GetPrintDocument().PrinterSettings.CanDuplex;
Dim isDuplex As Boolean = pdf.GetPrintDocument().PrinterSettings.CanDuplex
VB   C#

If the printer supports duplex, the following code demonstrates how to interact with the printer's duplex settings.

var duplexSettings = pdf.GetPrintDocument().PrinterSettings.Duplex;
var duplexSettings = pdf.GetPrintDocument().PrinterSettings.Duplex;
Dim duplexSettings = pdf.GetPrintDocument().PrinterSettings.Duplex
VB   C#

Get Paper Sources

The following code shows how to get the paper sources of the printer.

var paperSources = pdf.GetPrintDocument().PrinterSettings.PaperSources;
var paperSources = pdf.GetPrintDocument().PrinterSettings.PaperSources;
Dim paperSources = pdf.GetPrintDocument().PrinterSettings.PaperSources
VB   C#

Conclusion

IronPDF provides all the necessary features needed while developing .NET applications that require printing functionalities. There are multiple ways to print PDF files. IronPDF also provides a feature to print multiple PDF files.

Summary

This tutorial demonstrated how to print PDFs in very simple ways by using the IronPDF library: generate PDF files using URL, print PDF to file, print PDF to the default printer, specify the number of copies, specify from and to the page, collate property, and get the paper source. IronPDF is free for development and provides high performance. Please visit the examples pages for more print PDF capabilities with IronPDF.

Furthermore, IronPDF is the perfect tool for PDF-related tasks using C#. IronPDF offers developers methods to render PDF documents into images, extract text and content from a PDF. Additionally, IronPDF is also capable of rendering charts in PDFs, adding barcodes, and even handling PDF forms programmatically.

There are other many useful libraries available today, such as IronXL for working with Excel Sheets, IronBarcode for Reading and Generating barcodes, and IronOCR for Extracting Text from images or PDF files. You can currently get all five libraries for the price of just two by purchasing the complete Iron Suite. Please visit the licensing page, for more details.

< PREVIOUS
How to Print PDF files in a C# Windows application
NEXT >
C# Send PDF to Printer (Step-By-Step Tutorial)

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 12,923 View Licenses >