產品比較

使用 iTextSharp 與 IronPDF 在 C# 中從 PDF 中提取文本

發佈 2023年11月14日
分享:

在瞬息萬變的數字文件管理領域中,從 PDF 檔案中輕鬆提取資料的能力是支撐多種應用程式的基本任務。提取文字過程對全面數據分析、內容索引、商業用途及文字操作等目的而言至關重要。在眾多可用工具中,iTextSharp 是備受推崇的 C# 函式庫,成為從 PDF 檔案提取文字的出色解決方案。

在這篇全面的文章中,我們將深入探討使用 iTextSharp 的豐富功能,探討這個強大而多功能的解析函式庫,如何讓開發人員能夠使用 C# 程式語言有效地從 PDF 文件中提取文本內容。我們將解開基本方法、示範技術和最佳實踐,為開發人員提供有效利用 iTextSharp 進行文本提取所需的知識。我們也將在本文中討論並比較最優秀及最強大的 PDF 函式庫 IronPDF。

如何從 PDF C# 提取文字

  1. 下載用於從 PDF 中提取文字的 C# 庫。

  2. 通過實例化 PdfReader 對象加載現有的 PDF。

  3. 使用 GetTextFromPage 方法從 PdfDocument 對象中提取文字。

  4. 實例化 foreach 循環以遍歷各行文字。

  5. 使用 WriteLine 方法將各行文字寫入文件。

什麼是 IronPDF?

IronPDF, 作為.NET開發領域中著名且功能豐富的庫,革新了PDF的生成和操作。IronPDF為開發人員提供了全面的工具組,使其能夠無縫集成到C#應用程序中,從而輕鬆創建、修改和呈現PDF文件。憑藉其直觀的API和強大的功能,這個多功能庫開啟了從HTML、圖像和內容生成高品質PDF的世界。在本文中,我們將探討IronPDF的功能,深入了解其主要特點,並展示如何利用它高效處理相關的PDF任務。

iTextSharp Library

iTextSharp 是一個在使用 C# 進行 PDF 操作領域中著名且強大的庫,已經徹底改變了開發人員處理 PDF 文件的方式。它作為一個多功能且可靠的工具,方便創建、修改和從 PDF 文件中提取內容。iTextSharp 讓開發人員能夠生成高級 PDF,提取圖像,操作現有文件和提取數據,使其成為各種應用程式的首選解決方案。在本文中,我們將深入探討 iTextSharp 的功能和特點,探索如何在 C# 程序環境中有效地使用它來管理和操作 PDF。

安裝 IronPDF

安裝 IronPDF 是一個簡單的過程,以下是將 IronPDF 安裝並整合到您的 C# 項目中的步驟。

  1. 打開 Visual Studio,創建一個新項目或打開一個現有項目。

  2. 前往工具,從下拉選單中選擇 NuGet 套件管理器。

  3. 在新的側邊菜單中選擇 NuGet 套件管理器解決方案。

    使用 iTextSharp 或 IronPDF 在 C# 中從 PDF 提取文字 圖 1 - NuGet 套件管理器

  4. 在「NuGet 套件管理員」視窗中,選擇「瀏覽」標籤頁。

  5. 在搜尋欄中輸入「IronPDF」並按 Enter。

  6. 會出現 IronPDF 的列表,選擇最新版本並按安裝。

使用iTextSharp VS IronPDF在C#中從PDF提取文本 圖2 - IronPDF 安裝

就這樣,IronPDF 已安裝並可以在您的 C# 專案中使用。

安裝 iTextSharp 函式庫

安裝 iTextSharp PDF 函式庫與安裝 IronPDF 相同。重複之前解釋的所有步驟,只需在瀏覽窗口中搜索 "iTextSharp" 而不是 IronPDF,從包清單中選擇,然後點擊安裝以在您的項目中整合 iTextSharp PDF 函式庫。

使用 iTextSharp 與 IronPDF 在 C# 中從 PDF 提取文本 圖 3 - iTextSharp

使用 IronPDF 從 PDF 檔案中提取文字

IronPDF 提供從 PDF 檔案中提取文字的功能,您可以根據特定頁面自動提取文字,或者從所有 PDF 中提取文字。在下面的程式碼範例中,我們將看到如何從示例 PDF 文件的特定頁面提取文字。

using IronPdf;
using System;
using PdfDocument PDF = PdfDocument.FromFile("Watermarked.pdf");
string Text = PDF.ExtractTextFromPage(1);
Console.Write(Text);
using IronPdf;
using System;
using PdfDocument PDF = PdfDocument.FromFile("Watermarked.pdf");
string Text = PDF.ExtractTextFromPage(1);
Console.Write(Text);
Imports IronPdf
Imports System
Private PdfDocument As using
Private Text As String = PDF.ExtractTextFromPage(1)
Console.Write(Text)
VB   C#

上述程式碼在 C# 中使用 IronPDF 程式庫從 PDF 文件中提取文字並顯示在控制台中。首先,必要的命名空間被導入,包括 IronPDF 和 System。程式碼隨後使用 FromFile 方法將標題為 "Watermarked.pdf" 的 PDF 文件載入到一個 PdfDocument 對象中。接下來,它使用 ExtractTextFromPage 從 PDF 的第二頁提取文字並將其存儲在名為 Text 的字串變數中。最後,使用 Console.Write 在控制台中顯示提取的文字。

使用iTextSharp與IronPDF於C#中從PDF擷取文本 圖4 - 輸出

使用 iTextSharp 庫從 PDF 文件中提取文字

您也可以使用 iTextSharp 從 PDF 文件中提取文字,以下是一個使用 iTextSharp 庫的範例。

using System;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

namespace PDFApp2
{
    class Program
    {
        static void Main(string [] args)
        {
            string filePath = @"C:\Users\buttw\OneDrive\Desktop\highlighted PDF.pdf";
            string outPath = @"C:\Users\buttw\OneDrive\Desktop\name.txt";
            int pagesToScan = 2;

            string strText = string.Empty;
            try
            {
                PdfReader reader = new PdfReader(filePath);
                for (int page = 1; page <= pagesToScan; page++) 
                {
                    ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
                    strText = PdfTextExtractor.GetTextFromPage(reader, page, its);

                    strText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(strText)));
                    string [] lines = strText.Split('\n');
                    foreach (string line in lines)
                    {
                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(outPath, true))
                        {
                            file.WriteLine(line);
                        }
                    }
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
        }
    }
}
using System;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

namespace PDFApp2
{
    class Program
    {
        static void Main(string [] args)
        {
            string filePath = @"C:\Users\buttw\OneDrive\Desktop\highlighted PDF.pdf";
            string outPath = @"C:\Users\buttw\OneDrive\Desktop\name.txt";
            int pagesToScan = 2;

            string strText = string.Empty;
            try
            {
                PdfReader reader = new PdfReader(filePath);
                for (int page = 1; page <= pagesToScan; page++) 
                {
                    ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
                    strText = PdfTextExtractor.GetTextFromPage(reader, page, its);

                    strText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(strText)));
                    string [] lines = strText.Split('\n');
                    foreach (string line in lines)
                    {
                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(outPath, true))
                        {
                            file.WriteLine(line);
                        }
                    }
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
        }
    }
}
Imports Microsoft.VisualBasic
Imports System
Imports System.Text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.pdf.parser

Namespace PDFApp2
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			Dim filePath As String = "C:\Users\buttw\OneDrive\Desktop\highlighted PDF.pdf"
			Dim outPath As String = "C:\Users\buttw\OneDrive\Desktop\name.txt"
			Dim pagesToScan As Integer = 2

			Dim strText As String = String.Empty
			Try
				Dim reader As New PdfReader(filePath)
				For page As Integer = 1 To pagesToScan
					Dim its As ITextExtractionStrategy = New iTextSharp.text.pdf.parser.LocationTextExtractionStrategy()
					strText = PdfTextExtractor.GetTextFromPage(reader, page, its)

					strText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(strText)))
					Dim lines() As String = strText.Split(ControlChars.Lf)
					For Each line As String In lines
						Using file As New System.IO.StreamWriter(outPath, True)
							file.WriteLine(line)
						End Using
					Next line
				Next page
				reader.Close()
			Catch ex As Exception
				Console.Write(ex)
			End Try
		End Sub
	End Class
End Namespace
VB   C#

提供的程式碼是一個使用 iTextSharp 函式庫從特定頁面的 PDF 文件中提取文本並將其保存到文本文件的 C# 程式。首先,導入必要的命名空間,包括 System.TextiTextSharp.text.pdfiTextSharp.text.pdf.parser。程式指定了檔名、輸入 PDF 文件路徑、輸出文本文件路徑及要掃描的頁數。接著,利用 iTextSharp 的 PdfReader 來讀取 PDF 文件。對於每個指定頁面,它使用 iTextSharp 的新 LocationTextExtractionStrategy 來提取文本,將編碼轉換為 UTF-8。提取的文本被分割成行,新 StringBuilder 文本按正確的方向從 PDF 中工作。任何過程中遇到的異常都會被捕獲並顯示在主控台中。程式結束時關閉 PdfReader

在 C# 中使用 iTextSharp 與 IronPDF 從 PDF 提取文本 圖 5 - 使用 iTextSharp 提取文本

結論

iTextSharp 是一個強大且多功能的 C# 庫,徹底改變了 PDF 的操作方式,使內容創建、修改和提取變得無縫。其強大的功能使其成為開發人員的首選解決方案,讓他們能夠生成複雜的 PDF 並有效地管理 PDF 中的文本內容。此外,IronPDF,同樣是一個在 .NET 領域中顯著的庫,提供了一套全面的工具,用於 PDF 生成和圖像操作,增強了開發人員輕鬆從各種來源創建、修改和渲染高質量 PDF 的能力。在比較這兩個 PDF 庫時,IronPDF 由於其良好的文檔和易於使用的 API 而遙遙領先,這些 API 只需幾行代碼即可完成所有文本提取,而 iTextSharp 則需要撰寫冗長且複雜的代碼,並且需要深入了解庫和 C#。 IronPDF 及其功能,请访问此链接 這裡. 使用 IronPDF 提取文字的完整教程可以在這裡找到 連結. 有關IronPDF和iTextSharp的完整教學,請造訪以下網址 連結.

< 上一頁
iTextSharp 文件審查與 IronPDF 比較
下一個 >
IronPDF 與 Puppeteer C#:比較

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 10,993,239 查看許可證 >