C# Print PDF to Specific Printer (Code Example Tutorial)

There are many instances where developers may need to print PDF documents directly from their application. This task can sometimes seem complicated when the user either wants to print multiple PDF files or use a specific printer name other than the default printer. Today, there are multiple methods out there that can assist us with printing PDF files. Some of these need to be paid for, some perform erratically, and some are difficult to implement.

The IronPDF is a .NET that provides a set of classes that can be used to create PDF files programmatically. These classes are located in the IronPDF.Core assembly and are designed to be easy to use with any .NET language, including C#, VB.NET, F#, etc. The library offers many functions for creating PDF documents, manipulating existing PDFs, reading PDFs, printing PDFs, and programmatically creating PDF Forms.

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

Install IronPDF

First of all, install the IronPDF library. For that, go to the Package Manager Console and write the following command.

Install-Package IronPrint

C# Print PDF to Specific Printer (Code Example Tutorial), Figure 1: Install the package via the Package Manager Console Install the package via the Package Manager Console

The next step is to create a PDF document first. You can also simply load an existing PDF document if you don't want to create a new one. Let's create a PDF document.

Create a PDF Document using a URL

Let's create a PDF Document using a URL. IronPDF provides two functions for this purpose. RenderUrlAsPdf and RenderUrlAsPdfAsync. Both functions have the same functionality other than RenderUrlAsPdfAsync provides asynchronous functionality.

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

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

Create a PDF Document using HTML String

It is also possible to create a PDF document using HTML string. IronPDF provides two methods for creating a PDF file using HTML string: RenderHtmlAsPdf and RenderHtmlAsPdfAsync. These methods are similar to one another, the only difference being that RenderHtmlAsPdfAsync is used for asynchronous operations.

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

IronPdf.ChromePdfRenderer renderer = new IronPdf.ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(@"<h1>My PDF File</h1> <p>This is sample PDF document created to demonstrate the PDF File generation using HTML string</p>");
IronPdf.ChromePdfRenderer renderer = new IronPdf.ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(@"<h1>My PDF File</h1> <p>This is sample PDF document created to demonstrate the PDF File generation using HTML string</p>");
Dim renderer As New IronPdf.ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>My PDF File</h1> <p>This is sample PDF document created to demonstrate the PDF File generation using HTML string</p>")
VB   C#

Print PDF Files to a Specific Printer

There are multiple instances where printing PDF files using a specific printer is needed. This task can be done simply by using the PrinterSettings.PrinterName property. Set your printer name to this property. Let's have a quick demonstration of how to print PDF documents to a specific printer in C#.

var printDoc = pdf.GetPrintDocument();
printDoc.PrinterSettings.PrinterName = "myPrinter";
var printDoc = pdf.GetPrintDocument();
printDoc.PrinterSettings.PrinterName = "myPrinter";
Dim printDoc = pdf.GetPrintDocument()
printDoc.PrinterSettings.PrinterName = "myPrinter"
VB   C#

The pdf refers to a PDF file that was just created from the URL.

GetPrintDocument Returns a PrintDocument for the PDF allowing granular control over sending the PDF to a Printer.

Then, a printer name can be specified as needed.

Print Dialogue

Let's suppose the user needs to select all the printer settings using UI dialogue as it appears in a Word document or Adobe Acrobat. IronPDF's capabilities can show the print GUI dialogue to the user by passing just one parameter to Print function as shown below.

pdf.Print(true);
pdf.Print(true);
pdf.Print(True)
VB   C#

Just pass the true value to the argument of the Print function as this is false by default. It shows how easy it is to display the GUI print dialogue. pdf.Print() function prints to a default printer.

Specify the Number of Copies

In case it is needed to print multiple copies of a single document. This can easily be achieved by setting the PrinterSettings.Copies property. The following sample code demonstrates this.

var printDoc = pdf.GetPrintDocument();
printDoc.PrinterSettings.Copies = 5;
var printDoc = pdf.GetPrintDocument();
printDoc.PrinterSettings.Copies = 5;
Dim printDoc = pdf.GetPrintDocument()
printDoc.PrinterSettings.Copies = 5
VB   C#

Here, pdf is a current print document object.

Select Page Range to Print PDF file

There is often a use case in which users may not want to print a complete document. In such cases, specifying the PrinterSettings.FromPage PrinterSettings.ToPage property will help the job done.

The following code demonstrates the use of this IronPDF feature.

var printDoc = pdf.GetPrintDocument();
printDoc.PrinterSettings.FromPage = 3;
printDoc.PrinterSettings.ToPage = 3;
var printDoc = pdf.GetPrintDocument();
printDoc.PrinterSettings.FromPage = 3;
printDoc.PrinterSettings.ToPage = 3;
Dim printDoc = pdf.GetPrintDocument()
printDoc.PrinterSettings.FromPage = 3
printDoc.PrinterSettings.ToPage = 3
VB   C#

The first line of code will set the page number of the first page that needs to be printed. The second line will set the last page of the document to print.

Collate Property

IronPDF also provides advanced printing features — and "collate" is one such feature.

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. this property can be set as true or false as per our choice.

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

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

Get Paper Sources

It is also possible 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 for developing .NET applications that require printing functionalities. There are multiple options available for printing PDF files. Use any of the choices that best fit your needs and can also print multiple PDF files.

Summary

This tutorial demonstrated how to print to PDF straightforwardly using a specific printer using the IronPDF library — it is free for development and provides high-performance levels. For more print PDF capabilities with IronPDF, please click this sample page.

Furthermore, IronPDF is also capable of rendering charts in PDFs, adding barcodes, enhancing security with passwords and watermarking in just a few lines of code.

There are also other many useful libraries such as IronXL for working with Excel documents, IronBarcode for working with barcodes, and IronOCR for working with OCR. 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.