如何使用IronOCR在C#中讀取PDF
IronOCR 只需一行代碼即可讓您使用 C# 從 PDF 檔案中擷取文字,支援所有 PDF 版本,並透過其基於 Tesseract 的引擎提供精確的 OCR 結果。
PDF 是 "Portable Document Format" 的縮寫。它是由 Adobe 開發的一種檔案格式,可保留任何來源文件的字型、影像、圖形和排版,而不受用來建立這些文件的應用程式和平台所影響。 PDF 文件通常用於以一致的格式共用和檢視文檔,而無需考慮開啟它們的軟體或硬體。 IronOCR 可處理各種版本的 PDF 文件,從較舊的 PDF 1.0 規格到最新的 PDF 2.0 標準。
快速開始:在幾秒鐘內OCR一個PDF文件使用IronOCR快速配置OCR,只需構建一個OcrPdfInput指向您的PDF,然後調用Read即可。 本範例示範使用 IronOCR 從 PDF 擷取文字。
-
1使用 IronOCR 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var result = new IronOcr.IronTesseract().Read(new IronOcr.OcrPdfInput("document.pdf", OcrContent: IronOcr.PdfContents.TextAndImages));Dim result = New IronOcr.IronTesseract().Read(New IronOcr.OcrPdfInput("document.pdf", OcrContent:=IronOcr.PdfContents.TextAndImages))Dim result = New IronOcr.IronTesseract().Read(New IronOcr.OcrPdfInput("document.pdf", OcrContent:=IronOcr.PdfContents.TextAndImages)) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronOCR,並使用 免費試用
最小工作流程(5 個步驟)
- 下載用於閱讀PDF的C#程式庫
- 準備要閱讀的 PDF 文件
- 使用 PDF 檔案路徑建立OcrPdfInput對象
- 使用
Read方法對匯入的 PDF 檔案執行 OCR 操作。 - 通過提供頁面索引列表來閱讀特定頁面
我怎樣才能閱讀整個PDF文件?
首先實例化IronTesseract類來執行OCR。 然後,利用'using'陳述式來創建一個OcrPdfInput對象,將PDF文件路徑傳遞給它。 最後,使用Read方法執行OCR。 此方法適用於掃描的PDF(基於圖像)和可搜索的PDF(基於文本),適合於從各種PDF類型中提取文本。
/* :path=/static-assets/ocr/content-code-examples/how-to/input-pdfs-read-pdf.cs */
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add PDF
using var pdfInput = new OcrPdfInput("Potter.pdf");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(pdfInput);
// Access the extracted text
string extractedText = ocrResult.Text;
System.Console.WriteLine(extractedText);/* :path=/static-assets/ocr/content-code-examples/how-to/input-pdfs-read-pdf.cs */
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add PDF
using var pdfInput = new OcrPdfInput("Potter.pdf");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(pdfInput);
// Access the extracted text
string extractedText = ocrResult.Text;
System.Console.WriteLine(extractedText);Imports IronOcr
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add PDF
Using pdfInput As New OcrPdfInput("Potter.pdf")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(pdfInput)
' Access the extracted text
Dim extractedText As String = ocrResult.Text
System.Console.WriteLine(extractedText)
End UsingImports IronOcr
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add PDF
Using pdfInput As New OcrPdfInput("Potter.pdf")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(pdfInput)
' Access the extracted text
Dim extractedText As String = ocrResult.Text
System.Console.WriteLine(extractedText)
End Using
在大多數情況下,無需指定DPI屬性。 然而,在構建OcrPdfInput時提供一個高DPI數值可以提高閱讀精確性。 預設 DPI 設定通常足以處理大多數標準 PDF 文件,但專業文件可能需要調整。
我應該什麼時候調整DPI設置?
在處理低解析度掃描文件或包含小文字的 PDF 時,DPI(每英吋點數)設定變得至關重要。 為獲得最佳效果,考慮在以下情況下調整DPI設置:
- 處理低於 200 DPI 的掃描文件
- 處理歷史或檔案 PDF
- 處理複雜的版面或小字體
- 遇到預設設定的精確度問題
對於大多數的 OCR 作業,建議使用 300 的 DPI,而對於具有非常小的文字或複雜細節的文件,則可能需要使用 600 DPI。
除了PDF之外,IronOCR還支持哪些文件格式?
IronOCR 為 PDF 以外的眾多檔案格式提供全面支援。 您可以處理多種格式的圖像,包括:
- JPEG/JPG 適用於標準照片
- PNG 適用於具有透明度的影像
- TIFF 適用於多頁文件
- BMP 用於未壓縮的影像
- 簡單圖形的 GIF
此外,IronOCR可以直接從內存中處理PDF流,適合於網絡應用和雲端服務。
處理PDF內容類型
在處理 PDF 時,您可以透過指定內容類型來優化效能。 PdfContents枚舉允許您針對特定內容:
// For PDFs that contain text (faster processing)
var textOnlyPdf = new OcrPdfInput("document.pdf", OcrContent: PdfContents.TextAndImages);
// For image-only PDFs (scanned documents)
var imageOnlyPdf = new OcrPdfInput("scanned.pdf", OcrContent: PdfContents.OnlyImages);
// For mixed content (default)
var mixedPdf = new OcrPdfInput("mixed.pdf", OcrContent: PdfContents.TextAndImages);// For PDFs that contain text (faster processing)
var textOnlyPdf = new OcrPdfInput("document.pdf", OcrContent: PdfContents.TextAndImages);
// For image-only PDFs (scanned documents)
var imageOnlyPdf = new OcrPdfInput("scanned.pdf", OcrContent: PdfContents.OnlyImages);
// For mixed content (default)
var mixedPdf = new OcrPdfInput("mixed.pdf", OcrContent: PdfContents.TextAndImages);' For PDFs that contain text (faster processing)
Dim textOnlyPdf = New OcrPdfInput("document.pdf", OcrContent:=PdfContents.TextAndImages)
' For image-only PDFs (scanned documents)
Dim imageOnlyPdf = New OcrPdfInput("scanned.pdf", OcrContent:=PdfContents.OnlyImages)
' For mixed content (default)
Dim mixedPdf = New OcrPdfInput("mixed.pdf", OcrContent:=PdfContents.TextAndImages)' For PDFs that contain text (faster processing)
Dim textOnlyPdf = New OcrPdfInput("document.pdf", OcrContent:=PdfContents.TextAndImages)
' For image-only PDFs (scanned documents)
Dim imageOnlyPdf = New OcrPdfInput("scanned.pdf", OcrContent:=PdfContents.OnlyImages)
' For mixed content (default)
Dim mixedPdf = New OcrPdfInput("mixed.pdf", OcrContent:=PdfContents.TextAndImages)如何讀取PDF文件中的特定頁面?
從 PDF 文件讀取特定頁面時,請指定要匯入的頁面索引編號。 要做到這一點,請在構建OcrPdfInput時將頁面索引列表傳遞給PageIndices參數。 請注意,頁面索引採用從零開始的編號。 在處理只有特定頁面包含相關資訊的大型文件時,此功能尤其有用。
using IronOcr;
using System.Collections.Generic;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Create page indices list
List<int> pageIndices = new List<int>() { 0, 2 };
// Add PDF
using var pdfInput = new OcrPdfInput("Potter.pdf", PageIndices: pageIndices);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(pdfInput);using IronOcr;
using System.Collections.Generic;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Create page indices list
List<int> pageIndices = new List<int>() { 0, 2 };
// Add PDF
using var pdfInput = new OcrPdfInput("Potter.pdf", PageIndices: pageIndices);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(pdfInput);Imports IronOcr
Imports System.Collections.Generic
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Create page indices list
Private pageIndices As New List(Of Integer)() From {0, 2}
' Add PDF
Private pdfInput = New OcrPdfInput("Potter.pdf", PageIndices:= pageIndices)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(pdfInput)Imports IronOcr
Imports System.Collections.Generic
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Create page indices list
Private pageIndices As New List(Of Integer)() From {0, 2}
' Add PDF
Private pdfInput = New OcrPdfInput("Potter.pdf", PageIndices:= pageIndices)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(pdfInput)為什麼頁碼從零開始?
零基索引是 C# 和大多數程式語言的標準慣例。 這表示第一頁是索引 0,第二頁是索引 1,以此類推。 這種與陣列索引的一致性使開發人員更容易以程式化的方式處理頁面集合。 從人類可讀的頁碼 (1, 2, 3...) 轉換為索引時,只需從頁碼中減去 1 即可。
我該如何讀取不連續的頁面?
使用 IronOCR 可以直接閱讀非連續頁面。 只需將所需的頁面索引以任何順序加入您的清單即可。 舉例來說
// Read pages 1, 3, 5, and 10 (using zero-based indices)
List<int> pageIndices = new List<int>() { 0, 2, 4, 9 };
// Or use LINQ for range-based selection
var evenPages = Enumerable.Range(0, 10).Where(x => x % 2 == 0).ToList();// Read pages 1, 3, 5, and 10 (using zero-based indices)
List<int> pageIndices = new List<int>() { 0, 2, 4, 9 };
// Or use LINQ for range-based selection
var evenPages = Enumerable.Range(0, 10).Where(x => x % 2 == 0).ToList();Imports System.Collections.Generic
Imports System.Linq
' Read pages 1, 3, 5, and 10 (using zero-based indices)
Dim pageIndices As New List(Of Integer)() From {0, 2, 4, 9}
' Or use LINQ for range-based selection
Dim evenPages = Enumerable.Range(0, 10).Where(Function(x) x Mod 2 = 0).ToList()Imports System.Collections.Generic
Imports System.Linq
' Read pages 1, 3, 5, and 10 (using zero-based indices)
Dim pageIndices As New List(Of Integer)() From {0, 2, 4, 9}
' Or use LINQ for range-based selection
Dim evenPages = Enumerable.Range(0, 10).Where(Function(x) x Mod 2 = 0).ToList()OCR 引擎將只處理指定的頁面,大幅提升大型文件的效能。
如果我指定了無效的頁碼,會怎樣?
如果您指定的頁面索引超過文件的頁數,IronOCR 將會產生異常。 在處理前實施錯誤處理或驗證頁數。 您可以在執行 OCR 之前檢查 PDF 的總頁數,以確保您的索引有效。
如何對PDF的特定區域進行OCR?
透過縮小閱讀範圍,可以顯著提高閱讀效率。 為了達到此目的,請指定需要閱讀的匯入 PDF 的精確區域。 在下面的程式碼範例中,IronOCR 只專注於擷取章節編號和標題。 此技術類似於為圖像定義OCR區域,能提高速度和準確性。
using IronOcr;
using IronSoftware.Drawing;
using System;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Specify crop regions
Rectangle[] scanRegions = { new Rectangle(550, 100, 600, 300) };
// Add PDF
using (var pdfInput = new OcrPdfInput("Potter.pdf", ContentAreas: scanRegions))
{
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(pdfInput);
// Output the result to console
Console.WriteLine(ocrResult.Text);
}using IronOcr;
using IronSoftware.Drawing;
using System;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Specify crop regions
Rectangle[] scanRegions = { new Rectangle(550, 100, 600, 300) };
// Add PDF
using (var pdfInput = new OcrPdfInput("Potter.pdf", ContentAreas: scanRegions))
{
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(pdfInput);
// Output the result to console
Console.WriteLine(ocrResult.Text);
}Imports IronOcr
Imports IronSoftware.Drawing
Imports System
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Specify crop regions
Private scanRegions() As Rectangle = { New Rectangle(550, 100, 600, 300) }
' Add PDF
Using pdfInput = New OcrPdfInput("Potter.pdf", ContentAreas:= scanRegions)
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(pdfInput)
' Output the result to console
Console.WriteLine(ocrResult.Text)
End UsingImports IronOcr
Imports IronSoftware.Drawing
Imports System
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Specify crop regions
Private scanRegions() As Rectangle = { New Rectangle(550, 100, 600, 300) }
' Add PDF
Using pdfInput = New OcrPdfInput("Potter.pdf", ContentAreas:= scanRegions)
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(pdfInput)
' Output the result to console
Console.WriteLine(ocrResult.Text)
End Using我如何確定正確的矩形坐標?

尋找正確的座標需要瞭解 PDF 的座標系統。 Rectangle構造函數需要四個參數:X(水平位置)、Y(垂直位置)、Width和Height。 所有測量單位均為像素。 具有尺規功能的 PDF 檢視器或除錯工具等工具可協助辨識精確的座標。 另外,也可以使用小幅度調整的試誤法來完善您的選擇範圍。
為了更精確地定義區域,您可以使用高亮文本進行調試功能來可視化處理中的區域。
我可以在一次操作中指定多個區域嗎?
是的,IronOCR 在單一 OCR 作業中支援多個區域。 只需將多個Rectangle對象添加到您的陣列中:
Rectangle[] scanRegions = {
new Rectangle(50, 50, 200, 100), // Header region
new Rectangle(50, 200, 500, 300), // Main content region
new Rectangle(50, 550, 200, 50) // Footer region
};Rectangle[] scanRegions = {
new Rectangle(50, 50, 200, 100), // Header region
new Rectangle(50, 200, 500, 300), // Main content region
new Rectangle(50, 550, 200, 50) // Footer region
};Imports System.Drawing
Dim scanRegions As Rectangle() = {
New Rectangle(50, 50, 200, 100), ' Header region
New Rectangle(50, 200, 500, 300), ' Main content region
New Rectangle(50, 550, 200, 50) ' Footer region
}Imports System.Drawing
Dim scanRegions As Rectangle() = {
New Rectangle(50, 50, 200, 100), ' Header region
New Rectangle(50, 200, 500, 300), ' Main content region
New Rectangle(50, 550, 200, 50) ' Footer region
}每個區域將單獨處理,並按指定的順序合併結果。
為什麼選擇特定區域的OCR而不是整頁OCR?
特定區域的 OCR 具備多項優點:
- 效能:處理較小區域的速度顯著加快
- 準確性:專注於特定區域可減少來自不相關內容的噪音
- 結構:更可靠地從表單和表格中提取數據
- 成本效率:較少的處理時間意味著較低的計算成本
這種方法在處理結構化文件(如發票、表單或報告等資料出現在可預測位置的文件)時特別有價值。 對於複雜的文件結構,探索閱讀文件中的表格以獲得專門的表格提取技術。
有哪些高級PDF OCR功能可用?
IronOCR 提供 PDF 處理的其他功能,這些功能超越了基本的文字擷取。 您可以從掃描文件中創建可搜索的PDF,同時保留原始版式並添加文本層以便搜索和複製。 該程式庫還支持多線程以更快地處理大型PDF集合。
對於希望在其.NET應用中開始使用OCR的開發人員,探索簡單的OCR範例提供了堅實的基礎,了解IronOCR的功能和最佳實踐。
處理複雜的PDF情境
在處理具有挑戰性的 PDF 文件時,IronOCR 提供了多種進階功能:
這些功能使 IronOCR 成為滿足企業級 PDF 處理需求的全面解決方案。
常見問題
如何使用IronOCR在C#中提取PDF中的文字?
要使用IronOCR從PDF中提取文字,您需要實例化IronTesseract類並使用OcrPdfInput來指定您的PDF檔案。然後,調用Read方法進行OCR,並訪問提取出的文字。
使用IronOCR處理掃描PDF的好處是什麼?
IronOCR藉由它的基於Tesseract的引擎,提供精確的掃描PDF的OCR結果,支持影像型和文字型的PDF。
IronOCR能處理不同版本的PDF嗎?
是的,IronOCR支援範圍廣泛的PDF版本,從較舊的PDF 1.0規範到最新的PDF 2.0標準,確保可靠的相容性。
調整IronOCR中的DPI設定如何影響文本提取?
在IronOCR中調整DPI設定可以增強OCR的準確性,特別是針對低解析度文件、複雜版面或小字體的文件,建議挑戰性文件設置為300或600 DPI。
除了PDF,IronOCR還可以處理哪些其他檔案格式?
IronOCR可以處理多種影像格式,包括JPEG、PNG、TIFF、BMP和GIF,以及來自記憶體的PDF流。
如何使用IronOCR讀取PDF的特定頁面?
要讀取PDF中的特定頁面,請在OcrPdfInput對象中使用PageIndices參數指定頁碼。這使得目標頁面處理更有效率。
相比整頁OCR,使用特定區域OCR的優勢是什麼?
特定區域OCR通過專注於相關區域來提高效能,增強準確性並減少來自不相關內容的干擾。這對於表格和結構化文件特別有用。
IronOCR為PDF提供了哪些進階OCR功能?
IronOCR提供創建可搜尋式PDF、多執行緒以加速處理、圖像預處理以及多語言支持等功能,以滿足複雜文件處理需求。
在IronOCR中如何處理無效頁碼?
如果在IronOCR中指定了無效的頁碼,它將拋出異常。建議實施錯誤處理或在處理之前驗證頁數。
IronOCR能處理PDF中的不連續頁面嗎?
可以,IronOCR可以通過在列表中指定所需的頁碼來處理不連續的頁面。這允許以任意順序選擇性處理頁面。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。