PRODUCT COMPARISONS

PdfiumViewer Print PDF in C# (Alternative Tutorial)

Published November 13, 2023
Share:

PDF documents play a crucial role in various software applications, including generating invoices, displaying reports, and sharing information. When it comes to working with PDFs in C#, developers have multiple options. This article explores two popular libraries for printing PDFs using Microsoft Print in C#:

  • IronPDF
  • PDFiumViewer

Let's delve into their features, and ease of use, and compare their printing capabilities to help you make an informed decision for your next C# project.

IronPDF

Overview of IronPDF

IronPDF is a robust C# library designed for creating, manipulating, and processing PDFs effortlessly. It offers a wide range of features, making it a preferred choice among developers. IronPDF stands out for its ability to

  • Convert HTML, CSS, and images to PDF files.
  • Generate PDF file document from scratch for the user.
  • Edit existing PDFs.
  • Support various PDF document elements like images, text, tables, and forms.
  • Provide advanced functionalities such as digital signatures, watermarks, and encryption.
  • Enable silent printing without any third-party tools or libraries.
  • Offer a user-friendly interface and comprehensive documentation.

PDFiumViewer

Overview of PDFiumViewer

PDFiumViewer is another popular option for working with PDFs in C#. Built on top of the open-source PDFium project, it provides a .NET wrapper for its functionality. PDFiumViewer offers:

  • The ability to render PDFs and display them in Windows Forms applications.
  • Support for navigation, zooming, and text selection within PDF documents.
  • A straightforward integration process for Windows Forms projects.

Installing IronPDF

To start using IronPDF, follow these steps to install it using the NuGet Package Manager in Visual Studio:

  1. Open Visual Studio and create a new Console Application or open an existing one.

    PdfiumViewer Print PDF in C# (Alternative Tutorial) Figure 1 - New Project- PDFiumViewer Print PDF C#

  2. Right-click on the project in the Solution Explorer and select "Manage NuGet Packages."
  3. Switch to the "Browse" tab, search for "IronPDF," and click "Install."

    PdfiumViewer Print PDF in C# (Alternative Tutorial) Figure 2 - IronPDF Installation

With IronPDF successfully installed, we can begin using it for printing PDFs and other PDF document-related tasks. Before that, let's also install PDFiumViewer in our system.

Installing PDFiumViewer

You can install PDFiumViewer via the NuGet Package Manager as well. Here's how:

  1. Open your Visual Studio project and create a Windows Forms application.

    PdfiumViewer Print PDF in C# (Alternative Tutorial) Figure 3 - New Project

  2. Drag a button to the Form and name it "Print PDF".

    PdfiumViewer Print PDF in C# (Alternative Tutorial) Figure 4 - Print Button

  3. Right-click on the project in the Solution Explorer and choose "Manage NuGet Packages."
  4. In the "NuGet Package Manager" window, switch to the "Browse" tab, search for "PDFiumViewer" and click "Install."

    PdfiumViewer Print PDF in C# (Alternative Tutorial) Figure 5 - PDFiumViewer Installation

  5. Alternatively, to install the PDFium DLL, you can search for "PDFiumViewer.Native" 32-bit or 64-bit, depending on your OS requirements. This DLL is necessary to load PDF files or pages using PDFiumViewer in a Windows Forms application.

    PdfiumViewer Print PDF in C# (Alternative Tutorial) Figure 6 - PDFiumViewer.Native

Once the installation is complete, you can start using PDFiumViewer for printing PDF files and other PDF-related tasks.

Printing PDFs with IronPDF

Printing PDFs using IronPDF is straightforward. Here's the source code example demonstrating how to print a PDF file without a printer name:

using IronPdf;

var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://ironpdf.com");
pdf.Print(300, true);
using IronPdf;

var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://ironpdf.com");
pdf.Print(300, true);
Imports IronPdf

Private renderer = New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com")
pdf.Print(300, True)
VB   C#

In this code example, IronPDF efficiently renders a PDF from a URL and sends it to the default printer for printing. In this case, the string printer is "Microsoft Print to PDF". IronPDF also provides multiple printer settings. You print through a specific printer by setting parameters for the printer name, pages to print, print directly without user interaction, and much more. For more detailed information on advanced printing options, please visit the C# Print PDF Documents.

Output

On executing the project, the Print method displays a print dialog to save the file as PDF. If the default printer is set to the system or actual printer, the document will get printed directly.

PdfiumViewer Print PDF in C# (Alternative Tutorial) Figure 7 - Print Dialog

The output saved is a pixel-perfect PDF document:

PdfiumViewer Print PDF in C# (Alternative Tutorial) Figure 8 - PDF Output

Printing PDFs with PDFiumViewer

While PDFiumViewer excels in rendering and displaying PDFs, it doesn't provide native PDF printing capabilities. To print a PDF document using PDFiumViewer, you'll need to utilize additional third-party drawing tools or libraries. To print directly using PDFiumViewer, we need to use Microsoft's System.Drawing.Printing assembly along with PDFiumViewer library.

In the following code, first, we load the PDF using the PdfDocument method. Then, we create a printing object called printDocument using the CreatePrintDocument method, which comes from the System.Drawing.Printing namespace. Finally, we use the Print method to send the loaded PDF to the printer for printing.

using System.Drawing.Printing;
using PdfiumViewer;

private void btnPrintPDF_Click(object sender, EventArgs e)
{
    string doc = @"C:\assets\input.pdf"; // absolute path with filename
    var pdf = PdfDocument.Load(doc);
    var printDocument = pdf.CreatePrintDocument();
    printDocument.Print();
}
using System.Drawing.Printing;
using PdfiumViewer;

private void btnPrintPDF_Click(object sender, EventArgs e)
{
    string doc = @"C:\assets\input.pdf"; // absolute path with filename
    var pdf = PdfDocument.Load(doc);
    var printDocument = pdf.CreatePrintDocument();
    printDocument.Print();
}
Imports System.Drawing.Printing
Imports PdfiumViewer

Private Sub btnPrintPDF_Click(ByVal sender As Object, ByVal e As EventArgs)
	Dim doc As String = "C:\assets\input.pdf" ' absolute path with filename
	Dim pdf = PdfDocument.Load(doc)
	Dim printDocument = pdf.CreatePrintDocument()
	printDocument.Print()
End Sub
VB   C#

Note: PDFiumViewer requires a System.Windows.Forms assembly to work. Otherwise, it will throw an exception. This is because the PDFiumViewer library is designed to be used with Windows Forms applications. Make sure you perform this task in a valid Windows Forms application.

On executing the app, the Windows form is displayed with a "Print PDF" button. Upon clicking the button the print dialog is displayed. Save the document as a PDF file.

PdfiumViewer Print PDF in C# (Alternative Tutorial) Figure 9 - Save as PDF

The output is exact same as the input PDF file. If the printer settings had the physical printer name, then it would have been printed out on paper perfectly.

PdfiumViewer Print PDF in C# (Alternative Tutorial) Figure 10 - Output

Conclusion

IronPDF and PDFiumViewer both serve distinct purposes when it comes to working with PDFs. IronPDF offers a comprehensive set of features for creating, manipulating, and printing PDFs. Its ease of use and rich functionality make it a popular choice for .NET developers.

On the other hand, PDFiumViewer shines in rendering and displaying PDFs within Windows Forms applications. However, it lacks native PDF printing capabilities, which may require developers to implement additional solutions for printing data as shown in the above example.

The choice between IronPDF and PDFiumViewer depends on your specific project requirements. If you need access to a versatile library with robust PDF manipulation features, IronPDF is an excellent choice. On the other hand, if your focus is on displaying PDFs in a Windows Forms application, PDFiumViewer can fulfill that role.

IronPDF is a powerful PDF library for C# developers. It is free for development purposes, and commercial licenses start at $749 for a single developer. There is also a free trial with full features and support, so you can try it out before buying. You can download the software from here.

< PREVIOUS
A Comparison of Printing PDF in C# Between Itextsharp and IronPDF
NEXT >
How to Print a PDF File Using PDFSharp

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 12,281 View Licenses >