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 および画像ドキュメント内で検出されたページ、段落、行、単語、文字、バーコードに関する情報も提供されます。 返された OcrResult オブジェクトからReadメソッドを使用してこの情報にアクセスできます。

: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のテキスト座標

出力からわかるように、3 つの段落に対応する 3 セットの座標が表示されます。

OCR結果の追加属性

テキストとテキスト座標の他に、IronOCR は追加情報を提供します。 段落、行、単語、個々の文字など、テキストの各部分について、次の情報を提供します。

  • テキスト: 文字列としての実際のテキスト。
  • X: ページの左端からの位置(ピクセル単位)。
  • Y: ページの上端からの位置(ピクセル単位)。
  • 幅: ピクセル単位の幅。
  • 高さ: ピクセル単位の高さ。
  • テキストの方向: テキストが読まれた方向 ("左から右"や"上から下"など)。
  • 場所: このテキストがページ上のどこにあるかをピクセル単位で示す四角形。

段落、行、単語、文字の比較

以下は検出された段落、行、単語、文字の比較です。

段落を強調表示する
ハイライトライン
単語を強調表示
ハイライト文字

バーコードとQRコード

正解です! IronOcr はバーコードと QR コードを読み取ることができます。 IronOcr の機能は IronBarcode ほど強力ではないかもしれませんが、一般的なバーコードの種類をサポートしています。バーコード検出を有効にするには、 Configuration.ReadBarCodesプロパティを true に設定してください。

さらに、検出されたバーコードから、形式、値、座標 (x、y)、高さ、幅、位置などの貴重な情報を Iron Software.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` オブジェクト内の方向プロパティを分析することで、'Left to Right' や 'Top to Bottom' など、複数の方向でテキストを読み取ることが可能です。

IronOCR における `CropRectangle` オブジェクトとは何ですか?

IronOCR における `CropRectangle` オブジェクトは、ページ上のテキスト要素の位置とサイズを座標と寸法で定義し、正確なテキストの識別と抽出を支援します。

IronOCR の `Read` メソッドを使用してドキュメントを分析するにはどうすればよいですか?

IronOCR の `Read` メソッドを使用するには、IronOCR エンジンのインスタンスを作成し、ターゲットドキュメントをロードし、`Read` メソッドを実行して OCR 結果を取得します。これにより、テキストデータやドキュメントプロパティにアクセスすることができます。

IronOCR は QR コードの検出をどのように処理しますか?

IronOCR は `Configuration.ReadBarCodes` 設定を介してバーコード読み取りを有効にし、QR コードのデータを抽出します。これには形式、値、および位置などのデータが含まれます。

`OcrResult`はテキスト抽出においてどのような役割を果たしますか?

`OcrResult` オブジェクトは、抽出されたテキストおよびテキスト要素の位置、寸法、方向などの添付の詳細、さらにはバーコード情報を保持することで、テキスト抽出において重要な役割を果たします。

IronOCR で正確なテキスト抽出を保証するにはどうすればよいですか?

IronOCR で正確なテキスト抽出を保証するには、高品質な入力ドキュメントを提供し、バーコード検出用に `Configuration.ReadBarCodes` などの設定を適切に構成して、OCR パフォーマンスを最適化してください。

Chaknith Bin
ソフトウェアエンジニア
ChaknithはIronXLとIronBarcodeに取り組んでいます。彼はC#と.NETの深い専門知識を持ち、ソフトウェアの改善や顧客サポートに貢献しています。ユーザーとの対話から得られる洞察が、より良い製品、ドキュメント、および全体的な経験に寄与しています。
準備はできましたか?
Nuget ダウンロード 5,167,857 | Version: 2025.11 リリース