跳過到頁腳內容
使用 IRONOCR

如何 OCR PDF:使用 C# .NET OCR PDF 從掃描的文件中萃取文字

掃描的 PDF 文件對 .NET 開發人員而言是一個常見的挑戰:文字僅以影像的形式存在,因此無法進行搜尋、複製或程式化處理。 光學字元識別 (OCR) 技術透過將掃描的影像和影像檔案轉換為可編輯和可搜尋的資料來解決這個問題 - 將掃描的紙本文件、數位相機擷取的影像或任何可搜尋的 PDF 檔案轉換為機器可讀的文字。 無論是將紙張檔案數位化、自動化資料擷取,或是建立由 AI 驅動的文件處理應用程式,使用光學字元識別轉換 PDF 檔案的能力都是不可或缺的。 IronOCR for .NET 是一個功能強大的 .NET OCR 函式庫,提供以 C# 語言進行 PDF OCR 的簡化方法。 此 .NET 光學字元識別函式庫以具備更高準確度的 Tesseract OCR 引擎為基礎,僅需幾行程式碼即可從任何 PDF 文件中擷取文字。

IronOCR是一個功能強大的 .NET OCR 函式庫,可在 C# 中提供簡化的 PDF OCR 方法。 此 .NET 光學字元識別函式庫以具備更高準確度的 Tesseract OCR 引擎為基礎,僅需幾行程式碼即可從任何 PDF 文件中擷取文字。

如何在 C# 中對 PDF 執行 OCR?

首先,透過 NuGet Package Manager 安裝 IronOCR 函式庫,將這個功能強大的 OCR 引擎加入您的系統:

Install-Package IronOcr

以下範例示範如何載入 PDF 檔案並辨識整個掃描文件中的文字:

using IronOcr;
// Initialize the OCR engine
IronTesseract ocr = new IronTesseract();
// Load the PDF and perform OCR
using var pdfInput = new OcrPdfInput("scanned-report.pdf");
OcrResult result = ocr.Read(pdfInput);
// Output the extracted text
string extractedText = result.Text;
Console.WriteLine(extractedText);
using IronOcr;
// Initialize the OCR engine
IronTesseract ocr = new IronTesseract();
// Load the PDF and perform OCR
using var pdfInput = new OcrPdfInput("scanned-report.pdf");
OcrResult result = ocr.Read(pdfInput);
// Output the extracted text
string extractedText = result.Text;
Console.WriteLine(extractedText);
Imports IronOcr

' Initialize the OCR engine
Dim ocr As New IronTesseract()

' Load the PDF and perform OCR
Using pdfInput As New OcrPdfInput("scanned-report.pdf")
    Dim result As OcrResult = ocr.Read(pdfInput)
    ' Output the extracted text
    Dim extractedText As String = result.Text
    Console.WriteLine(extractedText)
End Using
$vbLabelText   $csharpLabel

IronTesseract 類別作為主要的 OCR 引擎,包覆 Tesseract 5,並針對 .NET Core 和 .NET Framework 應用程式進行最佳化。 OcrPdfInput 物件在內部處理 PDF 載入和頁面渲染,省去了手動轉換圖像格式的需要。 當您呼叫 Read 方法時,OCR 程序會分析每個頁面,並傳回一個 OcrResult 包含以字串形式擷取的文字,以及有關段落、行、字及其位置的結構化資料。 然後使用者可以將輸出內容儲存至 TXT 檔案、目標資料夾、Word 文件,或使用 API 進一步處理資料。

輸入

如何 OCR PDF:使用 C# .NET OCR PDF 從掃描文件中提取文字:圖片 1 - PDF 輸入樣本

輸出

如何 OCR PDF:使用 C# .NET OCR PDF 從掃描文件中提取文字:圖片 2 - 控制台輸出

如何閱讀 PDF 中的特定頁面?

當您只針對需要的頁面時,處理大型文字文件會變得更有效率。 傳送頁面索引清單至 PageIndices 參數,以選擇性地轉換掃描的 PDF 頁面:

using IronOcr;
using System.Collections.Generic;
IronTesseract ocr = new IronTesseract();
// Specify pages to process (zero-based indexing)
List<int> targetPages = new List<int>() { 0, 2, 4 };
using var pdfInput = new OcrPdfInput("lengthy-document.pdf", PageIndices: targetPages);
OcrResult result = ocr.Read(pdfInput);
// Save or process the OCR results
Console.WriteLine(result.Text);
using IronOcr;
using System.Collections.Generic;
IronTesseract ocr = new IronTesseract();
// Specify pages to process (zero-based indexing)
List<int> targetPages = new List<int>() { 0, 2, 4 };
using var pdfInput = new OcrPdfInput("lengthy-document.pdf", PageIndices: targetPages);
OcrResult result = ocr.Read(pdfInput);
// Save or process the OCR results
Console.WriteLine(result.Text);
Imports IronOcr
Imports System.Collections.Generic

Dim ocr As New IronTesseract()
' Specify pages to process (zero-based indexing)
Dim targetPages As New List(Of Integer)() From {0, 2, 4}
Using pdfInput As New OcrPdfInput("lengthy-document.pdf", PageIndices:=targetPages)
    Dim result As OcrResult = ocr.Read(pdfInput)
    ' Save or process the OCR results
    Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

請注意 IronOCR 使用基於零的索引,因此第 0 頁代表 PDF 文件的第一頁。 在處理多頁掃描文件時,只有特定部分包含相關的可搜尋資料,這種選擇性的方法可減少處理時間和記憶體消耗。

如何從特定區域擷取資料?

發票處理、表單數位化和文件解析通常需要從定義的區域而非整頁提取文字。 此 OCR 工具允許您使用 ContentAreas 參數建立目標掃描,此參數接受一個矩形陣列,指定要處理的區域:

using IronOcr;
using IronSoftware.Drawing;
using System;
IronTesseract ocr = new IronTesseract();
// Define the scan region (x, y, width, height in pixels)
Rectangle[] invoiceFields = {
    new Rectangle(130, 290, 250, 50)   // Invoice number area
};
using var pdfInput = new OcrPdfInput("invoice.pdf", ContentAreas: invoiceFields);
OcrResult result = ocr.Read(pdfInput);
// Extract and output the structured data
Console.WriteLine(result.Text);
using IronOcr;
using IronSoftware.Drawing;
using System;
IronTesseract ocr = new IronTesseract();
// Define the scan region (x, y, width, height in pixels)
Rectangle[] invoiceFields = {
    new Rectangle(130, 290, 250, 50)   // Invoice number area
};
using var pdfInput = new OcrPdfInput("invoice.pdf", ContentAreas: invoiceFields);
OcrResult result = ocr.Read(pdfInput);
// Extract and output the structured data
Console.WriteLine(result.Text);
Imports IronOcr
Imports IronSoftware.Drawing
Imports System

Dim ocr As New IronTesseract()
' Define the scan region (x, y, width, height in pixels)
Dim invoiceFields As Rectangle() = {
    New Rectangle(130, 290, 250, 50)   ' Invoice number area
}

Using pdfInput As New OcrPdfInput("invoice.pdf", ContentAreas:=invoiceFields)
    Dim result As OcrResult = ocr.Read(pdfInput)
    ' Extract and output the structured data
    Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

Rectangle 建構器會接受四個參數:X 位置、Y 位置、寬度和高度 - 全部以像素為單位,從頁面左上角開始量起。 這種有針對性的文字識別方法可將 OCR 引擎集中在特定的內容區域,而非處理不相干的背景元素,因而大幅提升速度與精確度。 針對批次發票處理,結合區域萃取與結果頁迭代,從多個 PDF 檔案建立可編輯的結構化資料。

輸入

如何 OCR PDF:使用 C# .NET OCR PDF 從掃描文件中提取文字:圖片 3 - 樣本發票

輸出

 如何 OCR PDF:使用 C# .NET OCR PDF 從掃描文件中提取文字:圖片 4 - 擷取的資料輸出

如何提高掃描文件的 OCR 準確度?

現實世界中掃描過的紙本文件通常都會有品質問題:頁面歪斜、解析度低或掃描軟體產生的數位雜訊。 IronOCR 包括可解決這些挑戰的預處理篩選器,並協助將影像品質問題轉換為精確的文字轉換:

using IronOcr;
IronTesseract ocr = new IronTesseract();
using var input = new OcrInput();
// Load PDF with higher DPI for better text recognition
input.LoadPdf("poor-quality-scan.pdf", DPI: 300);
// Apply image correction filters to process scanned images
input.Deskew();   // Straighten rotated pages
input.DeNoise();  // Remove scanning artifacts
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
using IronOcr;
IronTesseract ocr = new IronTesseract();
using var input = new OcrInput();
// Load PDF with higher DPI for better text recognition
input.LoadPdf("poor-quality-scan.pdf", DPI: 300);
// Apply image correction filters to process scanned images
input.Deskew();   // Straighten rotated pages
input.DeNoise();  // Remove scanning artifacts
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Dim ocr As New IronTesseract()
Using input As New OcrInput()
    ' Load PDF with higher DPI for better text recognition
    input.LoadPdf("poor-quality-scan.pdf", DPI:=300)
    ' Apply image correction filters to process scanned images
    input.Deskew()   ' Straighten rotated pages
    input.DeNoise()  ' Remove scanning artifacts
    Dim result As OcrResult = ocr.Read(input)
    Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

DPI 參數可控制 PDF 頁面在 OCR 過程前所呈現的解析度 - 較高的值 (200-300 DPI) 可提高小文字文件的精確度。 Deskew 方法可自動偵測和糾正頁面旋轉,而 DeNoise 則可移除干擾光學字元識別 (OCR) 的斑點和雜訊。 對於需要額外影像調整的文件,IronOCR 提供對比強化、二進位以及其他編輯影像品質的工具。

此 .NET OCR 函式庫也可在輸入建構時接受憑證,以處理受到密碼保護的 PDF 文件。 本軟體支援 125 種以上的語言套件,可在國際文件上進行 OCR。 除了標準的 PDF 檔案之外,IronOcr 還能處理 PNG、TIFF (包括多頁 TIFF) 以及其他影像格式的檔案。 部署可在 Windows、Linux、macOS 以及包括 Azure 和 Docker 容器在內的雲端平台上無縫運作。

結論

IronOCR 將複雜的 PDF 文字擷取工作轉變為簡單直接的操作。 從基本的文件閱讀到針對具有挑戰性的掃描影像進行目標區域擷取與預處理,此 OCR 函式庫可處理複雜的技術,同時揭露可跨 .NET Core 與 .NET Framework 運作的簡潔 C# API。

上面的程式碼範例展示了核心功能,但 IronOCR 還進一步擴展了條碼和 QR 代碼讀取、可搜尋的 PDF 建立(將掃描的 PDF 檔案轉換為可編輯的可搜尋文件),以及結構化資料輸出(包括置信度分數和文字定位)。 探索進階實作的完整 API 參考 - 或在試用期間嘗試免費的專業版功能。

購買 License 以在 .NET 應用程式生產環境中部署 IronOCR,或與我們的工程團隊交談,以取得專案特定的指導。

準備好在您的 .NET 應用程式中執行 OCR 嗎? 從免費試用開始,探索完整的功能集並下載 SDK。

常見問題解答

什麼是 OCR,為什麼它對 .NET 開發人員很重要?

OCR 或「光學字元辨識」是一種將掃描影像和 PDF 檔案轉換為可編輯和可搜尋文字的技術。這對於需要以程式化的方式處理文件影像的 .NET 開發人員來說是非常重要的,可以實現搜尋和複製文字等功能。

IronOCR 如何增強 OCR 流程?

IronOCR 在 Tesseract OCR 引擎的基礎上增強了 OCR 流程,提供了更高的精確度,並簡化了從 C# 掃描文件中提取文字的方法。

IronOCR 可以直接處理 PDF 檔案以進行文字萃取嗎?

是的,IronOCR 可以直接處理 PDF 檔案,讓開發人員只需使用幾行 C# 程式碼即可從掃描的 PDF 文件中擷取文字。

IronOCR 可以處理哪些類型的文件?

IronOCR 可以處理各種文件,包括掃描的紙本文件、數位相機擷取的影像,以及可搜尋的 PDF 檔案,並將它們轉換為機器可讀的文字。

IronOCR 適合自動化資料擷取任務嗎?

絕對的,IronOCR 是自動化資料擷取工作的理想選擇,因為它可以將掃描的影像轉換成結構化、可編輯的資料,簡化工作流程並提高生產力。

使用 IronOCR 為人工智能驅動的文件處理應用程式提供了哪些優勢?

IronOCR 的優勢在於可將文件轉換為機器可讀取的文字,這對於建立需要文字辨識與分析能力的人工智能驅動文件處理應用程式而言,是不可或缺的。

在 C# 專案中實作 IronOCR 有多容易?

在 C# 專案中實作 IronOCR 非常簡單直接,只需要幾行程式碼即可整合其 OCR 功能,並開始從文件中擷取文字。

IronOCR 是否改進了 Tesseract OCR 引擎?

是的,IronOCR 建立在 Tesseract OCR 引擎的基礎上,增強了其準確性和效能,提供卓越的文字辨識結果。

IronOCR 可否用於紙本檔案的數位化?

是的,IronOCR 非常適合用於紙本檔案的數位化,因為它可以將掃描的紙本文件轉換為可搜尋、可編輯的數位文字,方便文件管理。

IronOCR 支援哪些編碼語言來實作 OCR?

IronOCR 支援 C# 語言的 OCR 實作,使其成為在 .NET Framework 內工作的開發人員的強大工具。

Kannaopat Udonpant
軟體工程師
在成為軟體工程師之前,Kannapat 完成了日本北海道大學的環境資源博士學位。在攻讀學位期間,Kannapat 也成為生物製造工程系車輛機器人實驗室的成員。2022 年,他利用自己的 C# 技能加入 Iron Software 的工程團隊,主要負責 IronPDF 的開發。Kannapat 非常重視他的工作,因為他可以直接向撰寫 IronPDF 使用的大部分程式碼的開發者學習。除了同儕學習之外,Kannapat 也很享受在 Iron Software 工作的社交生活。不寫程式碼或文件時,Kannapat 通常會用 PS5 玩遊戲或重看《最後的我們》。