Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
PDF document is a Portable Document Format which can store and transfer data in the requested format. It preserves the formatting of the data stored and allows its users to focus on other aspects of handling digital documents. Printing digital documents is a tedious task as data displays differently on different platforms. But sending data in PDF documents helps preserve the format for printing. However, printing PDF files programmatically can be a challenge for developers in C#. Thanks to IronPDF - C# PDF Library, it makes the process of printing PDF files extremely easy and hassle-free.
This article will explain how to silently print PDF documents in C# using the IronPDF Library.
Print
method to print PDF document with default settingsPrinterName
property to target the specific printerPrinterResolution
class in C#IronPDF is a C# .NET Library, which allows developers to create, read, and edit PDF documents. It is a top notch C# Library and prioritizes accuracy, ease of use, and speed. It is specially designed for C#, F#, & VB.NET and highly compatible with .NET 7, 6, 5, Core, Standard, or Framework. It helps generate PDFs from HTML for Web, Desktop and Console utilizing IronPDF's powerful Chromium engine.
Additionally, IronPDF allows the user to manipulate and edit PDFs, add headers and footers, extract text and images from PDFs with ease.
Some Important Features include:
To silently print PDF documents, first we need the following components to be installed on the local computer.
Create Project - To create a console application for PDF printing in C#, follow the steps using visual studio 2022:
Open Visual Studio and click on create a new project
Select the C# Console App and click on next
Now, type the name of your project, select the Location, and click next
Choose the latest .NET Framework for your application. We will use stable version 6.0.
Install IronPDF - There are 3 ways to download and install the IronPDF library. These are as follows:
Using Visual Studio - Visual Studio has NuGet Package Manager which helps to install NuGet packages in C# projects.
Right click the project file in the Solution Explorer
Once it is opened, browse IronPDF in NuGet package manager and install it, as shown below:
Here we will generate a PDF file from the URL. Creating a PDF file is easy and usually a two step process. The following code sample generates a PDF:
using IronPdf;
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
using IronPdf;
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
Imports IronPdf
Private Renderer As New ChromePdfRenderer()
Private Pdf As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
A PDF document object is created with the above code and it is ready for printing. Next, we will use the default printer to paper print PDF documents. The code is a one-liner and is as follows:
Pdf.Print();
Pdf.Print();
Pdf.Print()
This Print
method will send the PDF to the default printer for printing.
For silent printing, IronPDF provides various advanced printing options.
PdfDocument.GetPrintDocument
method is used, and the result is stored in System.Drawing.Printing.PrintDocument
object. The code is simple and as following:
//Remember to add assembly reference to System.Drawing.dll in project
System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument();
//Remember to add assembly reference to System.Drawing.dll in project
System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument();
'Remember to add assembly reference to System.Drawing.dll in project
Dim PrintPDF As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()
IronPDF also gives the opportunity to print to specify the printer. To specify the name, the PrinterSettings.PrinterName
property is used. First, we need to get the current PDF document object. The code sample goes as follows:
using (var printDocument = pdfDocument.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.Print();
}
using (var printDocument = pdfDocument.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.Print();
}
Using printDocument = pdfDocument.GetPrintDocument()
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
printDocument.Print()
End Using
Another cool feature is setting the printer resolution. We can control the number of pixels to be printed, displayed, depending on the output. The DefaultPageSettings.PrinterResolution
property of the PDF document can be used to set the resolution. Here is a very quick code sample:
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
printDocument.Print();
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
printDocument.Print();
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
printDocument.Print()
The PdfDocument
class provides PrintToFile
method which allows to print PDF to a file in C#. It takes the pathtofile
as an argument to print the file directly to that location without opening the printer dialog. The code is simple and as following:
printDocument.PrintToFile(“PathToFile”, false);
printDocument.PrintToFile(“PathToFile”, false);
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'printDocument.PrintToFile("PathToFile”, false);
The complete code example goes as follows:
using IronPdf;
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
using (var printDocument = pdfDocument.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
printDocument.Print();
}
using IronPdf;
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
using (var printDocument = pdfDocument.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
printDocument.Print();
}
Imports IronPdf
Private Renderer As New ChromePdfRenderer()
Private pdfDocument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
Using printDocument = pdfDocument.GetPrintDocument()
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
printDocument.Print()
End Using
When the code is executed, it converts a URL to a PDF document. Then to silently print a pdf document, the GetPrintDocument method is used. On successful compiling and executing the program files, a printer dialog box appears to save it as a PDF document. PDF is then saved using the printer name provided.
In this article, we closely looked at how to silently print a pdf document using IronPDF. IronPDF provides a lot of useful options while pdf printing. It can also keep track of printed pages and also allows you to print between the page range.
Silent printing along with other printing options makes IronPDF a standout Library in C# while working with PDFs.
IronPDF helps convert data from different formats to PDF and from PDF to different formats. It makes it easy for the developers to integrate PDF functionality in the application development process. Moreover, it does not require Adobe acrobat reader to view and edit PDF documents.
IronPDF is free for individual development and can be licensed for commercial use. It provides a free trial license to access and test out the full functionality of the library. You can check more details on this link.
9 .NET API products for your office documents