USING IRONPRINT

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. Multiple methods can assist us with printing PDF files. Some of these need to be paid for, some perform erratically, and some are difficult to implement.

IronPDF is a .NET library that provides a set of classes for creating 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. 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

IronPDF provides two functions for creating a PDF document using a URL: RenderUrlAsPdf and RenderUrlAsPdfAsync. The 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")
$vbLabelText   $csharpLabel

Create a PDF Document using HTML String

It is possible to create a PDF document using an HTML string. IronPDF provides two methods for this purpose: RenderHtmlAsPdf and RenderHtmlAsPdfAsync. The RenderHtmlAsPdfAsync method is for asynchronous operations.

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

IronPdf.ChromePdfRenderer renderer = new IronPdf.ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(@"<h1>My PDF File</h1> <p>This is a 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 a 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 a sample PDF document created to demonstrate the PDF file generation using HTML string</p>")
$vbLabelText   $csharpLabel

Print PDF Files to a Specific Printer

Printing PDF files to a specific printer can be done easily by using the PrinterName property of PrinterSettings. Here's a 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"
$vbLabelText   $csharpLabel

The pdf variable refers to the PDF document that was created. The GetPrintDocument method returns a PrintDocument instance, allowing granular control over sending the PDF to a printer.

Print Dialogue

If the user needs to select all the printer settings using a UI dialogue similar to that appearing in a Word document or Adobe Acrobat, IronPDF can show the print GUI dialogue by passing a single parameter to the Print function.

pdf.Print(true);
pdf.Print(true);
pdf.Print(True)
$vbLabelText   $csharpLabel

Passing true to the Print function's argument displays the GUI print dialogue. By default, the Print function prints to the default printer.

Specify the Number of Copies

Printing multiple copies of a single document can be achieved by setting the Copies property of PrinterSettings. The following sample code shows 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
$vbLabelText   $csharpLabel

Here, the pdf is the current print document object.

Select Page Range to Print PDF file

If you do not wish to print a complete document, you can specify the FromPage and ToPage properties of PrinterSettings.

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
$vbLabelText   $csharpLabel

This code sets both the starting and ending pages for printing, so only a specific range of the document prints.

Collate Property

The Collate feature in printing means if you're printing more than one copy of a multi-page document, the copies will print all pages of each copy before printing the second copy. You can set this property as needed.

var printDoc = pdf.GetPrintDocument();
printDoc.PrinterSettings.Collate = false;
var printDoc = pdf.GetPrintDocument();
printDoc.PrinterSettings.Collate = false;
Dim printDoc = pdf.GetPrintDocument()
printDoc.PrinterSettings.Collate = False
$vbLabelText   $csharpLabel

With Collate set to false, the printer will print all required copies of each page before printing the next page.

Get Paper Sources

To retrieve available paper sources for the printer, use the following code:

var paperSources = pdf.GetPrintDocument().PrinterSettings.PaperSources;
var paperSources = pdf.GetPrintDocument().PrinterSettings.PaperSources;
Dim paperSources = pdf.GetPrintDocument().PrinterSettings.PaperSources
$vbLabelText   $csharpLabel

Conclusion

IronPDF provides all the necessary features for developing .NET applications that require printing functionalities. Multiple options are available for printing PDF files, allowing you to choose the best fit for your needs and print multiple PDF files.

Summary

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

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

Additionally, there are other useful libraries such as IronXL for working with Excel documents, IronBarcode for working with barcodes, and IronOCR for working with OCR. You can get all five libraries for the price of just two by purchasing the complete Iron Suite. Please visit the licensing page for more details.

Frequently Asked Questions

What is the new .NET printing library for developers?

IronPrint is Iron Software's .NET printing library that offers compatibility across various platforms such as Windows, macOS, Android, and iOS.

How do I install the PDF manipulation library?

To install IronPDF, use the following command in the Package Manager Console: Install-Package IronPdf.

How can I create a PDF document using a URL with the library?

You can create a PDF document using a URL with IronPDF by using the RenderUrlAsPdf or RenderUrlAsPdfAsync methods provided by the ChromePdfRenderer class.

How do I print a PDF to a specific printer in C#?

To print a PDF to a specific printer in C#, use the PrinterName property of PrinterSettings to specify the printer, and then execute the Print method.

Can I print multiple copies of a PDF using the library?

Yes, you can print multiple copies by setting the Copies property of PrinterSettings before executing the Print method.

How can I print only a specific range of pages in a PDF?

To print a specific range of pages, set the FromPage and ToPage properties of PrinterSettings to define the page range before printing.

What does the Collate property do in the library?

The Collate property, when set to true, ensures that all pages of each copy are printed before printing the next copy when printing multiple copies of a document.

How can I display the print dialogue when printing a PDF?

To display the print dialogue, pass 'true' to the Print function's argument, which shows the GUI print dialogue for printer settings.

Can the library retrieve available paper sources for a printer?

Yes, you can retrieve available paper sources for a printer using the PaperSources property of PrinterSettings.

What additional features does the PDF library offer?

IronPDF offers features such as rendering charts, adding barcodes, enhancing security with passwords, and watermarking, making it a versatile tool for PDF manipulation.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
< PREVIOUS
How to Print PDF in VB.NET (Step-by-Step) Tutorial
NEXT >
How to Print PDF File Without Dialog in C#

Ready to get started? Version: 2025.6 just released

View Licenses >