使用IronOCR在C#中讀取掃描文件
IronOCR 可讓 C# 開發人員使用 OCR 技術從掃描的 PDF 和影像中擷取文字,僅需幾行程式碼即可將不可搜尋的影像式文件轉換為可搜尋、可存取的內容。
許多 PDF 文件包含無法搜尋的、基於圖像的文字。 IronOCR 將此轉換為可搜尋的內容,讓您更容易找到特定資訊,並提升文件的可讀性,尤其是對有視覺障礙的人而言。
自動提取無需手動複製或重新建立文字和圖像,從而確保準確性和效率。 這對於研究、法律文件和內容創作特別有用,因為重複使用 PDF 的特定部分是很常見的事。
企業可以從PDF中提取關鍵數據進行分析或系統整合,簡化工作流程。 設計師和行銷人員還可以提取圖像,以便在各種項目中進行增強和重複使用。
在本教程中,我們將探索OcrPdfInput的方法,涵蓋可用的選項和參數,以展示IronOCR如何簡化各種應用的PDF文本和圖像提取。
要使用此功能,您還必須安裝IronOcr.Extensions.AdvancedScan套件。
幾秒內即可開始使用 - 使用IronOCR的OcrInput.LoadPdf或LoadImage的一行程式碼載入掃描的PDF或圖像,並通過ReadDocument立即提取文本。 非常適合希望快速啟動並運行 OCR 功能的開發人員。
-
1使用 IronOCR 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var input = new IronOcr.OcrInput(); input.LoadPdf("scanned.pdf"); var text = new IronOcr.IronTesseract().ReadDocument(input).Text;Dim input = New IronOcr.OcrInput() input.LoadPdf("scanned.pdf") Dim text = New IronOcr.IronTesseract().ReadDocument(input).TextDim input = New IronOcr.OcrInput() input.LoadPdf("scanned.pdf") Dim text = New IronOcr.IronTesseract().ReadDocument(input).Text -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronOCR,並使用 免費試用
最小工作流程(5 個步驟)
- 下載用於讀取掃描文件的C#程式庫
- 導入掃描文件進行處理
- 對於影像,請使用
LoadImage方法;對於掃描的 PDF 文件,請使用LoadPdf方法。 - 使用
ReadDocument方法提取文字 - 根據需要保存或導出提取的文字以供進一步使用
如何從掃描文件中提取文本?
要從文件中的所有圖像中提取文本,請使用ReadDocument方法。 此方法處理文件並返回一個包含提取文本的對象,可以通過Text屬性訪問。 下面的示例展示了如何使用此方法與範例TIFF檔案。
IronOCR 支援多種文件格式的掃描。 對於圖像,您可以處理JPG、PNG、GIF、TIFF和BMP格式,PDF支持範圍包括單頁和多頁文件。 程式庫使用先進的Tesseract 5技術,以確保支持的所有格式的高準確性。
輸入文件的樣子如何?

我如何實現 OCR 碼?
using IronOcr;
using System;
// Instantiate OCR engine
var ocr = new IronTesseract();
// Configure OCR engine
using var input = new OcrInput();
input.LoadImage("potter.tiff");
// Perform OCR
OcrResult result = ocr.ReadDocument(input);
Console.WriteLine(result.Text);using IronOcr;
using System;
// Instantiate OCR engine
var ocr = new IronTesseract();
// Configure OCR engine
using var input = new OcrInput();
input.LoadImage("potter.tiff");
// Perform OCR
OcrResult result = ocr.ReadDocument(input);
Console.WriteLine(result.Text);Imports IronOcr
Imports System
' Instantiate OCR engine
Dim ocr As New IronTesseract()
' Configure OCR engine
Using input As New OcrInput()
input.LoadImage("potter.tiff")
' Perform OCR
Dim result As OcrResult = ocr.ReadDocument(input)
Console.WriteLine(result.Text)
End UsingImports IronOcr
Imports System
' Instantiate OCR engine
Dim ocr As New IronTesseract()
' Configure OCR engine
Using input As New OcrInput()
input.LoadImage("potter.tiff")
' Perform OCR
Dim result As OcrResult = ocr.ReadDocument(input)
Console.WriteLine(result.Text)
End Using我能期望OCR處理獲得什麼結果?

如果您需要對PDF檔案進行OCR操作,只需將LoadImage方法替換為LoadPdf即可。 這樣,IronOCR 就可以用同樣的方法處理和擷取掃描 PDF 中的文字。
高級文件處理選項
在處理掃描文件時,您通常需要對 OCR 過程有更多的控制。 IronOCR 提供多種進階功能,可提升文字擷取結果。
處理多頁文件
對於有多頁的文件,IronOCR 可以有效率地進行批次處理:
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load a multi-page PDF
input.LoadPdf("multi-page-document.pdf");
// Process all pages
OcrResult result = ocr.ReadDocument(input);
// Access individual page results
foreach (var page in result.Pages)
{
Console.WriteLine($"Page {page.PageNumber}: {page.WordCount} words");
}using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load a multi-page PDF
input.LoadPdf("multi-page-document.pdf");
// Process all pages
OcrResult result = ocr.ReadDocument(input);
// Access individual page results
foreach (var page in result.Pages)
{
Console.WriteLine($"Page {page.PageNumber}: {page.WordCount} words");
}Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Load a multi-page PDF
input.LoadPdf("multi-page-document.pdf")
' Process all pages
Dim result As OcrResult = ocr.ReadDocument(input)
' Access individual page results
For Each page In result.Pages
Console.WriteLine($"Page {page.PageNumber}: {page.WordCount} words")
Next
End UsingImports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Load a multi-page PDF
input.LoadPdf("multi-page-document.pdf")
' Process all pages
Dim result As OcrResult = ocr.ReadDocument(input)
' Access individual page results
For Each page In result.Pages
Console.WriteLine($"Page {page.PageNumber}: {page.WordCount} words")
Next
End Using優化OCR性能
掃描文件的品質會直接影響 OCR 的精確度。 IronOCR包括內建的圖像優化過濾器,以提高文本識別:
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load and enhance image quality
input.LoadImage("low-quality-scan.jpg");
input.Deskew(); // Correct image skew
input.DeNoise(); // Remove background noise
input.Binarize(); // Convert to black and white
OcrResult result = ocr.ReadDocument(input);using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load and enhance image quality
input.LoadImage("low-quality-scan.jpg");
input.Deskew(); // Correct image skew
input.DeNoise(); // Remove background noise
input.Binarize(); // Convert to black and white
OcrResult result = ocr.ReadDocument(input);Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Load and enhance image quality
input.LoadImage("low-quality-scan.jpg")
input.Deskew() ' Correct image skew
input.DeNoise() ' Remove background noise
input.Binarize() ' Convert to black and white
Dim result As OcrResult = ocr.ReadDocument(input)
End UsingImports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Load and enhance image quality
input.LoadImage("low-quality-scan.jpg")
input.Deskew() ' Correct image skew
input.DeNoise() ' Remove background noise
input.Binarize() ' Convert to black and white
Dim result As OcrResult = ocr.ReadDocument(input)
End Using建立可搜尋的 PDFs
處理掃描文件時最有價值的功能之一是能創建可搜索的PDFs。 這樣既能保持原始文件的外觀,又能增加文字層次:
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadPdf("scanned-document.pdf");
// Process and save as searchable PDF
OcrResult result = ocr.ReadDocument(input);
result.SaveAsSearchablePdf("searchable-output.pdf");using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadPdf("scanned-document.pdf");
// Process and save as searchable PDF
OcrResult result = ocr.ReadDocument(input);
result.SaveAsSearchablePdf("searchable-output.pdf");Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
input.LoadPdf("scanned-document.pdf")
' Process and save as searchable PDF
Dim result As OcrResult = ocr.ReadDocument(input)
result.SaveAsSearchablePdf("searchable-output.pdf")
End UsingImports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
input.LoadPdf("scanned-document.pdf")
' Process and save as searchable PDF
Dim result As OcrResult = ocr.ReadDocument(input)
result.SaveAsSearchablePdf("searchable-output.pdf")
End Using處理不同類型的文件
IronOCR 擅長處理商業環境中常見的各種文件類型。 無論您要處理的是發票、合約或歷史文件,這個資料庫都提供了從不同來源擷取資料的專門功能。
處理舊文件
許多組織都有舊格式的掃描文件檔案。 IronOCR高效處理這些文件,包括支持常用於檔案管理系統的多頁TIFF文件。
語言支援
雖然此示例著重於英語文本,IronOCR支持超過125種國際語言。 因此非常適合處理多語言文件或非英語語言的文件。
文件掃描最佳實踐
在處理掃描文件時達到最佳效果:
- 掃描質量:使用最低300 DPI以獲得最佳效果
- 文件格式:TIFF和PNG格式的保真度比JPEG更好
- 預處理:根據您的文件情況應用適當的過濾器
- 性能:對於大批量,考慮使用多線程功能
疑難排解常見問題
在處理掃描文件時,您可能會遇到各種挑戰。 以下是常見問題的解決方案:
- 質量不佳的掃描:在OCR處理前應用增強過濾器
- 傾斜的文件:使用
Deskew()方法來校正方向 - 混合內容:如果文件包含文本和非文本元素,請處理特定區域
如需更詳細的指南,請探索我們的綜合C# OCR教程或查看簡單OCR範例以快速入門。
後續步驟
既然您已了解如何從掃描文件中提取文本,您可以探索更高級的功能如使任何PDF可搜索或處理適用於Web應用的PDF流。 IronOCR 的靈活性使其適用於從簡單的文件數位化到複雜的企業文件處理工作流程。
常見問題
如何使用IronOCR從掃描的PDF中提取文本?
使用IronOCR,您可以通過加載掃描的PDF(如使用OcrInput.LoadPdf方法)並調用ReadDocument()方法來提取文本。這使您可以將不可搜索的PDF轉換成可文本閱讀的格式。
IronOCR可以處理哪些圖像格式?
IronOCR支持包括JPG、PNG、GIF、TIFF和BMP在內的各種圖像格式,從而從多種類型的文件中提取文本。
IronOCR中的`ReadDocument`方法用於什麼?
ReadDocument方法處理加載的文檔並從中所有的圖像中提取文本,使得可以通過結果對象的Text屬性訪問該文本。
IronOCR 能夠處理多頁文件嗎?
是的,IronOCR可以高效處理多頁文檔,如PDF,通過使用LoadPdf方法加載文件,然後從每一頁提取文本。
如何提高掃描圖像的質量以獲得更好的OCR效果?
IronOCR包含圖像優化過濾器,如Deskew、DeNoise和Binarize,可以通過校正圖像傾斜、去除背景噪音以及將圖像轉為黑白來提高OCR的準確性。
IronOCR支持從不同語言的文件中提取文本嗎?
IronOCR支持超過125種語言,使其能夠高效處理多語種或非英語文件。
IronOCR在創建可搜索的PDF方面提供了什麼?
使用IronOCR,您可以將掃描的文件轉換為可搜索的PDF,保持其文本層以便保留原始外觀,同時增加搜索功能。
IronOCR需要什麼包來訪問高級OCR功能?
要訪問IronOCR的高級OCR功能,您需要安裝IronOcr.Extensions.AdvancedScan包,這增加了文件處理的附加功能。
有什麼掃描文件進行OCR處理的最佳做法?
為了獲得最佳的OCR結果,最低使用300 DPI解析度,將文件保存為TIFF或PNG格式,必要時應用預處理過濾器,並利用多線程處理大型批量。
IronOCR如何處理質量不佳的掃描件?
IronOCR可以利用增強過濾器如DeNoise來清理圖像和Deskew來矯正任何方向問題,以改善質量不佳的掃描的OCR結果。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。