USING IRONPRINT

How to Print PDF File Without Dialog in C#

Updated September 13, 2023
Share:

PDF (Portable Document Format) is the go-to choice globally as it preserves its formatting across all platforms. One essential aspect of working with PDFs is the ability to print them seamlessly from within a C# application. This article explores the significance of direct PDF printing and demonstrates how IronPDF, a robust C# library, can facilitate this process.

IronPDF - .NET Framework PDF Library

IronPDF is a powerful C# library designed to facilitate the creation, manipulation, and interaction with PDF documents. With IronPDF, developers can effortlessly generate PDFs from HTML content, images, and other sources, making it a versatile tool for both simple and complex PDF-related tasks. Its capabilities extend beyond mere PDF document generation; IronPDF also empowers you to merge, split, and manipulate PDFs in various ways. Moreover, its intuitive API makes it accessible to both beginners and experienced developers.

The Importance of Direct PDF Printing

When working with PDFs, printing is a fundamental requirement. Printing PDFs directly from a C# application without presenting a print dialog offers several advantages. It enhances user experience by eliminating unnecessary interaction, automates printing tasks, and enables seamless integration into larger workflows. IronPDF streamlines this process, allowing developers to maintain control over the printing process while ensuring the integrity of the printed output.

Prerequisites

Before moving on to the PDF printing process, ensure that you have the following prerequisites in place:

  1. Visual Studio: It is an integrated development environment (IDE) where you can create, edit, and compile your C# projects. To download and install it on your computer, visit the official Visual Studio website.
  2. IronPDF: The IronPDF library, which can be easily integrated into your project using NuGet Package Manager.

Create a Visual Studio Project

Creating a Visual Studio console project is a straightforward process. Follow these steps to create a new console application using Visual Studio:

  1. Open Visual Studio: Launch your Visual Studio IDE.
  2. Create a New Project: Once Visual Studio is open, click on "Create a new project".
  3. Choose Project Template: In the "Create a new project" window, you'll see a list of project templates. Select Visual C# console application.

    How to Print PDF File Without Dialog in C#: Figure 1 - Create a new C# Console App project in Visual Studio.

  4. Configure Project Details: After selecting the template, you'll be prompted to configure the project details.

    How to Print PDF File Without Dialog in C#: Figure 2 - Configure your new project. Specify the project details such as the Project name, Location, Solution name. Then, click on Next.

  5. Configure Additional Settings: Choose the .NET Framework that has long-term support. IronPDF supports the latest version of .NET Framework.
  6. Create Project: Once you've configured the project details, click the "Create" button. Visual Studio will create the project and open it in the IDE.

Installing IronPDF via NuGet

You can refer to the following steps to install IronPDF in your project:

  1. Open Visual Studio and your project.
  2. Go to the "Tools" menu and choose "NuGet Package Manager," then click on "Manage NuGet Packages for Solution."

    How to Print PDF File Without Dialog in C#: Figure 3 - Open your project in Visual Studio. Go the Tools menu and choose NuGet Package Manager, then click on Manage NuGet Packages for Solution.

  3. In the "Browse" tab, search for "IronPDF" in the search box.

    How to Print PDF File Without Dialog in C#: Figure 4 - In the NuGet-Solution window, select the Browse tab and enter IronPDF in the search box. Click on the `IronPdf` package from the search result, select it for your project and click on the Install button.

  4. Click on the IronPdf package and select it for your project, then click the "Install" button.

IronPDF for Printing without Dialog Box - Step-by-Step Process

Importing IronPdf Namespace

The code begins by importing the IronPdf and System.Drawing.Printing namespaces, which allow the use of classes and methods from the IronPDF library, and drawing and printing options from the System namespace.

using IronPdf;
using System.Drawing.Printing;
using IronPdf;
using System.Drawing.Printing;
Imports IronPdf
Imports System.Drawing.Printing
VB   C#

Creating PDF Document with ChromePDFRenderer

The ChromePdfRenderer is responsible for rendering the web page. The PdfDocument class represents the PDF document and provides methods to perform various operations on it, including printing. The following code generates a PDF file from the IronPDF website URL (https://ironpdf.com/):

ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
Dim Renderer As New ChromePdfRenderer()
Dim pdfDocument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
VB   C#

In the above code sample, the first line initializes an instance of ChromePdfRenderer from the IronPDF library, responsible for converting web pages to PDF format. The second line uses this instance to create a PdfDocument by rendering content from a specified URL, enabling further PDF-related actions.

Print PDF Files without Dialog

pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf")
VB   C#

The above line of code initiates the printing process for the pdfDocument using the specified print resolution (DPI), sending it to the "Microsoft Print to PDF" printer, which is normally the default printer if no printer is installed, and saving the printed output as a PDF file named "printedfile1.pdf" in the files directory. You can change the printer name and file location as per your needs.

The PDF file is printed pixel perfect:

How to Print PDF File Without Dialog in C#: Figure 5 - Output image of the PDF file named printedfile1.pdf. The PDF file displays the IronPDF web page that is printed pixel perfect.

Silent Printing with Advanced Printing Options

To have more control over printing PDF files programmatically, have a look at the following code snippet with advanced options:

using (var printDocument = pdfDocument.GetPrintDocument())
{
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
    printDocument.PrinterSettings.PrintToFile = true;
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    printDocument.Print();
}
using (var printDocument = pdfDocument.GetPrintDocument())
{
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
    printDocument.PrinterSettings.PrintToFile = true;
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    printDocument.Print();
}
Using printDocument = pdfDocument.GetPrintDocument()
	printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
	printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf"
	printDocument.PrinterSettings.PrintToFile = True
	printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
		.Kind = PrinterResolutionKind.Custom,
		.X = 1200,
		.Y = 1200
	}

	printDocument.Print()
End Using
VB   C#

In the above code snippet, IronPDF facilitates advanced PDF printing customization. It generates a PrintDocument from a PdfDocument, allowing customization of printer settings, printer name, output file name, and resolution. Setting the PrintFileName is important, as it allows you to bypass the print dialog for printing. The code then triggers printing using the print method, directing the print PDF document content to a specified printer, i.e., "Microsoft Print to PDF" in this case. Finally, saving the output as a PDF file. This enables seamless, dialog-free PDF printing with tailored settings.

You can also specify a page range to print a PDF document without a printer dialog and Adobe Acrobat Reader, all by incorporating IronPDF into your project. For more detailed information on printing, visit the code examples page.

Output

After executing the project, the two output printed PDF files are generated in the specified folder without any user interaction. You can also compare the size difference with advanced options used.

How to Print PDF File Without Dialog in C#: Figure 6 - Image displaying the two printed PDF files printedfile1.pdf and printedfile2.pdf successfully generated in the specified folder.

Conclusion

Printing a PDF file directly from a C# application simplifies document processing and enhances the user experience. IronPDF, with its array of PDF manipulation tools, empowers developers to print PDF documents seamlessly. By integrating IronPDF into your C# project, you can take full advantage of its features.

IronPDF is a commercial library renowned for its robust PDF handling capabilities. It offers a free trial period, enabling users to test and experience its features. Following the trial, a license can be acquired for continued usage. To get started, you can download the product from the official IronPDF website.

< PREVIOUS
C# Print PDF to Specific Printer (Code Example Tutorial)
NEXT >
How to Print PDF Files in C# Silently

Ready to get started? Version: 2024.5 just released

Start Free Trial Total downloads: 3,233
View Licenses >