PRODUCT COMPARISONS

How to Print a PDF File Using PDFSharp

Published August 2, 2023
Share:

Printing functionality is a critical aspect of PDF manipulation libraries, allowing developers to generate high-quality printed documents directly from their applications. In this article, we will compare the printing capabilities of two popular libraries, IronPDF and PDFSharp, in the context of C# or .NET development. We'll explore the strengths and limitations of each library, shedding light on their approaches to printing PDF files.

PDFsharp

PDFsharp is the Open Source .NET library that easily creates and processes PDF documents on the fly from any .NET language program files. The same drawing routines can be used to create documents, draw on the screen, or send output to any arbitrary IP printer name.

IronPDF

IronPDF is a popular library for C# .NET that enables developers to work with PDF files in a versatile and efficient manner. It provides a comprehensive set of functionalities for creating, reading, modifying, and converting PDF documents, making it an all-in-one solution for PDF-related tasks. One of its key features is the ability to convert HTML content to PDF, making it ideal for generating PDFs from web pages and dynamic content. IronPDF also supports advanced features like adding watermarks, digital signatures, post attachments and interactive form fields to PDFs. With its easy-to-use API and robust performance, IronPDF has become a favored choice among developers for seamless PDF manipulation in the C#

How to Print PDF Files using PDFsharp?

PDFsharp does not provide any functions to print PDF files. However, we can use Acrobat with PDFsharp to print PDF documents. Please note that you will not be able to print PDF files using Acrobat in the latest version of PDFsharp. You need to install the 1.3.0 version of PDFsharp to print the PDF documents in C# .NET. You must have Adobe Reader installed on your machine. If you don't have an Adobe Reader installed, you can download and install one.

How to Print a PDF File Using PDFSharp: Figure 1 - Adobe Reader

Install PDFsharp Library

Installing the PDFsharp library is a straightforward process. PDFsharp is available as a NuGet package, which allows you to easily add it to your C# or .NET project. Here are the steps to install PDFsharp using NuGet

  1. Create or open a project in which you want to use the PDFsharp library.
  2. In Visual Studio, right-click on your project's solution in the Solution Explorer. Then, select "Manage NuGet Packages for Solution..." from the context menu.
  3. In the NuGet Package Manager, you'll see a "Browse" tab. Type "PDFsharp" into the search bar and press Enter. This will display a list of available PDFsharp packages.

    How to Print a PDF File Using PDFSharp: Figure 2 - PDFsharp

  4. From the forum search results, choose the PdfSharp package and select the 1.3.0 version.
  5. After selecting the package and version, click on the "Install" button to add it to your project. NuGet will take care of downloading and installing the PDFsharp library along with any dependencies.
  6. With PDFsharp installed in your project, you can start using its classes and methods to work with PDF documents in your C# or .NET code.

Print PDF Document using PDFsharp

PDF printing with PDFsharp is not a simple task. We need an Adobe Acrobat Reader installed in your system. PDFsharp doesn't provide any function to print PDF file. We will first start the Adobe Reader process using PDFsharp and then print the document using the Adobe Reader process.

The following line of code demonstrates the example of printing a PDF file.

internal class Program
{
    static void Main(string [] args)
    { 
        //C:\Program Files\Adobe\ Acrobat Reader exe path
        PdfFilePrinter.AdobeReaderPath = @"C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe";
        PdfFilePrinter printer = new PdfFilePrinter(@"D:\Tutorial Project\PDFSharpPrintPDF\SamplePDF.pdf", "Microsoft Print To PDF");

        try
        {
            printer.Print();
        }
        //exception ex
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}
internal class Program
{
    static void Main(string [] args)
    { 
        //C:\Program Files\Adobe\ Acrobat Reader exe path
        PdfFilePrinter.AdobeReaderPath = @"C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe";
        PdfFilePrinter printer = new PdfFilePrinter(@"D:\Tutorial Project\PDFSharpPrintPDF\SamplePDF.pdf", "Microsoft Print To PDF");

        try
        {
            printer.Print();
        }
        //exception ex
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		'C:\Program Files\Adobe\ Acrobat Reader exe path
		PdfFilePrinter.AdobeReaderPath = "C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe"
		Dim printer As New PdfFilePrinter("D:\Tutorial Project\PDFSharpPrintPDF\SamplePDF.pdf", "Microsoft Print To PDF")

		Try
			printer.Print()
		'exception ex
		Catch ex As Exception
			Console.WriteLine("Error: " & ex.Message)
		End Try
	End Sub
End Class
VB   C#

The PdfFilePrinter.AdobeReaderPath sets the path to the Acrobat Reader executable (Acrobat.exe) on your system. It tells the PdfFilePrinter where to find the Acrobat application, which is needed for the printing process. Make sure to add a correct path, else it will not work.

In the second line, a new PdfFilePrinter object is created. The constructor takes two arguments: The first argument is the path to the PDF file you want to print. The second argument is the name of the printer to which you want to send the print job ("Microsoft Print To PDF" in this case). You can replace this with the name of your desired printer.

The next code block attempts to print the PDF document using the Print method of PdfFilePrinter. It is enclosed in a try-catch block to handle any exceptions that may occur during the printing process. If an exception occurs, the error message will be displayed on the console.

When you run the program, Adobe Acrobat Reader will open and print the document.

How to Print a PDF File Using PDFSharp: Figure 3 - Save File dialog

Install IronPDF NuGet Package

Installing IronPDF in your project is a very straightforward process.

  1. Launch Visual Studio 2022 and open your C# project or create a new one if you haven't already.
  2. Right-click on your project in the Solution Explorer. Then, select "Manage NuGet Packages..." from the context menu.
  3. In the NuGet Package Manager window, make sure you are in the "Browse" tab. Type "IronPDF" into the search bar.
  4. From the search results, find the IronPdf package offered by Iron Software, and click on it to select.
  5. On the right side of the "NuGet Package Manager" window, you will see a list of projects in your solution. Choose the project(s) where you want to install the package. Typically, you will select the main project where you'll be working with PDF documents.

    How to Print a PDF File Using PDFSharp: Figure 4 - `IronPdf` package

  6. After selecting the project(s), click on the "Install" button to add the IronPdf package to your project(s). NuGet will download and install the package along with any required dependencies.

That's it! Now you have successfully installed the IronPDF NuGet package in your Visual Studio 2022 C# project, and you can start using its classes and methods to work with PDF documents.

Print a PDF Document using IronPDF

Printing PDF file using IronPDF is a quite simple task. Unlike PDFsharp, IronPDF provides a function to print the PDF document. IronPDF provides printing settings, and other required methods to print documents. We do not need to use any other process or third party library. The following line of code demonstrates the example of printing a PDF file using IronPDF.

static void Main(string [] args)
{
    PdfDocument pdfDocument = new PdfDocument(@"D:\Tutorial Project\PDFSharpPrintPDF\SamplePDF.pdf");
    pdfDocument.Print();
}
static void Main(string [] args)
{
    PdfDocument pdfDocument = new PdfDocument(@"D:\Tutorial Project\PDFSharpPrintPDF\SamplePDF.pdf");
    pdfDocument.Print();
}
Shared Sub Main(ByVal args() As String)
	Dim pdfDocument As New PdfDocument("D:\Tutorial Project\PDFSharpPrintPDF\SamplePDF.pdf")
	pdfDocument.Print()
End Sub
VB   C#

This first line creates a new instance of the PdfDocument class from IronPDF with a PDF document from the specified file path. The path points to the location of the PDF file you want to print ("SamplePDF.pdf" in this case). The PdfDocument class represents a PDF document that you can work with using IronPDF.

After loading the PDF document, the Print method is called on the PdfDocument instance. In the context of IronPDF, the Print method sends the PDF document to the default printer installed on the machine, initiating the process.

Assume that our default printer is Microsoft Print to PDF. In this case, the code above has opened the Save file dialog during execution.

How to Print a PDF File Using PDFSharp: Figure 5 - Save File

You can also specify a different printer using the following code.

pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "my Printer";
pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "my Printer";
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

It provides all the necessary printer settings to print the document.

Conclusion

IronPDF and PDFsharp are both powerful libraries for working with PDF files in C# or .NET. However, IronPDF offers a more modern and feature-rich solution, providing functionalities for creating, reading, modifying, and converting PDFs, along with support for HTML-to-PDF conversion. IronPDF's straightforward API and streamlined approach makes it easy for developers to work with PDFs directly within their applications. On the other hand, PDFsharp, while capable, requires an external application like Acrobat for certain tasks, such as printing PDFs, making it comparatively less convenient for handling the entire PDF workflow.

IronPDF is free for development and comes with a free trial for commercial use.

< PREVIOUS
PdfiumViewer Print PDF in C# (Alternative Tutorial)

Ready to get started? Version: 2024.10 just released

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