如何使用 IronOCR 在 C# 中擷取讀取結果

This article was translated from English: Does it need improvement?
Translated
View the article in English

讀取或 OCR 結果包含有關檢測到的段落、行、單字和單字元的大量資訊。 對於這些要素中的每一個,結果都提供了一套全面的詳細資訊。

它為每個元素提供文字內容、精確的 X 和 Y 座標、尺寸(寬度和高度)、文字方向(從左到右或從上到下)以及在CropRectangle物件中的位置。

快速入門:從檢測到的第一個單字中檢索單字文字

只需幾秒鐘即可上手:使用 IronTesseract 的 Read 方法對圖像進行 OCR 識別,並使用 Words 集合提取第一個單字的文字。 非常適合快速安裝和簡單的提取任務。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronOCR

    PM > Install-Package IronOcr

  2. 複製並運行這段程式碼。

    string wordText = new IronTesseract().Read("file.jpg").Words[0].Text;
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronOCR,免費試用!
    arrow pointer

數據輸出

結果值不僅包含提取的文本,還提供有關 IronOcr 在 PDF 和圖像文件中發現的頁面、段落、行、單字、字元和條碼的資訊。 您可以使用Read方法從傳回的 OcrResult 物件存取此資訊。

:path=/static-assets/ocr/content-code-examples/how-to/read-results-output-information.cs
using IronOcr;
using System;
using static IronOcr.OcrResult;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;

// Output information to console
Console.WriteLine($"Text: {paragraphs[0].Text}");
Console.WriteLine($"X: {paragraphs[0].X}");
Console.WriteLine($"Y: {paragraphs[0].Y}");
Console.WriteLine($"Width: {paragraphs[0].Width}");
Console.WriteLine($"Height: {paragraphs[0].Height}");
Console.WriteLine($"Text direction: {paragraphs[0].TextDirection}");
Imports IronOcr
Imports System
Imports IronOcr.OcrResult

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)

' Retrieve list of detected paragraphs
Private paragraphs() As Paragraph = ocrResult.Paragraphs

' Output information to console
Console.WriteLine($"Text: {paragraphs(0).Text}")
Console.WriteLine($"X: {paragraphs(0).X}")
Console.WriteLine($"Y: {paragraphs(0).Y}")
Console.WriteLine($"Width: {paragraphs(0).Width}")
Console.WriteLine($"Height: {paragraphs(0).Height}")
Console.WriteLine($"Text direction: {paragraphs(0).TextDirection}")
$vbLabelText   $csharpLabel
OcrResult 中的數據

OCR結果中的文本

OcrResult物件以簡單、直觀的方式呈現提取的文本,允許開發人員直接使用它或將其整合到應用程式的其他部分中。

讓我們來看一個循環列印文字的程式碼範例,以驗證結果。

:path=/static-assets/ocr/content-code-examples/how-to/read-results-output-text.cs
using IronOcr;
using System;
using static IronOcr.OcrResult;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add image
using var imageInput = new OcrImageInput("sampleText.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;

// Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---");
foreach (Paragraph paragraph in paragraphs)
{
    // Print the text of the current paragraph
    Console.WriteLine(paragraph.Text);
    
    // Add a blank line for better separation (optional)
    Console.WriteLine(); 
}

IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

輸出

OcrResult 中的文字數據

以下是控制台輸出的內容。 如您所見,IronOCR 能夠逐行精確地提取段落文字。

OCR結果中的文字位置

除了提取的文字外, OcrResult還提供詳細的位置資料。 以下程式碼示範如何遍歷每個段落並將其座標(X 和 Y)列印到控制台。

:path=/static-assets/ocr/content-code-examples/how-to/read-results-output-coordinates.cs
using IronOcr;
using System;
using static IronOcr.OcrResult;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add image
using var imageInput = new OcrImageInput("sampleText.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;

Console.WriteLine("--- All Detected Coordinates of Paragraphs ---");

// Use a 'for' loop to get an index for each paragraph
for (int i = 0; i < paragraphs.Length; i++)
{
    // Get the current paragraph
    Paragraph paragraph = paragraphs[i];

    // Print the paragraph number, text, and its location
    Console.WriteLine($"X: {paragraph.X}"); // X-coordinate (left edge)
    Console.WriteLine($"Y: {paragraph.Y}"); // Y-coordinate (top edge)

    // Add a blank line for better separation
    Console.WriteLine();
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

輸出

OcrResult 中的文字座標

從輸出結果可以看出,有三組座標分別對應三個段落。

OCR結果中的其他屬性

除了文字和文字座標外,IronOCR 還提供其他資訊。 對於文本的每個部分,例如段落、行、單字和單個字符,我們提供以下資訊:

  • 文字:實際的文字字串。
  • X:從頁面左邊緣到該點的距離,單位為像素。
  • Y:從頁面頂部邊緣算起的像素位置。
  • 寬度:以像素為單位的寬度。
  • 高度:以像素為單位的高度。
  • 文字方向:文字的閱讀方向,例如"從左到右"或"從上到下"。
  • 位置:一個矩形,以像素為單位顯示此文字在頁面上的位置。

段落、行、字和字元比較

以下對比的是偵測到的段落、行、單字和字元。

突出顯示段落
高亮線
高亮單字
突顯角色

條碼和二維碼

沒錯! IronOcr可以讀取條碼和二維碼。 雖然IronOcr的功能可能不如IronBarcode強大,但它確實支援常見的條碼類型。若要啟用條碼偵測,請將Configuration.ReadBarCodes屬性設為true。

此外,還可以從偵測到的條碼中提取有價值的訊息,包括其格式、值、座標(x,y)、高度、寬度和位置(IronSoftware.Drawing.Rectangle 物件)。 IronDrawing中的Rectangle類別允許在文件上進行精確定位。

:path=/static-assets/ocr/content-code-examples/how-to/read-results-barcodes.cs
using IronOcr;
using System;
using static IronOcr.OcrResult;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Enable barcodes detection
ocrTesseract.Configuration.ReadBarCodes = true;

// Add image
using OcrInput ocrInput = new OcrInput();
ocrInput.LoadPdf("sample.pdf");

// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(ocrInput);

// Output information to console
foreach(var barcode in ocrResult.Barcodes)
{
    Console.WriteLine("Format = " + barcode.Format);
    Console.WriteLine("Value = " + barcode.Value);
    Console.WriteLine("X = " + barcode.X);
    Console.WriteLine("Y = " + barcode.Y);
}
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports System
Imports IronOcr.OcrResult

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Enable barcodes detection
ocrTesseract.Configuration.ReadBarCodes = True

' Add image
Using ocrInput As New OcrInput()
	ocrInput.LoadPdf("sample.pdf")
	
	' Perform OCR
	Dim ocrResult As OcrResult = ocrTesseract.Read(ocrInput)
	
	' Output information to console
	For Each barcode In ocrResult.Barcodes
		Console.WriteLine("Format = " & barcode.Format)
		Console.WriteLine("Value = " & barcode.Value)
		Console.WriteLine("X = " & barcode.X)
		Console.WriteLine("Y = " & barcode.Y)
	Next barcode
	Console.WriteLine(ocrResult.Text)
End Using
$vbLabelText   $csharpLabel

輸出

偵測條碼

常見問題解答

如何使用C#從圖像和PDF中提取文本元素?

您可以使用IronOCR的`Read`方法從圖像和PDF中提取文本元素,此方法執行光學字符識別(OCR)以獲取段落、行、單詞和字符的詳細信息,包括它們的文本內容、座標和尺寸。

開始使用.NET C#中的OCR的過程是什麼?

要開始用.NET C#中的OCR,請從NuGet下載IronOCR庫,準備您的圖像或PDF文檔,並使用`Read`方法獲取`OcrResult`對象,該對象包含關於提取文本和文檔結構的詳細信息。

IronOCR能檢測並提取條碼信息嗎?

是的,IronOCR通過將`Configuration.ReadBarCodes`屬性設置為true來檢測和提取條碼信息,允許您檢索例如條碼格式、值及其在文檔中的位置等數據。

IronOCR能檢測哪些類型的文檔元素?

IronOCR能檢測各種文檔元素,包括頁、段落、行、單詞和獨立的字符,以及條碼和QR碼,為文檔結構提供全面分析。

如何配置IronOCR來讀取不同方向的文本?

IronOCR能夠通過分析`OcrResult`對象中的方向屬性來讀取多方向的文本,例如“從左到右”或“從上到下”。

IronOCR中的`CropRectangle`對象是什麼?

IronOCR中的`CropRectangle`對象定義了頁面上文本元素的位置和大小,以座標和尺寸的形式,有助於精確的文本識別和提取。

如何使用IronOCR的`Read`方法分析文檔?

要使用IronOCR的`Read`方法,創建一個IronOCR引擎的實例,載入目標文檔,並執行`Read`方法以獲取OCR結果,可用於訪問文本數據和文檔屬性。

IronOCR如何處理QR碼的檢測?

IronOCR通過啟用條碼閱讀的`Configuration.ReadBarCodes`設置來處理QR碼的檢測,這使其能提取QR碼數據,包括其格式、值和位置。

在文本提取中`OcrResult`的角色是什麼?

`OcrResult`對象在文本提取中起著關鍵作用,持有提取的文本和伴隨的信息,如文本元素的位置、尺寸、方向以及條碼信息。

如何確保使用IronOCR進行準確的文本提取?

為了確保IronOCR的準確文本提取,請務必提供優質的輸入文檔,並適當配置如`Configuration.ReadBarCodes`的設置以優化OCR性能。

Chaknith Bin
軟體工程師
Chaknith 在 IronXL 和 IronBarcode 上工作。他對 C# 和 .NET 擁有深厚的專業知識,幫助改進了軟體並支持客戶。他從用戶互動中得到的見解有助於改善產品、文檔和整體體驗。
準備好開始了嗎?
Nuget 下載 5,167,857 | Version: 2025.11 剛發表