DEV Community

Cover image for iTextShartp (Itext 7) VS IronPDF C# PDF Library Comparison
Mehr Muhammad Hamza
Mehr Muhammad Hamza

Posted on

iTextShartp (Itext 7) VS IronPDF C# PDF Library Comparison

When it comes to handling PDFs in .NET applications, two popular libraries often come to mind: iTextSharp and IronPDF. Both have their own set of features, strengths, and limitations. This article will provide a detailed comparison of iTextSharp and IronPDF, covering various scenarios and topics, and will demonstrate which library is the better choice for many applications.

Overview

iTextSharp

iTextSharp is a .NET port of the popular Java PDF library iText. iTextSharp has now reached to end of Life and is replaced by iText also known as iText7. It is a powerful library for creating and manipulating PDF documents. It offers a wide range of features, including the ability to generate PDFs from scratch, modify existing PDFs, and extract content.

IronPDF

IronPDF is a .NET library designed to facilitate PDF generation and manipulation within C# and VB.NET applications. It allows developers to create, edit, and manipulate PDF documents programmatically with ease. IronPDF supports a wide range of features including HTML to PDF conversion, PDF merging and splitting, adding watermarks, and extracting text and images from PDF files. It's commonly used in web applications, desktop software, and cloud environments where PDF functionality is required.

Comparison

1. Installation and Setup

iTextSharp

iTextSharp (iText7) can be installed via NuGet. Here is a typical installation command:

Install-Package iText7
Enter fullscreen mode Exit fullscreen mode

Image description

IronPDF

IronPDF can also be installed via NuGet. Here is the command to Install IronPDF

Install-Package IronPdf
Enter fullscreen mode Exit fullscreen mode

Image description

2. Creating PDFs

iTextSharp

iTextSharp (iText7) requires a fair amount of code to create pdf documents. Here is an example:

PdfWriter writer = new PdfWriter("pdf_with_iText.pdf");
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
document.Add(new Paragraph("Hello, World!"));
document.Add(new Paragraph("PDF Created with iText7 (iTextSHarp)!"));
document.Close();
Enter fullscreen mode Exit fullscreen mode

The Output PDF is as:

Image description

IronPDF

IronPDF excels in HTML to PDF conversion, offering built-in support that simplifies the process:

 var renderer = new ChromePdfRenderer();
 var pdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1><p>PDF Created with IronPDF!</p>");
 pdfDocument.SaveAs("pdf_with_IronPDF.pdf");
Enter fullscreen mode Exit fullscreen mode

Image description
In the comparison between iText and IronPDF for creating PDF files, iText requires multiple steps to initialize a document and add content programmatically, which can be more complex. On the other hand, IronPDF offers simplicity with its HTML to PDF rendering capability, allowing for quick conversion of HTML content to PDF directly. This makes IronPDF a better choice for rapid development and straightforward PDF generation tasks.

3. HTML to PDF Conversion

iTextSharp:

iText Library does not natively support HTML to PDF conversion. We need to install an additional "PdfHTML" Library to achieve this.

Run the following command in the Package Manager Console to install the library

Install-Package itext7.pdfhtml 
Enter fullscreen mode Exit fullscreen mode

The following code will convert HTML to pdf

using (FileStream htmlSource = File.Open("index.html", FileMode.Open))
using (FileStream pdfDest = File.Open("html_to_pdf_withiText.pdf", FileMode.Create))
{
    ConverterProperties converterProperties = new ConverterProperties();
    HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);
}
Enter fullscreen mode Exit fullscreen mode

The Output file is as:

Image description

IronPDF

IronPDF excels in HTML to PDF conversion, offering built-in support that simplifies the process:

var renderer = new ChromePdfRenderer();
 var pdfDocument = renderer.RenderHtmlFileAsPdf("index.html");
 pdfDocument.SaveAs("HtmlToPDFWith_IronPDF.pdf");
Enter fullscreen mode Exit fullscreen mode

PDF Document is as:

Image description
In comparing the two approaches for converting HTML to PDF, iText requires additional setup and dependencies (itext7.pdfhtml library) for HTML conversion, which increases complexity. Meanwhile, IronPDF offers a straightforward solution with built-in HTML to PDF conversion capabilities, making it easier and more convenient to create PDFs directly from HTML files. This makes IronPDF advantageous for developers needing seamless integration of HTML content into PDF documents without the need for extra libraries or configurations.

4. Manipulating Existing PDFs

Adding Watermark with iText

iText provides comprehensive tools for manipulating existing PDFs, but the API can be complex. Here's an example of adding a watermark to an existing PDF:

 string inputFilePath = "pdf_with_iText.pdf";
 string outputFilePath = "add_watermark_with_iText.pdf";

 PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputFilePath), new PdfWriter(outputFilePath)); // document class
 PdfCanvas under = new PdfCanvas(pdfDoc.GetFirstPage().NewContentStreamBefore(), new PdfResources(), pdfDoc);
 PdfFont font = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(StandardFonts.HELVETICA));

 PdfCanvas over = new PdfCanvas(pdfDoc.GetFirstPage());
 over.SetFillColor(ColorConstants.BLACK);

 Paragraph paragraph = new Paragraph("This TRANSPARENT watermark is added from iText")
         .SetFont(font)
         .SetFontSize(15);
 over.SaveState();

 // Creating a dictionary that maps resource names to graphics state parameter dictionaries
 PdfExtGState gs1 = new PdfExtGState();
 gs1.SetFillOpacity(0.5f);
 over.SetExtGState(gs1);
 Canvas canvasWatermark3 = new Canvas(over, pdfDoc.GetDefaultPageSize())
         .ShowTextAligned(paragraph, 297, 450, 1, iText.Layout.Properties.TextAlignment.CENTER, iText.Layout.Properties.VerticalAlignment.TOP, 0);
 canvasWatermark3.Close();
 over.RestoreState();

 pdfDoc.Close();
Enter fullscreen mode Exit fullscreen mode

The Output PDF is as:

Image description

Adding Watermark with IronPDF

IronPDF simplifies this process with higher-level methods:

var pdf = PdfDocument.FromFile("pdf_with_IronPDF.pdf");
pdf.ApplyWatermark("This TRANSPARENT watermark is added from IronPDF");
pdf.SaveAs("watermark_with_IronPDF");
Enter fullscreen mode Exit fullscreen mode

The output pdf file is as:

Image description
In comparison, iText requires manual handling of PDF content streams and graphics states to add a transparent watermark, involving lower-level operations and specific positioning of text. On the other hand, IronPDF provides a simpler and more intuitive method for applying transparent watermarks directly to PDF documents, abstracting the complexities of PDF manipulation and enhancing developer productivity in PDF-related tasks. This makes IronPDF a preferred choice for developers seeking ease of use and efficiency in PDF watermarking operations.

5. Extracting Text and Images

iText

Extracting text and images from PDFs using iText involves parsing the PDF content stream, which can be complex. The source code is as:

StringBuilder text = new StringBuilder();
PdfDocument pdfDoc = new PdfDocument(new PdfReader("html_to_pdf_withiText.pdf"));
for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
{
    text.Append(PdfTextExtractor.GetTextFromPage(pdfDoc.GetPage(i)));
}
pdfDoc.Close();
Console.WriteLine(text.ToString());
Enter fullscreen mode Exit fullscreen mode

The Extracted Text is printed on the Console.

Image description

IronPDF

IronPDF provides straightforward methods for extracting text:

 var pdf = PdfDocument.FromFile("HtmlToPDFWith_IronPDF.pdf");
 Console.WriteLine(pdf.ExtractAllText());
Enter fullscreen mode Exit fullscreen mode

The Extracted Text is Printed on the Console.

Image description
In comparison, iText requires explicit iteration through PDF pages and manual extraction of text using its PdfTextExtractor, which involves managing PDF document objects and extracting text from each page individually. Conversely, IronPDF simplifies text extraction with a straightforward method called (ExtractAllText()), providing a more streamlined approach that abstracts the complexities of PDF parsing and enhances developer productivity in handling PDF content retrieval tasks.

6. Printing PDF Documents

Print PDF Documents using itextSharp:

iTextSharp (iText7) provides capabilities for generating PDF documents programmatically, but it does not directly handle printing functionalities. Implementing printing from iText-generated PDFs typically requires integrating with additional libraries or platforms that support printing.

Print PDF Documents using IronPDF:

IronPDF offers built-in support for printing PDF documents directly from within .NET applications. This feature simplifies the process of printing PDFs without requiring external dependencies or complex setup, making it convenient for applications that need to generate and print documents seamlessly.

PdfDocument pdf = new PdfDocument("HtmlToPDFWith_IronPDF.pdf");
pdf.Print();
Enter fullscreen mode Exit fullscreen mode

This will send the document to the default printer. In my case, it is 'Microsoft Print to PDF', so it will prompt to choose a path to save the file, as shown below

Image description

Performance and Reliability

iText

iText is known for its reliability and robustness, especially in enterprise environments. However, it can be more challenging to work with due to its lower-level API.

IronPDF

IronPDF focuses on ease of use and rapid development. It provides a more intuitive API and better support for modern web technologies like HTML and CSS. This makes it an excellent choice for developers who need to quickly implement PDF functionality without delving into complex PDF internals.

Licensing

iText7 (iTextSharp)

iTextSharp is available under the AGPL license, which requires that your application be open source if you distribute it. A commercial license is available if you need to use it in a closed-source project.

IronPDF

IronPDF is a commercial product with various licensing options, including a free trial. It does not have open-source licensing restrictions, making it suitable for commercial applications.

Conclusion

While both iTextSharp and IronPDF are powerful tools for handling PDFs in .NET, IronPDF offers several advantages that make it a better choice for many developers:

Ease of Use: IronPDF provides a more straightforward API, reducing the amount of code needed for common tasks.

HTML to PDF Conversion: IronPDF excels in converting HTML to PDF, a feature that requires additional effort with iTextSharp.

High-Level Functions: IronPDF offers higher-level functions for manipulating PDFs, and simplifying tasks like adding watermarks and extracting text.

Commercial Licensing: IronPDF's commercial licensing model is more flexible for closed-source projects.

For developers looking for a quick, efficient, and powerful way to handle PDFs in their .NET applications, IronPDF is often the best choice. It is ideal for integrating PDF handling into existing projects or moving existing projects to incorporate robust PDF functionalities seamlessly.

Top comments (0)