Zum Fußzeileninhalt springen
PRODUKTVERGLEICHE

iTextSharp Lesen von PDF-Alternativen (Entwicklertutorial)

PDF (Portable Document Format) is a widely used file format for sharing documents consistently and securely. Reading and manipulating such files in C# is a common requirement in various applications, such as document management systems, reporting tools, and more. In this article, we will compare two popular libraries for reading PDF files in C#: IronPDF and iTextSharp (the latest .NET library iText).

IronPDF is a comprehensive C# library from Iron Software that provides a wide range of features for working with PDF files. It allows developers to create, edit, and manipulate PDF documents seamlessly. IronPDF is known for its simplicity and ease of use, making it an excellent choice for developers who need to integrate PDF functionality into their applications quickly.

iTextSharp is another popular library for working with PDF files in C#. It has been around for quite some time and is widely used in the industry. iText provides a rich set of features for creating and manipulating PDF documents. It is known for its flexibility and extensibility, making it suitable for complex PDF-related tasks.

How to Use IronPDF Vs iTextSharp to Read PDFs in C#

  1. Create a new C# project in Visual Studio to compare IronPDF vs. iTextSharp for reading PDF files.
  2. Install IronPDF and iTextSharp libraries to the project.
  3. Read PDF files using IronPDF.
  4. Read PDF files using iTextSharp.

Prerequisites

  1. Visual Studio: Ensure you have Visual Studio or any other C# development environment installed.
  2. NuGet Package Manager: Make sure you can use NuGet to manage packages in your project.

Step 1: Create a new C# project in Visual Studio to compare IronPDF Vs iTextSharp read PDF files

Begin by setting up a C# Console Application. Open Visual Studio and select Create a new project. Select Console Application type.

iTextSharp Read PDF Alternatives (Developer Tutorial): Figure 1 - Console App

Provide the project name as shown below.

iTextSharp Read PDF Alternatives (Developer Tutorial): Figure 2 - Project Configuration

Select the required .NET version for the project.

iTextSharp Read PDF Alternatives (Developer Tutorial): Figure 3 - Framework

Once this is done, Visual Studio will generate a new project.

Step 2: Install IronPDF and iTextSharp libraries to the project

iTextSharp can be installed from the NuGet Package Manager for iText Package Manager. The latest version is available as an iText package.

iTextSharp Read PDF Alternatives (Developer Tutorial): Figure 4 - iText

Or from the Visual Studio Package Manager as shown below. Search for iText in Package Manager and click Install.

iTextSharp Read PDF Alternatives (Developer Tutorial): Figure 5 - NuGet Package Manager

IronPDF can be installed from the NuGet Package Manager for IronPDF as shown below.

iTextSharp Read PDF Alternatives (Developer Tutorial): Figure 6 - IronPDF

Or from the Visual Studio package manager as shown below. Search for IronPDF: C# PDF Library in Package Manager and click Install.

iTextSharp Read PDF Alternatives (Developer Tutorial): Figure 7 - Install IronPDF

Step 3: Read Text from a PDF file using IronPDF

Add the below code to the Program.cs file and provide a sample PDF document which has the specified content.

iTextSharp Read PDF Alternatives (Developer Tutorial): Figure 8 - PDF Input

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
        var pdfReader = PdfDocument.FromFile(filename);

        // Extract all text from the PDF
        var allText = pdfReader.ExtractAllText();
        Console.WriteLine("------------------Text From PDF-----------------");
        Console.WriteLine(allText);
        Console.WriteLine("------------------Text From PDF-----------------");

        // Extract all images from the PDF
        var allImages = pdfReader.ExtractAllImages();
        Console.WriteLine("------------------Image Count From PDF-----------------");
        Console.WriteLine($"Total Images = {allImages.Count()}");
        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(text);
        }
    }
}
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
        var pdfReader = PdfDocument.FromFile(filename);

        // Extract all text from the PDF
        var allText = pdfReader.ExtractAllText();
        Console.WriteLine("------------------Text From PDF-----------------");
        Console.WriteLine(allText);
        Console.WriteLine("------------------Text From PDF-----------------");

        // Extract all images from the PDF
        var allImages = pdfReader.ExtractAllImages();
        Console.WriteLine("------------------Image Count From PDF-----------------");
        Console.WriteLine($"Total Images = {allImages.Count()}");
        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(text);
        }
    }
}
Imports 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()

'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'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
'		var pdfReader = PdfDocument.FromFile(filename);
'
'		' Extract all text from the PDF
'		var allText = pdfReader.ExtractAllText();
'		Console.WriteLine("------------------Text From PDF-----------------");
'		Console.WriteLine(allText);
'		Console.WriteLine("------------------Text From PDF-----------------");
'
'		' Extract all images from the PDF
'		var allImages = pdfReader.ExtractAllImages();
'		Console.WriteLine("------------------Image Count From PDF-----------------");
'		Console.WriteLine(string.Format("Total Images = {0}", allImages.Count()));
'		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(text);
'		}
'	}
'}
$vbLabelText   $csharpLabel

Code Explanation

  1. Create a Word Document: Initially, create a Word document with the desired text content and save it as a PDF document named Example.pdf.
  2. PDFReader Instance: The code creates a PdfDocument object using the PDF file path to extract text and images.
  3. Extract Text and Images: The ExtractAllText method is used to capture all text in the document, while ExtractAllImages extracts images.
  4. Extract Text Per Page: Text from each page is extracted using the ExtractTextFromPage method.

Output

iTextSharp Read PDF Alternatives (Developer Tutorial): Figure 9 - Read PDF Using IronPDF Output

Step 3: Read Text from a PDF file using iTextSharp

Now to compare the read text from iTextSharp, add the below code to the same Program.cs file. For simplicity, we have not separated the classes into different files.

using IronPdf;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser.Listener;
using iText.Kernel.Pdf.Canvas.Parser;

// 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
        PdfReader pdfReader = new PdfReader(pdfFile);

        // Initialize a new PDF Document
        iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfReader);

        // Use a text extraction strategy to extract plain text from the PDF
        LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
        string pdfText = PdfTextExtractor.GetTextFromPage(pdfDocument.GetPage(1), strategy);

        // Display the extracted text
        Console.WriteLine(pdfText);
    }
}
using IronPdf;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser.Listener;
using iText.Kernel.Pdf.Canvas.Parser;

// 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
        PdfReader pdfReader = new PdfReader(pdfFile);

        // Initialize a new PDF Document
        iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfReader);

        // Use a text extraction strategy to extract plain text from the PDF
        LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
        string pdfText = PdfTextExtractor.GetTextFromPage(pdfDocument.GetPage(1), strategy);

        // Display the extracted text
        Console.WriteLine(pdfText);
    }
}
Imports IronPdf
Imports iText.Kernel.Pdf
Imports iText.Kernel.Pdf.Canvas.Parser.Listener
Imports iText.Kernel.Pdf.Canvas.Parser

' 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()

'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'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
'		PdfReader pdfReader = New PdfReader(pdfFile);
'
'		' Initialize a new PDF Document
'		iText.Kernel.Pdf.PdfDocument pdfDocument = New iText.Kernel.Pdf.PdfDocument(pdfReader);
'
'		' Use a text extraction strategy to extract plain text from the PDF
'		LocationTextExtractionStrategy strategy = New LocationTextExtractionStrategy();
'		string pdfText = PdfTextExtractor.GetTextFromPage(pdfDocument.GetPage(1), strategy);
'
'		' Display the extracted text
'		Console.WriteLine(pdfText);
'	}
'}
$vbLabelText   $csharpLabel

Output

iTextSharp Read PDF Alternatives (Developer Tutorial): Figure 10 - Read PDF using iTextSharp Output

iTextSharp Limitations

  1. Learning Curve: iTextSharp has a steeper learning curve, especially for beginners.
  2. Licensing: iTextSharp's licensing model may not be suitable for all projects, especially those with budget constraints.

IronPDF Benefits

  1. Ease of Use: IronPDF is known for its straightforward API, making it easy for developers to get started.
  2. Document Rendering: IronPDF provides accurate rendering of PDF documents, ensuring that the extracted text is faithful to the original.

Licensing (Free Trial Available)

Insert your IronPDF license key into the appsettings.json file.

"IronPdf.LicenseKey": "your license key"

To receive a trial license, please provide your email.

Conclusion

Choosing between IronPDF and iTextSharp depends on the specific requirements of your project. If you need a straightforward and easy-to-use library for common PDF operations, IronPDF might be the better choice. Consider factors like your application's complexity, budget, and the learning curve when making your decision. IronPDF is designed to seamlessly integrate PDF generation into your application, effortlessly handling the conversion of formatted documents into PDFs. This versatile tool allows you to convert web forms, local HTML pages, and other web content to PDF using .NET. Users 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. Elevate your application with IronPDF's intuitive and efficient PDF generation capabilities.

Hinweis:iText and iTextSharp are registered trademarks of their respective owner. This site is not affiliated with, endorsed by, or sponsored by iText or iTextSharp. All product names, logos, and brands are property of their respective owners. Comparisons are for informational purposes only and reflect publicly available information at the time of writing.

Häufig gestellte Fragen

Wie kann ich PDF-Dateien in C# lesen?

Sie können PDF-Dateien lesen, indem Sie die IronPDF-Bibliothek verwenden, indem Sie eine PdfDocument-Instanz erstellen und Methoden wie ExtractAllText und ExtractAllImages verwenden, um Inhalte aus dem PDF zu extrahieren.

Was sollte ich bei der Auswahl einer PDF-Bibliothek für C# beachten?

Berücksichtigen Sie Faktoren wie Benutzerfreundlichkeit, Lizenzierung, Lernkurve und spezifische Projektanforderungen, wenn Sie zwischen Bibliotheken wie IronPDF und iTextSharp für die PDF-Manipulation in C# wählen.

Wie kann ich eine PDF-Bibliothek in meinem C#-Projekt installieren?

Sie können IronPDF über den NuGet-Paket-Manager in Visual Studio installieren, indem Sie nach 'IronPDF: C# PDF Library' suchen und auf die Schaltfläche 'Installieren' klicken.

Was sind die Vorteile der Verwendung von IronPDF für die PDF-Manipulation?

IronPDF bietet Benutzerfreundlichkeit, eine unkomplizierte API und genaue Dokumentendarstellung, was es ideal für Entwickler macht, die schnell PDF-Funktionen in ihre Anwendungen integrieren müssen.

Gibt es einen Unterschied in der Komplexität der Verwendung von IronPDF und iTextSharp?

Ja, IronPDF ist bekannt für seine Einfachheit, während iTextSharp mehr Flexibilität und Erweiterbarkeit bietet, was eine steilere Lernkurve beinhalten kann.

Kann IronPDF HTML-Inhalte in PDF umwandeln?

Ja, IronPDF kann HTML-Inhalte, wie Webformulare und -seiten, nahtlos in PDF-Dokumente umwandeln, was Aufgaben wie das Herunterladen und Versenden von PDFs erleichtert.

Was sind einige Einschränkungen der Verwendung von iTextSharp für PDF-Aufgaben?

iTextSharp kann eine steilere Lernkurve aufweisen und sein Lizenzmodell passt möglicherweise nicht zu jedem Projektbudget, insbesondere wenn Sie nach einer unkomplizierten Lösung suchen.

Wie verbessert IronPDF die Anwendungsfunktionalität?

IronPDF ermöglicht die Integration von PDF-Generierung und -Manipulationsfunktionen in Anwendungen und ermöglicht die Umwandlung von Webinhalten in PDFs sowie die Bearbeitung professioneller Dokumente wie Rechnungen und Berichte.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen