如何使用IronOCR在C#中閱讀PDF
IronOCR允許您在C#中使用一行程式碼從PDF文件中提取文字,支持所有PDF版本,並通過其基於Tesseract的引擎提供準確的OCR結果。
PDF代表"可攜式文件格式"。這是一種由Adobe開發的檔案格式,可以保留任何源文件的字體、圖像、圖形和佈局,無論使用何種應用程式和平臺建立。 PDF文件通常用於以一致的格式共享和查看文件,而不考慮打開它們所使用的軟體或硬體。 IronOCR處理各種版本的PDF文件,從早期的PDF 1.0規範到最新的PDF 2.0標準。
快速入門:在幾秒內對PDF檔案進行OCR快速配置IronOCR的OCR通過構建一個Read。 此範例示範如何使用IronOCR從PDF中提取文字。
-
1Install IronOCR with NuGet Package Manager
-
2複製並運行這段程式碼片段。
using var result = new IronOcr.IronTesseract().Read(new IronOcr.OcrPdfInput("document.pdf", PdfContents.TextAndImages));C# -
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);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 Using
在大多數情況下,無需指定DPI屬性。 然而,在構造OcrPdfInput時提供較高的DPI數字可以提高閱讀準確性。 預設的DPI設置通常足以滿足大多數標準PDF文件的需求,但專業化的文件可能需要調整。
我什麼時候應該調整DPI設置?
DPI(每英寸點數)設置在處理低解析度的掃描文件或包含小文字的PDF時變得至關重要。 為獲得最佳效果,考慮調整DPI設置,當:
- 處理低於200 DPI的掃描文件
- 處理歷史或檔案性的PDF
- 處理複雜的佈局或小字體
- 使用預設設置遇到準確性問題
對於大多數OCR操作,建議使用300 DPI,而對於包含非常小文字或複雜細節的文件可能需要600 DPI。
IronOCR除了PDF之外還支持哪些文件格式?
IronOCR提供對多種文件格式的全面支持,不僅限於PDF。 您可以處理多種格式的圖像,包括:
- JPEG/JPG 用於標準照片
- PNG 用於具透明度的圖像
- TIFF 用於多頁文件
- BMP 用於未壓縮的圖像
- GIF 用於簡單圖形
此外,IronOCR可以直接從記憶體處理PDF流,適合網路應用和雲服務。
處理PDF內容型別
在處理PDF時,您可以通過指定內容型別來優化處理效能。 PdfContents枚舉允許您針對特定內容:
// For text-only PDFs (faster processing)
var textOnlyPdf = new OcrPdfInput("document.pdf", PdfContents.Text);
// For image-only PDFs (scanned documents)
var imageOnlyPdf = new OcrPdfInput("scanned.pdf", PdfContents.Images);
// For mixed content (default)
var mixedPdf = new OcrPdfInput("mixed.pdf", PdfContents.TextAndImages);' For text-only PDFs (faster processing)
Dim textOnlyPdf = New OcrPdfInput("document.pdf", PdfContents.Text)
' For image-only PDFs (scanned documents)
Dim imageOnlyPdf = New OcrPdfInput("scanned.pdf", PdfContents.Images)
' For mixed content (default)
Dim mixedPdf = New OcrPdfInput("mixed.pdf", PdfContents.TextAndImages)如何從PDF中讀取特定頁面?
在從PDF文件中讀取特定頁面時,指定要導入的頁面索引號。 要做到這一點,將頁面索引列表傳遞給構建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);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();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);
}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 Using如何確定正確的矩形座標?

尋找正確座標需要理解PDF的座標系統。 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
};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處理需求的綜合解決方案。
常見問題
我要如何在C#中從PDF文件中提取文字?
您可以使用IronOCR僅需一行程式碼就能從PDF文件中提取文字。只需建立一個IronTesseract實例,並使用OcrPdfInput的Read方法:`using var result = new IronOcr.IronTesseract().Read(new IronOcr.OcrPdfInput("document.pdf", PdfContents.TextAndImages));`。IronOCR可處理掃描PDF(基於圖像)和可搜尋PDF(基於文字)。
提取文字時,支持哪些PDF版本?
IronOCR支持所有PDF版本,從老舊的PDF 1.0規範到最新的PDF 2.0標準。OCR引擎基於Tesseract技術構建,確保無論您使用哪個PDF版本,都能精確提取文字。
我可以只讀取PDF中的特定頁面,而不是整個文件嗎?
可以,IronOCR允許您通過提供頁面索引來讀取PDF中的特定頁面,而不是處理整個文件。您可以使用OcrPdfInput物件指定要提取文字的頁面,從而提高大型文件的OCR效率。
對PDF文件進行OCR的最小工作流程是什麼?
使用IronOCR的最小工作流程包含5個步驟:1) 下載C#程式庫,2) 準備您的PDF文件,3) 使用PDF文件路徑建立OcrPdfInput物件,4) 使用Read方法進行OCR,5) 選擇性地指定頁面索引以供選擇性閱讀。
什麼時候應該調整PDF OCR的DPI設置?
雖然IronOCR的預設DPI設置對大多數標準PDF工作良好,但當處理低解析度掃描文件(低於200 DPI)或包含小字的PDF時,應考慮調整DPI。在OcrPdfInput構建中採用更高的DPI設置能顯著提高專門文件的閱讀準確性。
OCR引擎能否處理掃描和可搜索的PDF?
可以,IronOCR能有效處理掃描PDF(圖片為基礎)和可搜索PDF(文字為基礎)。基於Tesseract的引擎能自動處理不同型別的PDF,不需要採用不同的方法從多種PDF格式中提取文字。
What are the advantages of using region-specific OCR over full-page OCR?
Region-specific OCR improves performance by focusing on relevant areas, enhancing accuracy and reducing noise from irrelevant content. It is especially useful for forms and structured documents.
What advanced OCR features does IronOCR offer for PDFs?
IronOCR provides features such as creating searchable PDFs, multithreading for faster processing, image preprocessing, and support for multiple languages, catering to complex document processing needs.
How do you handle invalid page numbers in IronOCR?
If invalid page numbers are specified in IronOCR, it will throw an exception. It is recommended to implement error handling or verify page counts prior to processing.
Can IronOCR process non-consecutive pages from a PDF?
Yes, IronOCR can handle non-consecutive pages by specifying the desired page indices in a list. This allows for selective processing of pages in any order.

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。