IronPDF vs iTextSharp: Reading PDF Files in C
IronPDF provides a modern C# API for reading PDFs with simpler syntax than iTextSharp. It supports text extraction, image parsing, and form data access, while iTextSharp requires AGPL licensing for commercial use, making IronPDF a preferred choice for enterprise applications.
PDF (Portable Document Format) is a widely used file format for sharing documents consistently and securely. Reading and manipulating such files in C# is common in various applications, such as document management systems and reporting tools. This article compares two popular libraries for reading PDF files in C#: IronPDF and iTextSharp (the latest .NET library iText).
IronPDF is a complete C# library from Iron Software that offers extensive features for working with PDF files. It allows you to create, edit, and manipulate PDF documents smoothly. IronPDF is known for its simplicity and ease of use, making it an excellent choice for quickly integrating PDF functionality into your applications. The library uses a Chrome rendering engine, ensuring accurate rendering and modern web standards support.
iTextSharp is another popular library for working with PDF files in C#. It has been widely used in the industry for many years. However, it's crucial to understand that iTextSharp's licensing changed to AGPL (Affero General Public License), which has significant implications for commercial applications. Under AGPL, if your application uses iTextSharp, you must make your entire application's source code available to users—a requirement often incompatible with proprietary software development. This licensing change has prompted many enterprises to seek alternatives like IronPDF, which offers commercial-friendly licensing.
How Do I Read PDFs Using IronPDF vs iTextSharp in C#?
- Create a new C# project in Visual Studio to compare IronPDF vs. iTextSharp for reading PDF files.
- Install IronPDF and iTextSharp libraries to your project via NuGet Package Manager.
- Read PDF files using IronPDF's intuitive API for text extraction.
- Read PDF files using iTextSharp's more complex object model.
What Are the Prerequisites for This Tutorial?
- Visual Studio: Ensure you have Visual Studio or any other C# development environment installed. IronPDF supports Windows, Linux, and macOS environments.
- NuGet Package Manager: Make sure you can use NuGet to manage packages in your project for advanced installation.
How Do I Set Up the Development Environment?
Begin by setting up a C# Console Application. Open Visual Studio and select Create a new project. Select Console Application type. For production applications, consider reviewing our guides on Azure deployment or AWS deployment if you're planning cloud-based PDF processing.
Provide your project name as shown below. Following .NET naming conventions, use PascalCase for your project name to maintain consistency with enterprise standards.
Select the required .NET version for your project. IronPDF supports .NET Framework, .NET Core, and .NET 5+, providing flexibility for both legacy and modern applications.
Once this is done, Visual Studio will generate a new project with the necessary structure for comparing PDF reading capabilities.
How Do I Install IronPDF and iTextSharp Libraries?
Which Package Manager Should I Use for iTextSharp?
You can install iTextSharp from the NuGet Package Manager for iText. The latest version is available as an iText package. Note the relatively lower download count compared to IronPDF, which reflects the licensing concerns many developers have with AGPL.
Or from the Visual Studio Package Manager as shown below. Search for iText in Package Manager and click Install. Be aware that accepting the AGPL license has legal implications for your project's distribution.
How Do I Install IronPDF via NuGet?
You can install IronPDF from the NuGet Package Manager for IronPDF as shown below. Notice the significantly higher download count (8.3M), indicating wider adoption in commercial applications.
Or from the Visual Studio package manager as shown below. Search for IronPDF: C# PDF Library in Package Manager and click Install. The installation process is straightforward and includes all necessary dependencies for Chrome rendering.
How Do I Read Text from PDFs Using IronPDF?
Add the below code to your Program.cs file and provide a sample PDF document with the specified content. IronPDF excels at extracting text from complex PDFs, including those with multiple columns, embedded fonts, and various encodings.
using IronPdf;
// Begin the comparison of IronPDF and iTextSharp for reading PDFs in C#
Console.WriteLine("Comparison of IronPDF And iTextSharp Read PDF Files in C#");
// Read PDF using IronPDF
ReadUsingIronPDF.Read();
public class ReadUsingIronPDF
{
public static void Read()
{
// Specify the path to the PDF document
string filename = "C:\\code\\articles\\ITextSharp\\ITextSharpIronPdfDemo\\Example.pdf";
// Create a PDF Reader instance to read the PDF
// IronPDF automatically handles various PDF versions and encryption
var pdfReader = PdfDocument.FromFile(filename);
// Extract all text from the PDF - maintains formatting and structure
var allText = pdfReader.ExtractAllText();
Console.WriteLine("------------------Text From PDF-----------------");
Console.WriteLine(allText);
Console.WriteLine("------------------Text From PDF-----------------");
// Extract all images from the PDF - supports various image formats
var allImages = pdfReader.ExtractAllImages();
Console.WriteLine("------------------Image Count From PDF-----------------");
Console.WriteLine($"Total Images = {allImages.Count()}");
// Save extracted images if needed (production example)
for (int i = 0; i < allImages.Count(); i++)
{
// allImages[i].SaveAs($"image_{i}.png");
}
Console.WriteLine("------------------Image Count From PDF-----------------");
// Iterate through each page to extract text from them
Console.WriteLine("------------------One Page Text From PDF-----------------");
var pageCount = pdfReader.PageCount;
for (int page = 0; page < pageCount; page++)
{
string text = pdfReader.ExtractTextFromPage(page);
Console.WriteLine($"Page {page + 1} content:");
Console.WriteLine(text);
}
// Additional IronPDF capabilities for production use
// Extract form data
var form = pdfReader.Form;
if (form != null)
{
foreach (var field in form.Fields)
{
Console.WriteLine($"Form Field: {field.Name} = {field.Value}");
}
}
// Access metadata
Console.WriteLine($"Author: {pdfReader.MetaData.Author}");
Console.WriteLine($"Title: {pdfReader.MetaData.Title}");
Console.WriteLine($"Created: {pdfReader.MetaData.CreationDate}");
}
}using IronPdf;
// Begin the comparison of IronPDF and iTextSharp for reading PDFs in C#
Console.WriteLine("Comparison of IronPDF And iTextSharp Read PDF Files in C#");
// Read PDF using IronPDF
ReadUsingIronPDF.Read();
public class ReadUsingIronPDF
{
public static void Read()
{
// Specify the path to the PDF document
string filename = "C:\\code\\articles\\ITextSharp\\ITextSharpIronPdfDemo\\Example.pdf";
// Create a PDF Reader instance to read the PDF
// IronPDF automatically handles various PDF versions and encryption
var pdfReader = PdfDocument.FromFile(filename);
// Extract all text from the PDF - maintains formatting and structure
var allText = pdfReader.ExtractAllText();
Console.WriteLine("------------------Text From PDF-----------------");
Console.WriteLine(allText);
Console.WriteLine("------------------Text From PDF-----------------");
// Extract all images from the PDF - supports various image formats
var allImages = pdfReader.ExtractAllImages();
Console.WriteLine("------------------Image Count From PDF-----------------");
Console.WriteLine($"Total Images = {allImages.Count()}");
// Save extracted images if needed (production example)
for (int i = 0; i < allImages.Count(); i++)
{
// allImages[i].SaveAs($"image_{i}.png");
}
Console.WriteLine("------------------Image Count From PDF-----------------");
// Iterate through each page to extract text from them
Console.WriteLine("------------------One Page Text From PDF-----------------");
var pageCount = pdfReader.PageCount;
for (int page = 0; page < pageCount; page++)
{
string text = pdfReader.ExtractTextFromPage(page);
Console.WriteLine($"Page {page + 1} content:");
Console.WriteLine(text);
}
// Additional IronPDF capabilities for production use
// Extract form data
var form = pdfReader.Form;
if (form != null)
{
foreach (var field in form.Fields)
{
Console.WriteLine($"Form Field: {field.Name} = {field.Value}");
}
}
// Access metadata
Console.WriteLine($"Author: {pdfReader.MetaData.Author}");
Console.WriteLine($"Title: {pdfReader.MetaData.Title}");
Console.WriteLine($"Created: {pdfReader.MetaData.CreationDate}");
}
}What Does the IronPDF Code Do?
- Create a Word Document: Initially, create a Word document with the desired text content and save it as a PDF document named
Example.pdf. - PDFReader Instance: The code creates a
PdfDocumentobject using the PDF file path to extract text and images. - Extract Text and Images: The
ExtractAllTextmethod is used to capture all text in the document, whileExtractAllImagesextracts images. - Extract Text Per Page: Text from each page is extracted using the
ExtractTextFromPagemethod.
What Output Should I Expect from IronPDF?
How Do I Read Text from PDFs Using iTextSharp?
Now to compare the text extraction from iTextSharp, add the below code to the same Program.cs file. For simplicity, we have not separated the classes into different files. Notice how iTextSharp requires more complex code for basic operations.
using IronPdf;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser.Listener;
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Kernel.Pdf.Xobject;
// Begin the comparison of IronPDF and iTextSharp for reading PDFs in C#
Console.WriteLine("Comparison of IronPDF And iTextSharp Read PDF Files in C#");
// Call method to read PDF using iTextSharp library
ReadUsingITextSharp.Read();
public class ReadUsingITextSharp
{
public static void Read()
{
// Specify the path to the PDF document
string pdfFile = "C:\\code\\articles\\ITextSharp\\ITextSharpIronPdfDemo\\Example.pdf";
// Create a PDF Reader instance - more verbose than IronPDF
PdfReader pdfReader = new PdfReader(pdfFile);
// Initialize a new PDF Document - additional step required
iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfReader);
// Extract text from all pages - more complex than IronPDF
Console.WriteLine("------------------Text From PDF (iTextSharp)-----------------");
for (int page = 1; page <= pdfDocument.GetNumberOfPages(); page++)
{
// Use a text extraction strategy to extract plain text from the PDF
LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
string pdfText = PdfTextExtractor.GetTextFromPage(pdfDocument.GetPage(page), strategy);
Console.WriteLine($"Page {page} content:");
Console.WriteLine(pdfText);
}
// Extract images - significantly more complex than IronPDF
Console.WriteLine("------------------Images From PDF (iTextSharp)-----------------");
int imageCount = 0;
for (int pageNum = 1; pageNum <= pdfDocument.GetNumberOfPages(); pageNum++)
{
var page = pdfDocument.GetPage(pageNum);
var resources = page.GetResources();
var xobjects = resources.GetResource(PdfName.XObject);
if (xobjects != null)
{
foreach (var key in xobjects.KeySet())
{
var xobject = xobjects.GetAsStream(key);
if (xobject != null)
{
var pdfObject = xobjects.Get(key);
if (pdfObject.IsStream())
{
var stream = (PdfStream)pdfObject;
var subtype = stream.GetAsName(PdfName.Subtype);
if (PdfName.Image.Equals(subtype))
{
imageCount++;
// Extracting the actual image requires additional complex code
}
}
}
}
}
}
Console.WriteLine($"Total Images Found: {imageCount}");
// Close the document - manual resource management required
pdfDocument.Close();
pdfReader.Close();
}
}using IronPdf;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser.Listener;
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Kernel.Pdf.Xobject;
// Begin the comparison of IronPDF and iTextSharp for reading PDFs in C#
Console.WriteLine("Comparison of IronPDF And iTextSharp Read PDF Files in C#");
// Call method to read PDF using iTextSharp library
ReadUsingITextSharp.Read();
public class ReadUsingITextSharp
{
public static void Read()
{
// Specify the path to the PDF document
string pdfFile = "C:\\code\\articles\\ITextSharp\\ITextSharpIronPdfDemo\\Example.pdf";
// Create a PDF Reader instance - more verbose than IronPDF
PdfReader pdfReader = new PdfReader(pdfFile);
// Initialize a new PDF Document - additional step required
iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfReader);
// Extract text from all pages - more complex than IronPDF
Console.WriteLine("------------------Text From PDF (iTextSharp)-----------------");
for (int page = 1; page <= pdfDocument.GetNumberOfPages(); page++)
{
// Use a text extraction strategy to extract plain text from the PDF
LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
string pdfText = PdfTextExtractor.GetTextFromPage(pdfDocument.GetPage(page), strategy);
Console.WriteLine($"Page {page} content:");
Console.WriteLine(pdfText);
}
// Extract images - significantly more complex than IronPDF
Console.WriteLine("------------------Images From PDF (iTextSharp)-----------------");
int imageCount = 0;
for (int pageNum = 1; pageNum <= pdfDocument.GetNumberOfPages(); pageNum++)
{
var page = pdfDocument.GetPage(pageNum);
var resources = page.GetResources();
var xobjects = resources.GetResource(PdfName.XObject);
if (xobjects != null)
{
foreach (var key in xobjects.KeySet())
{
var xobject = xobjects.GetAsStream(key);
if (xobject != null)
{
var pdfObject = xobjects.Get(key);
if (pdfObject.IsStream())
{
var stream = (PdfStream)pdfObject;
var subtype = stream.GetAsName(PdfName.Subtype);
if (PdfName.Image.Equals(subtype))
{
imageCount++;
// Extracting the actual image requires additional complex code
}
}
}
}
}
}
Console.WriteLine($"Total Images Found: {imageCount}");
// Close the document - manual resource management required
pdfDocument.Close();
pdfReader.Close();
}
}What Output Does iTextSharp Produce?
What Are the Limitations of iTextSharp?
- Learning Curve: Steeper learning curve, especially for beginners.
- Licensing: AGPL licensing requires open-sourcing your application.
- Complex API: Simple operations require multiple objects and manual management.
- Limited HTML Support: Minimal HTML rendering compared to IronPDF.
Manual Resource Management: You must explicitly close resources.
- Learning Curve: iTextSharp has a steeper learning curve, especially for beginners.
Licensing: iTextSharp's licensing model may not be suitable for all projects, especially those with budget constraints.
- Ease of Use: Straightforward API following .NET conventions.
- Document Rendering: Pixel-perfect rendering maintains original formatting.
- Commercial-Friendly Licensing: Transparent licensing without AGPL restrictions.
- Complete Features: Built-in support for forms, signatures, and annotations.
Better Performance: Improved for multi-threaded and large document processing.
- Ease of Use: IronPDF is known for its straightforward API, making it easy for developers to get started.
- Document Rendering: IronPDF provides accurate rendering of PDF documents, ensuring that the extracted text is faithful to the original.
For teams currently using iTextSharp, migrating to IronPDF is straightforward. Consider the following code example:
// Migration Example: Text Extraction
// iTextSharp (old way)
PdfReader reader = new PdfReader(filename);
PdfDocument doc = new PdfDocument(reader);
string text = PdfTextExtractor.GetTextFromPage(doc.GetPage(1));
doc.Close();
// IronPDF (new way)
var pdf = PdfDocument.FromFile(filename);
string text = pdf.ExtractTextFromPage(0); // 0-based indexing
// Migration Example: Form Field Reading
// iTextSharp (complex)
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, false);
IDictionary<string, PdfFormField> fields = form.GetFormFields();
foreach (var field in fields)
{
string value = field.Value.GetValueAsString();
}
// IronPDF (simple)
var form = pdf.Form;
foreach (var field in form.Fields)
{
string value = field.Value;
}// Migration Example: Text Extraction
// iTextSharp (old way)
PdfReader reader = new PdfReader(filename);
PdfDocument doc = new PdfDocument(reader);
string text = PdfTextExtractor.GetTextFromPage(doc.GetPage(1));
doc.Close();
// IronPDF (new way)
var pdf = PdfDocument.FromFile(filename);
string text = pdf.ExtractTextFromPage(0); // 0-based indexing
// Migration Example: Form Field Reading
// iTextSharp (complex)
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, false);
IDictionary<string, PdfFormField> fields = form.GetFormFields();
foreach (var field in fields)
{
string value = field.Value.GetValueAsString();
}
// IronPDF (simple)
var form = pdf.Form;
foreach (var field in form.Fields)
{
string value = field.Value;
}How Does Text Extraction Accuracy Compare?
IronPDF excels at handling complex PDFs that challenge other libraries:
- Scanned Documents: Better text flow when processing OCR-processed PDFs
- Multi-Column Layouts: Superior handling of newspaper-style layouts
- Encrypted Files: Automatic handling of password-protected PDFs
- Unicode Support: Full UTF-8 and international language support
- Embedded Fonts: Accurate extraction regardless of font embedding
How Do I Configure Licensing for IronPDF?
Insert your IronPDF license key into the appsettings.json file. For production deployments, consider using environment variables for secure key management.
{
"IronPdf.LicenseKey": "your license key",
"IronPdf.LoggingMode": "Custom",
"IronPdf.ChromeGpuMode": "Disabled"
}To receive a trial license, please provide your email at our licensing page. IronPDF offers flexible licensing options including development, staging, and production licenses.## Which Library Should I Choose for My Project?
Choosing between IronPDF and iTextSharp depends on your project's specific needs. For enterprise applications requiring commercial licensing, IronPDF is the preferred choice due to iTextSharp's restrictive AGPL license. If you need a straightforward and easy-to-use library for common PDF operations, IronPDF offers a superior developer experience with its intuitive API.
Consider these factors when making your decision:
- Licensing Requirements: AGPL vs commercial-friendly licensing
- API Complexity: Simple, intuitive API vs complex, low-level API
- Feature Set: IronPDF offers a full range of PDF manipulation features
- Performance: IronPDF provides better performance improvements
- Support: Active development and 24/5 technical support
IronPDF is designed to smoothly integrate PDF generation into your application, efficiently handling the conversion of formatted documents into PDFs. This approach provides clear benefits when you need to convert web forms, local HTML pages, and other web content to PDF using .NET. Your application can conveniently download, email, or store documents in the cloud. Whether you need to produce invoices, quotes, reports, contracts, or other professional documents, IronPDF's PDF Generation Capabilities have you covered. The library also supports advanced features like PDF compression, linearization for fast web view, and PDF/A compliance for long-term archival. Improve your application with IronPDF's intuitive and efficient PDF generation capabilities.
Frequently Asked Questions
How can I read PDF files in C#?
You can read PDF files using the IronPDF library by creating a PdfDocument instance and using methods like ExtractAllText and ExtractAllImages to extract content from the PDF.
What should I consider when choosing a PDF library for C#?
Consider factors such as ease of use, licensing, learning curve, and specific project requirements when choosing between libraries like IronPDF and iTextSharp for PDF manipulation in C#.
How can I install a PDF library in my C# project?
You can install IronPDF via the NuGet Package Manager in Visual Studio by searching for 'IronPDF: C# PDF Library' and clicking the 'Install' button.
What are the advantages of using IronPDF for PDF manipulation?
IronPDF offers ease of use, a straightforward API, and accurate document rendering, making it ideal for developers who need to quickly integrate PDF functionality into their applications.
Is there a difference in the complexity of using IronPDF and iTextSharp?
Yes, IronPDF is known for its simplicity, while iTextSharp offers more flexibility and extensibility, which may involve a steeper learning curve.
Can IronPDF convert HTML content to PDF?
Yes, IronPDF can seamlessly convert HTML content, such as web forms and pages, into PDF documents, facilitating tasks like downloading and emailing PDFs.
What are some limitations of using iTextSharp for PDF tasks?
iTextSharp may pose a steeper learning curve and its licensing model might not fit all project budgets, especially if you're looking for a straightforward solution.
How does IronPDF enhance application functionality?
IronPDF enables integration of PDF generation and manipulation features into applications, allowing for the conversion of web content to PDFs and handling of professional documents like invoices and reports.








