Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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 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.
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.
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
This code snippet creates a PDF with a single page containing data and the text "Hello, PDFsharp!".
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.
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.
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
This code snippet achieves the same result as the PDFsharp example, creating a PDF document with the text "Hello, iTextSharp!".
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.
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
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")
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.
While PDFsharp and iTextSharp serve their purposes, IronPDF emerges as the best choice for several reasons:
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.
9 .NET API products for your office documents