PRODUCT COMPARISONS

PDFsharp vs iTextSharp (C# PDF Library Comparison)

Published February 18, 2024
Share:

Introduction

In the realm of software development, the ability to generate and manipulate PDF documents is a common requirement. Developers often rely on third-party libraries to simplify the process and enhance productivity.

Three popular choices for working with PDFs in the .NET ecosystem are PDFSharp, iTextSharp, and IronPDF.

In this article, we will delve into details of the installation processes, provide code examples for basic tasks, and ultimately assess these three PDF libraries to determine the best choice for your PDF-related needs.

PDFsharp

PDFsharp is an open-source library for creating and processing PDF documents in .NET core applications. Developed by Empira Software GmbH, PDFsharp is lightweight and straightforward for commercial use.

It is particularly well-suited for basic PDF generation tasks for free and academic projects and is often used when simplicity is a priority.

Installation of PDFsharp

Installing PDFsharp is a relatively straightforward process. The library is available as a NuGet package, making integration into your project a breeze.

To install PDFsharp via NuGet, open the NuGet Package Manager Console in Visual Studio and execute the following command:

Install-Package PdfSharp

This command will download and install the latest version of PDFsharp into your project.

PDFsharp Code Example

Let's look at a simple code example to create a basic PDF using PDFsharp:

using PdfSharp.Pdf;
class Program
{
    static void Main()
    {
        // Create a new PDF document
        PdfDocument document = new PdfDocument();
        // Add a page to the document
        PdfPage page = document.AddPage();
        // Obtain a graphics object to draw on the page
        XGraphics gfx = XGraphics.FromPdfPage(page);
        // Draw "Hello, PDFsharp!" on the page
        gfx.DrawString("Hello, PDFsharp!", new XFont("Arial", 12), XBrushes.Black,
            new XRect(10, 10, page.Width, page.Height), XStringFormats.TopLeft);
        // Save the document to a file
        document.Save("output.pdf");
        // Close the document
        document.Close();
    }
}
using PdfSharp.Pdf;
class Program
{
    static void Main()
    {
        // Create a new PDF document
        PdfDocument document = new PdfDocument();
        // Add a page to the document
        PdfPage page = document.AddPage();
        // Obtain a graphics object to draw on the page
        XGraphics gfx = XGraphics.FromPdfPage(page);
        // Draw "Hello, PDFsharp!" on the page
        gfx.DrawString("Hello, PDFsharp!", new XFont("Arial", 12), XBrushes.Black,
            new XRect(10, 10, page.Width, page.Height), XStringFormats.TopLeft);
        // Save the document to a file
        document.Save("output.pdf");
        // Close the document
        document.Close();
    }
}
Imports PdfSharp.Pdf
Friend Class Program
	Shared Sub Main()
		' Create a new PDF document
		Dim document As New PdfDocument()
		' Add a page to the document
		Dim page As PdfPage = document.AddPage()
		' Obtain a graphics object to draw on the page
		Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
		' Draw "Hello, PDFsharp!" on the page
		gfx.DrawString("Hello, PDFsharp!", New XFont("Arial", 12), XBrushes.Black, New XRect(10, 10, page.Width, page.Height), XStringFormats.TopLeft)
		' Save the document to a file
		document.Save("output.pdf")
		' Close the document
		document.Close()
	End Sub
End Class
VB   C#

This code snippet creates a PDF with a single page containing data and the text "Hello, PDFsharp!".

PDFsharp vs iTextSharp (C# PDF Library Comparison): Figure 1

iTextSharp

iTextSharp is a widely used and feature-rich library for working with PDFs in the .NET framework. Developed by iText Software with open source Java codebase, this library provides a comprehensive set of tools and libraries for creating, manipulating, and extracting content from PDF files and documents.

iTextSharp is known for its flexibility and extensive documentation.

Installation of iTextSharp

To install iTextSharp, you can use the NuGet Package Manager Console with the following command:

Install-Package itext7

This command will install the latest version of iTextSharp 7 into your project.

iTextSharp Code Example

Let's explore a basic code example using iTextSharp to create a simple PDF file:

using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
class Program
{
    static void Main()
    {
        // Create a new PDF document
        using (var writer = new PdfWriter("output.pdf"))
        {
            using (var pdf = new PdfDocument(writer))
            {
                // Create a document
                var document = new Document(pdf);
                // Add a paragraph with the text "Hello, iTextSharp!"
                document.Add(new Paragraph("Hello, iTextSharp!"));
            }
        }
    }
}
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
class Program
{
    static void Main()
    {
        // Create a new PDF document
        using (var writer = new PdfWriter("output.pdf"))
        {
            using (var pdf = new PdfDocument(writer))
            {
                // Create a document
                var document = new Document(pdf);
                // Add a paragraph with the text "Hello, iTextSharp!"
                document.Add(new Paragraph("Hello, iTextSharp!"));
            }
        }
    }
}
Imports System
Imports System.IO
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
Friend Class Program
	Shared Sub Main()
		' Create a new PDF document
		Using writer = New PdfWriter("output.pdf")
			Using pdf = New PdfDocument(writer)
				' Create a document
				Dim document As New Document(pdf)
				' Add a paragraph with the text "Hello, iTextSharp!"
				document.Add(New Paragraph("Hello, iTextSharp!"))
			End Using
		End Using
	End Sub
End Class
VB   C#

This code snippet achieves the same result as the PDFsharp example, creating a PDF document with the text "Hello, iTextSharp!".

PDFsharp vs iTextSharp (C# PDF Library Comparison): Figure 2

IronPDF: A Leading Contender

IronPDF is a powerful .NET library for creating, editing, and manipulating PDF documents. Developed by Iron Software, it stands out for its ease of use, comprehensive features, and excellent performance.

While it is a commercial library, the advantages it offers make it a strong contender in the PDF file manipulation landscape.

Installation of IronPDF

To install IronPDF, you need to obtain a license from the Iron Software website. Once you have a license, you can download the IronPDF package and integrate it into your project.

For NuGet installation, you can use the following command:

Install-Package IronPdf

Create PDF Document using IronPDF

Now, let's explore a code example using IronPDF to create and edit a PDF:

using IronPdf;
var htmlToPdf = new HtmlToPdf();
var pdf = htmlToPdf.RenderHtmlAsPdf("<html><body><h1>Hello, IronPDF!</h1></body></html>");
pdf.SaveAs("ironpdf_example.pdf");
using IronPdf;
var htmlToPdf = new HtmlToPdf();
var pdf = htmlToPdf.RenderHtmlAsPdf("<html><body><h1>Hello, IronPDF!</h1></body></html>");
pdf.SaveAs("ironpdf_example.pdf");
Imports IronPdf
Private htmlToPdf = New HtmlToPdf()
Private pdf = htmlToPdf.RenderHtmlAsPdf("<html><body><h1>Hello, IronPDF!</h1></body></html>")
pdf.SaveAs("ironpdf_example.pdf")
VB   C#

This code snippet utilizes the IronPDF library to convert HTML content into a PDF. It begins by importing the IronPDF namespace and creating an instance of the HtmlToPdf class.

The HtmlToPdf object is then used to render an HTML string, which in this case contains a simple heading tag saying "Hello, IronPDF!".

The resulting PDF document is saved to a file named "ironpdf_example.pdf" using the SaveAs method.

Overall, this concise code demonstrates the ease with which IronPDF allows developers to convert HTML content to PDF, making it a powerful tool for handling generate PDFs tasks in .NET applications.

PDFsharp vs iTextSharp (C# PDF Library Comparison): Figure 3

Choosing the Best Library

While PDFsharp and iTextSharp serve their purposes, IronPDF emerges as the best choice for several reasons:

  1. Ease of Use: IronPDF provides a clean and intuitive programmatic API, making it easy for developers of all skill levels to work with.
  2. Comprehensive Features: IronPDF offers an extensive feature set, covering a wide range of PDF manipulation tasks, including HTML to PDF conversion, PDF merging, and more.
  3. Performance: IronPDF excels in performance, ensuring efficiency even in resource-intensive operations.
  4. Documentation: The extensive and well-organized good documentation of IronPDF simplifies the learning process and assists developers in efficiently utilizing its features.
  5. Licensing: While IronPDF requires a commercial license, the advantages it offers, combined with responsive support, justify the investment for professional projects.

Conclusion

In conclusion, the choice between PDFsharp, iTextSharp, and IronPDF depends on the specific requirements of your project.

PDFsharp is suitable for simple tasks, iTextSharp is ideal for complex PDF operations with open-source considerations, and IronPDF stands out as the top choice for its ease of use, comprehensive features, and excellent performance, making it a worthwhile investment for commercial applications.

Consider the specific needs of your project and the features offered by each library to make an informed decision.

IronPDF offers robust support and well-organized documentation, simplifying the learning process for developers and enabling efficient utilization of its features.

IronPDF offers the best HTML to PDF generation features to know more about IronPDF visit here, for code examples of HTML to PDF visit the following link.

IronPDF offers a commercial license for production use but it also offers a trial license for new users to test the IronPDF functionality.

< PREVIOUS
iTextSharp Read PDF Alternatives (Developer Tutorial)
NEXT >
A Comparison of Splitting PDF in C# Between iTextSharp and IronPDF

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 11,308,499 View Licenses >