Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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#:
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 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
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:
To start using IronPDF, follow these steps to install it using the NuGet Package Manager in Visual Studio:
Open Visual Studio and create a new Console Application or open an existing one.
Switch to the "Browse" tab, search for "IronPDF," and click "Install."
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.
You can install PDFiumViewer via the NuGet Package Manager as well. Here's how:
Open your Visual Studio project and create a Windows Forms application.
Drag a button to the Form and name it "Print PDF".
In the "NuGet Package Manager" window, switch to the "Browse" tab, search for "PDFiumViewer" and click "Install."
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.
Once the installation is complete, you can start using PDFiumViewer for printing PDF files and other PDF-related tasks.
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)
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.
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.
The output saved is a pixel-perfect PDF document:
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
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.
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.
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.
9 .NET API products for your office documents