Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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.
PrintDoc
class to configure the PDF PrinterPrinterName
propertyPrint
method to print with default DPI or custom DPIFirst of all, install the IronPDF library. For that, go to the Package Manager Console and write the following command.
Install-Package IronPrint
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.
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")
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>")
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"
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.
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)
Passing true
to the Print
function's argument displays the GUI print dialogue. By default, the Print
function prints to the default printer.
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
Here, the pdf
is the current print document object.
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
This code sets both the starting and ending pages for printing, so only a specific range of the document prints.
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
With Collate
set to false, the printer will print all required copies of each page before printing the next page.
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
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.
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.
IronPrint is Iron Software's .NET printing library that offers compatibility across various platforms such as Windows, macOS, Android, and iOS.
To install IronPDF, use the following command in the Package Manager Console: Install-Package IronPdf.
You can create a PDF document using a URL with IronPDF by using the RenderUrlAsPdf or RenderUrlAsPdfAsync methods provided by the ChromePdfRenderer class.
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.
Yes, you can print multiple copies by setting the Copies property of PrinterSettings before executing the Print method.
To print a specific range of pages, set the FromPage and ToPage properties of PrinterSettings to define the page range before printing.
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.
To display the print dialogue, pass 'true' to the Print function's argument, which shows the GUI print dialogue for printer settings.
Yes, you can retrieve available paper sources for a printer using the PaperSources property of PrinterSettings.
IronPDF offers features such as rendering charts, adding barcodes, enhancing security with passwords, and watermarking, making it a versatile tool for PDF manipulation.