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.

I have discovered IronPDF, which is free for development, easy to use, and provides high levels of performance. It is compatible with all Dot Net versions and languages including C# and F#. We can use IronPDF in ASP.NET MVC, ASP.NET Web Forms, ASP.NET WEB APIs, etc.

The IronPDF is a Dot 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, we need to install the IronPDF library. For that, go to the Package Manager Console and write the following command.

Install-package IronPDF

We need 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 URL

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

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

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

Create a PDF Document using HTML String

We can also 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 we might want to print PDF files using a specific printer. We can achieve this task 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 As Var=pdf.GetPrintDocument()
PrintDoc.PrinterSettings.PrinterName = "myPrinter"
VB   C#

The "doc" refers to a PDF file that we have just created from the URL.

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

Here we can specify the printer name that we wish to use.

Print Dialogue

Let's suppose we want the user to select all the printer settings using UI dialogue as it appears in a Word document or Adobe Acrobat. IronPDF's capabilities enable us to show the print GUI dialogue to the user. We can add UI Dialogue 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. This is false by default. We can see how easy it is to display the GUI print dialogue. pdf.print() function print to default printer.

Specify the Number of Copies

Suppose we have a use case in which we want 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 As Var=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 we specify particular pages to print, as users may not want to print a complete document. In such cases, we can achieve our aims by specifying the PrinterSettings.FromPage PrinterSettings.ToPage property.

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 As Var=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. We can set this property 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 As Var=pdf.GetPrintDocument()
PrintDoc.PrinterSettings.Collate = False
VB   C#

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

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 for developing Dot 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. We can also print multiple PDF files.

Summary

In this tutorial, we have learned to print to PDF using a specific printer in a straightforward way. We have used the IronPDF library in this demonstration — it is free for development and provides high-performance levels. For more print PDF capabilities with IronPDF, please click here.

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

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