USING IRONPRINT

How to Print PDF Files in C# Silently

Published March 29, 2023
Share:

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.

IronPDF - C# PDF Library

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:

  • Load and Generate PDF files from different file formats
  • Save and Print PDF files using Default printer
  • Merge and Splitting PDF file
  • PDF Editor without Adobe Reader

Steps to silently Print PDF Documents in C# using IronPDF Library

To silently print PDF documents, first we need the following components to be installed on the local computer.

  1. Visual Studio - It is the official IDE for C# development and it must be installed on the computer. You can download and install from Visual Studio website.
  2. 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

      Visual Studio

      Visual Studio

    • Select the C# Console App and click on next

      New Project Dialog Box

      New Project Dialog Box

    • Now, type the name of your project, select the Location, and click next

      Web Forms

      Web Forms

    • Choose the latest .NET Framework for your application. We will use stable version 6.0.

      Additional Information

      Additional Information

    • Click on create, the console project is created and we are ready to print PDF documents programmatically.
  3. 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.

      • Click on Tools in Menu bar, or
      • Right click the project file in the Solution Explorer

        Solution Explorer

        Solution Explorer

        Project Menu > Manage NuGet Packages

        Project Menu > Manage NuGet Packages

      • Once it is opened, browse IronPDF in NuGet package manager and install it, as shown below:

        Install IronPDF from NuGet Package

        Install IronPDF from NuGet Package

    • Download the NuGet Package directly - Another way to download IronPDF is by navigating to the NuGet website and downloading the package directly. Here is the link https://www.nuget.org/packages/IronPdf/.
    • Download the IronPDF .DLL Library - IronPDF can also be downloaded directly from IronPDF website. Navigate to: IronPDF DLL download to install it. Reference the .DLL file in your project to use it.

Generate a PDF file and Print Job

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/")
VB   C#

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()
VB   C#

This Print method will send the PDF to the default printer for printing.

Advanced Printing Options

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()
VB   C#

Specify Printer Name

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
VB   C#

Set Printer Resolution

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()
VB   C#

PrintToFile Method

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);
VB   C#

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
VB   C#

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.

Summary

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.

< PREVIOUS
How to Print PDF File Without Dialog in C#
NEXT >
How to Print PDF Files in C# Without Using Adobe

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 10,762 View Licenses >