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
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!
After reading this article you will be able to perform the following tasks:
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 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. 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.
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
Write the following command in the console to Install IronPdf NuGet Package and press Enter.
Install-Package IronPrint
NuGet Package will be installed as shown below:
Install NuGet Package
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 to use IronPDF:
using IronPdf;
using IronPdf;
Imports IronPdf
Now, write the following code inside the Main
function:
using System; // Required for Console operations
class Program
{
static void Main()
{
// Load the PDF document from file
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
Console.WriteLine("File Printed Successfully!");
Console.ReadKey();
}
}
using System; // Required for Console operations
class Program
{
static void Main()
{
// Load the PDF document from file
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
Console.WriteLine("File Printed Successfully!");
Console.ReadKey();
}
}
Imports System ' Required for Console operations
Friend Class Program
Shared Sub Main()
' Load the PDF document from file
Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
' Print the loaded PDF document to the default printer
pdfDocument.Print()
' Print operation success message to console
Console.WriteLine("File Printed Successfully!")
Console.ReadKey()
End Using
End Sub
End Class
Let's understand the code.
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
End Using
Print()
function will print the loaded file using your default printer.Press Ctrl + F5 to run the program.
Console
I will show you how to print multiple PDF documents in C# using IronPDF.
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 all your files printed.
Write the following code inside the Main
function:
using System; // Required for Console operations
class Program
{
static void Main()
{
// Loop through each file number and print
for (int i = 1; i <= 14; i++)
{
// Load each PDF document from file
using PdfDocument pdfDocument = PdfDocument.FromFile($@"D:\Print\{i}.pdf");
// Inform which file is being printed
Console.WriteLine("Printing File: {0}.pdf", i);
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
Console.WriteLine("{0}.pdf Printed Successfully!", i);
}
Console.ReadKey();
}
}
using System; // Required for Console operations
class Program
{
static void Main()
{
// Loop through each file number and print
for (int i = 1; i <= 14; i++)
{
// Load each PDF document from file
using PdfDocument pdfDocument = PdfDocument.FromFile($@"D:\Print\{i}.pdf");
// Inform which file is being printed
Console.WriteLine("Printing File: {0}.pdf", i);
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
Console.WriteLine("{0}.pdf Printed Successfully!", i);
}
Console.ReadKey();
}
}
Imports System ' Required for Console operations
Friend Class Program
Shared Sub Main()
' Loop through each file number and print
For i As Integer = 1 To 14
' Load each PDF document from file
Using pdfDocument As PdfDocument = PdfDocument.FromFile($"D:\Print\{i}.pdf")
' Inform which file is being printed
Console.WriteLine("Printing File: {0}.pdf", i)
' Print the loaded PDF document to the default printer
pdfDocument.Print()
' Print operation success message to console
Console.WriteLine("{0}.pdf Printed Successfully!", i)
End Using
Next i
Console.ReadKey()
End Sub
End Class
I have used a for loop to generate Numbers from 1 to 14 according to my file name.
Press Ctrl + F5 to run the program.
Console
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:
using System; // Required for Console operations
class Program
{
static void Main()
{
// HTML to PDF Renderer
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// Inform about the printing operation
Console.WriteLine("Printing File");
// Render URL as PDF
using PdfDocument PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing");
// Print the PDF document
PdfDocument.Print();
// Print operation success message to console
Console.WriteLine("File Printed Successfully!");
}
}
using System; // Required for Console operations
class Program
{
static void Main()
{
// HTML to PDF Renderer
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// Inform about the printing operation
Console.WriteLine("Printing File");
// Render URL as PDF
using PdfDocument PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing");
// Print the PDF document
PdfDocument.Print();
// Print operation success message to console
Console.WriteLine("File Printed Successfully!");
}
}
Imports System ' Required for Console operations
Friend Class Program
Shared Sub Main()
' HTML to PDF Renderer
Dim Renderer As New IronPdf.HtmlToPdf()
' Inform about the printing operation
Console.WriteLine("Printing File")
' Render URL as PDF
Using PdfDocument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing")
' Print the PDF document
PdfDocument.Print()
' Print operation success message to console
Console.WriteLine("File Printed Successfully!")
End Using
End Sub
End Class
This program will parse https://ironpdf.com/how-to/print-pdf/#advanced-printing
to PDF and the Print()
function will print the PDF using the default printer.
Let's run the program.
Press Ctrl + F5 to run the solution.
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.
Let's use an example to help us understand further. Use the following code to change the default printer:
using System; // Required for Console operations
class Program
{
static void Main()
{
// Load the PDF document from file
PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
// Change to a different printer by name
pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Print the PDF document
pdfDocument.Print();
// Print operation success message to console
Console.WriteLine("File Printed Successfully!");
}
}
using System; // Required for Console operations
class Program
{
static void Main()
{
// Load the PDF document from file
PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
// Change to a different printer by name
pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Print the PDF document
pdfDocument.Print();
// Print operation success message to console
Console.WriteLine("File Printed Successfully!");
}
}
Imports System ' Required for Console operations
Friend Class Program
Shared Sub Main()
' Load the PDF document from file
Dim pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
' Change to a different printer by name
pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF"
' Print the PDF document
pdfDocument.Print()
' Print operation success message to console
Console.WriteLine("File Printed Successfully!")
End Sub
End Class
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 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.
IronPDF is a .NET Library that provides a simple way to read, create, manipulate, and print PDF files. It works with C# and VB .NET applications and is compatible with ASP .NET, ASP .NET core, MVC, Web Forms, and Web APIs on .NET and .NET core.
To print a PDF file using IronPDF in C#, you need to load the PDF document from a file using PdfDocument.FromFile method and then call the Print method on the loaded document.
Yes, you can print multiple PDF files at once by iterating through an array or list of file paths and calling the Print method for each file using IronPDF.
To convert a URL to a PDF and print it using IronPDF, use the HtmlToPdf.RenderUrlAsPdf method to create a PDF from the URL, and then call the Print method on the resulting PDF document.
Yes, you can specify a different printer by setting the PrinterName property of the PrinterSettings object in the GetPrintDocument method of the PdfDocument class.
IronPDF can be used with .NET Framework, .NET Core, and .NET 5 and 6 applications. It is compatible with Windows, macOS, Android, and iOS platforms.
You can find more information about getting started with IronPrint by visiting the documentation provided by Iron Software at their website.
PDF files are lightweight, supported by most operating systems, easily shared between devices, and maintain consistent formatting across different platforms, making them ideal for document sharing.
Yes, having a basic knowledge of C# programming is helpful for understanding and implementing the examples and functionalities of IronPDF.