How to Print a PDF File Using PDFSharp
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# environment.
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.
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:
- Create or open a project in which you want to use the PDFsharp library.
- 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.
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.
- From the forum search results, choose the
PdfSharp
package and select the 1.3.0 version. - 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.
- 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 Adobe Acrobat Reader installed in your system. PDFsharp doesn't provide any function to print PDF files. 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)
{
// Set the path to the Adobe Acrobat Reader executable
PdfFilePrinter.AdobeReaderPath = @"C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe";
// Create a PdfFilePrinter object with the path to the PDF and printer name
PdfFilePrinter printer = new PdfFilePrinter(@"D:\Tutorial Project\PDFSharpPrintPDF\SamplePDF.pdf", "Microsoft Print To PDF");
try
{
// Print the PDF document
printer.Print();
}
catch (Exception ex) // Catch any exceptions during printing
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
internal class Program
{
static void Main(string[] args)
{
// Set the path to the Adobe Acrobat Reader executable
PdfFilePrinter.AdobeReaderPath = @"C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe";
// Create a PdfFilePrinter object with the path to the PDF and printer name
PdfFilePrinter printer = new PdfFilePrinter(@"D:\Tutorial Project\PDFSharpPrintPDF\SamplePDF.pdf", "Microsoft Print To PDF");
try
{
// Print the PDF document
printer.Print();
}
catch (Exception ex) // Catch any exceptions during printing
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Set the path to the Adobe Acrobat Reader executable
PdfFilePrinter.AdobeReaderPath = "C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe"
' Create a PdfFilePrinter object with the path to the PDF and printer name
Dim printer As New PdfFilePrinter("D:\Tutorial Project\PDFSharpPrintPDF\SamplePDF.pdf", "Microsoft Print To PDF")
Try
' Print the PDF document
printer.Print()
Catch ex As Exception ' Catch any exceptions during printing
Console.WriteLine("Error: " & ex.Message)
End Try
End Sub
End Class
- The
PdfFilePrinter.AdobeReaderPath
sets the path to the Acrobat Reader executable (Acrobat.exe) on your system. It tells thePdfFilePrinter
where to find the Acrobat application, which is needed for the printing process. Make sure to add the correct path, else it will not work. - 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 code attempts to print the PDF document using the
Print
method ofPdfFilePrinter
. It is enclosed in atry-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.
Install IronPDF NuGet Package
Installing IronPDF in your project is a very straightforward process.
- Launch Visual Studio 2022 and open your C# project or create a new one if you haven't already.
- Right-click on your project in the Solution Explorer. Then, select "Manage NuGet Packages..." from the context menu.
- In the NuGet Package Manager window, make sure you are in the "Browse" tab. Type "IronPDF" into the search bar.
- From the search results, find the
IronPdf
package offered by Iron Software, and click on it to select. 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.
- 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 a 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)
{
// Create a new PDF document from the specified file path
PdfDocument pdfDocument = new PdfDocument(@"D:\Tutorial Project\PDFSharpPrintPDF\SamplePDF.pdf");
// Print the PDF document using the default printer
pdfDocument.Print();
}
static void Main(string[] args)
{
// Create a new PDF document from the specified file path
PdfDocument pdfDocument = new PdfDocument(@"D:\Tutorial Project\PDFSharpPrintPDF\SamplePDF.pdf");
// Print the PDF document using the default printer
pdfDocument.Print();
}
Shared Sub Main(ByVal args() As String)
' Create a new PDF document from the specified file path
Dim pdfDocument As New PdfDocument("D:\Tutorial Project\PDFSharpPrintPDF\SamplePDF.pdf")
' Print the PDF document using the default printer
pdfDocument.Print()
End Sub
- The 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). ThePdfDocument
class represents a PDF document that you can work with using IronPDF. - After loading the PDF document, the
Print
method is called on thePdfDocument
instance. In the context of IronPDF, thePrint
method sends the PDF document to the default printer installed on the machine, initiating the printing process. - Assume that our default printer is Microsoft Print to PDF. In this case, the code above opens the Save file dialog during execution.
You can also specify a different printer using the following code:
// Set the printer name to a specific printer
pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "my Printer";
// Set the printer name to a specific printer
pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "my Printer";
' Set the printer name to a specific printer
pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "my Printer"
- This line configures the print job to use a specified printer other than the default one by setting the
PrinterName
property in thePrinterSettings
.
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 make 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.
Frequently Asked Questions
What is IronPrint?
IronPrint is Iron Software's new .NET printing library, offering compatibility across multiple platforms, such as Windows, macOS, Android, and iOS.
What are the main features of this PDF library?
IronPDF allows developers to create, read, modify, and convert PDF documents. It supports converting HTML content to PDF, adding watermarks, digital signatures, attachments, and interactive form fields.
Can PDFsharp print PDF files directly?
No, PDFsharp does not provide functions to print PDF files directly. It requires the use of Adobe Acrobat Reader to print PDF documents.
How do you install the PDFsharp library using NuGet?
In Visual Studio, right-click on your project's solution, select 'Manage NuGet Packages for Solution...', search for 'PDFsharp', choose the 1.3.0 version, and click 'Install'.
How do you print a PDF using PDFsharp?
To print a PDF with PDFsharp, use Adobe Acrobat Reader by setting up the AdobeReaderPath and creating a PdfFilePrinter object to handle the printing process.
How do you install this PDF library using NuGet?
In Visual Studio, right-click on your project, select 'Manage NuGet Packages...', search for 'IronPDF', select your project, and click 'Install'.
How do you print a PDF using this PDF library?
Create a PdfDocument instance with the PDF file path, then call the Print method to send the document to the default printer.
Can you specify a printer other than the default one using this PDF library?
Yes, you can specify a different printer by setting the PrinterName property in the PrinterSettings of the PdfDocument.
What are the advantages of using this PDF library over PDFsharp?
IronPDF offers a more modern and feature-rich solution for handling PDFs, including HTML-to-PDF conversion and a straightforward API, without needing external applications for printing.
Is this PDF library free for development?
Yes, IronPDF is free for development and provides a free trial for commercial use.