C# Print PDF Programatically (Code Example Tutorial)

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 dot 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.

To the best of my knowledge, IronPDF is best for these purposes. It is free for development, easy to use, and provides high performance. It is compatible with all dot net frameworks and languages including C#,VB.NET and F#. It is also compatible with any type of dot 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 of all, we need to install the IronPDF NuGet Package. Go to NuGet Package Manager > Manage NuGet Package for Solution, as shown below.

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

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

We first need a PDF document to print — let's create a PDF document.

Generate a PDF Document

I am generating a PDF document using a URL. IronPDF provides an easy way to do this.

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

Sometimes we may need to use the PrintToFile Option in our application for various reasons. IronPDF provides a printToFile() function. 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 in a similar way to the Microsoft print-to-PDF function.

Print to Default Printer

We often use our default printer to print PDF files. 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

Most of the time, we want our users to choose the printer settings using GUI print dialogue. IronPDF's capabilities enable us to show 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. We can see how easy it is to display GUI print dialogue. pdf.PrintToFile() function print to default printer.

Number of Copies to be Printed

We can also specify how many copies of a given document we need 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

We can also specify the last page to be printed in the PDF file. We can use the PrinterSettings.ToPage property 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 multi-page document, the copies will print all pages of each copy before printing the second copy. We can set this property as true or false as per our choice.

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

For example, if we set the collate property 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

We can get the default page settings of the printer using the following code.

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

We can use this default setting later in the program.

Double-Sided Printing

There are multiple use cases where we need to print PDF files on both sides of the pages. IronPDF provides the PrinterSettings.CanDuplex property which returns true or false telling us whether our 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 our printer supports duplex, we can also get our printer's duplex settings. The following code demonstrates its use.

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

Get Paper Sources

We can also 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 we need while developing dot net applications that require printing functionalities. There are multiple ways to print PDF files. We can use any as per our needs. IronPDF also provides a feature to print multiple PDF files.

Summary

In this tutorial, we have learned to print PDFs in very simple ways. We have learned to 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. We have used the IronPDF library for this demonstration. IronPDF is free for development and provides high performance. For more print PDF capabilities with IronPDF, please click here.

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 IronSuite. For more details, please click here.

I hope you have liked the article, please feel free to ask any questions in the comment section.