C# Print PDF Files using IronPDF

PDF is the most commonly used file format. It is lightweight and supported by most operating systems. PDF files can be shared easily from one device to another. They provide ease of reading as they appear exactly the same on any interface and OS. PDFs can be accessed from all devices including mobiles, tablets, etc.

Their popularity demands that all C# developers know how to print PDF files using C#. This is because you might come across a problem when you have to print a document such as an invoice, pay slip, order slip or profile, etc. These documents are often in PDF file format, so we must know how to print PDF files using C#.

Printing PDF documents in C# can be done using multiple libraries. But, one issue you may come across is the license fee. Most of them are not free or are difficult to use. But luckily, you do not have to worry. I have found the easiest and simplest way to print PDF files in C#.

I will use the IronPDF Library to print PDF documents. Believe me, you are going to like it!


What will you get from this article?

After reading this article you will be able to perform the following tasks:

  • Print single PDF documents in C#
  • Print multiple PDF files in one go using C#
  • Convert a URL to PDF and then print it.

You must have basic knowledge of C# programming for a better understanding of this tutorial.

Before starting the tutorial, let's first understand what IronPDF is.


IronPDF:

IronPDF is a .NET Library that provides us the simplest way to read, create, manipulate and print PDF files. This library can be used in C# and VB .NET applications. It works easily with ASP .Net, ASP .NET core, MVC, Web Forms, and Web APIs on both .NET and .NET core. It allows users to download PDF files, send them by email and store them in a cloud.

You can explore more about IronPDF using this link.

Let's start the demonstration.

Open Visual Studio

Open Visual Studio. Create a new project or open an existing project if you are going to add this functionality to your existing software.

I am using Console Application for this tutorial; you can use any of your choices according to your software requirements.

Project Created

Project Created.

Install NuGet Package

Open the console by clicking on Tools > NuGet Package Manager > Package Manager Console from the Menu bar on the top of the window.

NuGet Package Manager Console will open as shown below:

NuGet Package Manager Console

NuGet Package Manager Console

Write the following command in the console to Install IronPdf Nuget Package and press Enter.

Install-Package IronPdf

NuGet Package will be installed as shown below:

Install NuGet Package

Install NuGet Package


Print PDF Filed in C#

Now, let us consider an example to demonstrate how we can print a PDF file in C# using the IronPDF Library.

Open Program.cs file. Add the following namespace at the top of the Program.cs file using IronPDF:

Now, write the following Code inside the main function:

using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
            pdfDocument.Print();
            Console.WriteLine("File Printed Successfully!");
            Console.ReadKey();
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
            pdfDocument.Print();
            Console.WriteLine("File Printed Successfully!");
            Console.ReadKey();
Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
				pdfDocument.Print()
				Console.WriteLine("File Printed Successfully!")
				Console.ReadKey()
End Using
VB   C#

Let's understand the code.

The following line of code will load the PDF file into memory. "Sample.Pdf" is the file name and D Drive is my file location. You have to write a complete file path along with the file name.

using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");

The print() function will print the loaded file using your default printer.

Test the Program

Press Ctrl + F5 to run the program.

Console

Console


Print Multiple PDF Documents in C#

I will show you how to print multiple PDF documents in C# using IronPDF.

14 PDF files

14 PDF files

In this example I have 14 PDF documents which I need to print. I have named them as numbers 1 - 14. You can save all your files with an ID, or you can save the file location in a database. Extract the file Path and Filename from the database, and save them in an array or on a list. Iterate that array or list and get your all files printed.

Write the following code inside the Main Function:

for(int i = 1; i <= 14; i ++)
            {
                using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Print\"+i+".pdf");
                Console.WriteLine("Printing File: {0}.pdf",i);
                pdfDocument.Print();
                Console.WriteLine("{0}.pdf Printed Successfully!", i);
            }
            Console.ReadKey();
for(int i = 1; i <= 14; i ++)
            {
                using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Print\"+i+".pdf");
                Console.WriteLine("Printing File: {0}.pdf",i);
                pdfDocument.Print();
                Console.WriteLine("{0}.pdf Printed Successfully!", i);
            }
            Console.ReadKey();
For i As Integer = 1 To 14
				Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Print\" & i & ".pdf")
					Console.WriteLine("Printing File: {0}.pdf",i)
					pdfDocument.Print()
					Console.WriteLine("{0}.pdf Printed Successfully!", i)
				End Using
Next i
			Console.ReadKey()
VB   C#

I have used a for loop to generate Numbers from 1 to 14 according to my file name.

Run the Solution:

Press Ctrl + F5 to run the program.

Console

Console

Convert URL to PDF then Print it:

I will show you how to print a web page by parsing URLs to IronPDF. The library will create a PDF file by parsing HTML. We can print that PDF document using the print function. Let's consider an example.

Write the following code inside the main function:

IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
            Console.WriteLine("Priniting File");
            using PdfDocument Pdfdoument = 
Renderer.RenderUrlAsPdf("https://ironpdf.com/docs/questions/csharp-print-pdf/#advanced-printing");
            Pdfdoument.Print();
            Console.WriteLine("File Printed Successfully!");
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
            Console.WriteLine("Priniting File");
            using PdfDocument Pdfdoument = 
Renderer.RenderUrlAsPdf("https://ironpdf.com/docs/questions/csharp-print-pdf/#advanced-printing");
            Pdfdoument.Print();
            Console.WriteLine("File Printed Successfully!");
Dim Renderer As New IronPdf.HtmlToPdf()
			Console.WriteLine("Priniting File")
			Using Pdfdoument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/docs/questions/csharp-print-pdf/#advanced-printing")
				Pdfdoument.Print()
				Console.WriteLine("File Printed Successfully!")
			End Using
VB   C#

This program will parse "https://ironpdf.com/docs/questions/csharp-print-pdf/#advanced-printing" to PDF and the print() function will print the PDF using the default printer.

Let's run the program.

Run the Program:

Press Ctrl + F5 to run the solution.

Output displayed on screen

Output displayed on screen

The print() function will print the document using the default printer. There may be a situation where we do not want to use the default printer. In that case, IronPDF provides us with one line code to change the printer name.

Change Printer Name:

Let's use an example to help us understand further. Use the following code to change the default printer:

PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
            pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF";
            pdfDocument.Print();
            Console.WriteLine("File Printed Successfully!");
PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
            pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF";
            pdfDocument.Print();
            Console.WriteLine("File Printed Successfully!");
Dim pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
			pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF"
			pdfDocument.Print()
			Console.WriteLine("File Printed Successfully!")
VB   C#

You can see that I have added only one line of additional code to specify the printer. I have specified Microsoft Print to PDF for demonstration.

Run the Program:

Run the Program by pressing Ctrl + F5.

The following Dialogue Box will appear that will ask us to enter a file name to save because we are using Microsoft Print to PDF.

Specify the File name and click on the Save button. I have specified "SamplePrint.Pdf". The following console output will display:

Microsoft Print to PDF has printed a PDF file in my D Drive. Let's see it.

I hope this tutorial was helpful, interactive, and easy for you to understand. You can ask questions or give feedback on this tutorial in the comments section below.