IronOCRを使用してC#で読み取り結果を抽出する方法
IronOCRのReadメソッドは、検出された各要素に対する正確な座標、寸法、テキストの方向、および階層構造(段落、行、単語、文字)を含む抽出されたテキストと詳細なメタデータを含むOcrResultオブジェクトを返します。
OCRの結果には、検出された段落、行、単語、個々の文字に関する包括的な情報が含まれています。
各要素には、テキストコンテンツ、正確なXおよびY座標、寸法(幅と高さ)、テキストの方向(左から右または上から下)、及びCropRectangleオブジェクト内の位置を提供します。
IronTesseractのReadメソッドを使用して画像をOCRし、Wordsコレクションを使用して最初の単語のテキストを抽出します。
-
1IronOCR を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
string wordText = new IronTesseract().Read("file.jpg").Words[0].Text;Dim wordText As String = New IronTesseract().Read("file.jpg").Words(0).TextDim wordText As String = New IronTesseract().Read("file.jpg").Words(0).Text -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronOCRを使い始めるには、無料トライアルをお試しください。
最小限のワークフロー(5ステップ)
- 読み取り結果にアクセスするためのC#ライブラリをダウンロードする
- ターゲット画像と PDF ドキュメントを準備する
- インポートした文書に対してOCRを実行するには、
Readメソッドを使用します。 - 結果の
X,Y,width,height,text directionにアクセスすることができます - 検出された段落、行、単語、および文字の比較をチェックする
OCR結果からどのようなデータを抽出できますか?
翻訳結果には抽出されたテキストだけでなく、IronOCRによってPDFや画像ドキュメントから発見されたページ、段落、行、単語、文字、バーコードの情報も含まれます。 返されたOcrResultオブジェクトからReadメソッドを使用してこの情報にアクセスできます。
IronOCRの包括的な結果システムは、Tesseract 5エンジンの強力さを基に構築され、開発者に単純なテキスト認識を超えた構造化データ抽出機能を提供します。 スキャンされたドキュメント、写真、またはスクリーンショットを処理する際に、OcrResultクラスは抽出されたデータに対して詳細なコントロールを提供します。
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}");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}")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}")
OCR結果からテキストコンテンツにアクセスする方法は?
OcrResultオブジェクトは、抽出されたテキストをシンプルで直感的な方法で提示し、開発者がそれを直接使用したり、他のアプリケーションコンポーネントに統合したりすることを可能にします。 階層構造は、自然なドキュメントのテキスト構成を反映しており、異なる粒度レベルのコンテンツを簡単に扱うことができます。
複数言語サポートを必要とするアプリケーションでは、IronOCRは多言語ドキュメントを処理し、126のサポートされている言語全体で同じ構造化された結果フォーマットを維持します。
次のコード例は、結果を確認するためにループでテキストを表示します。
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();
}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();
}Imports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sampleText.png")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Retrieve list of detected paragraphs
Dim paragraphs As Paragraph() = ocrResult.Paragraphs
' Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---")
For Each paragraph As Paragraph In paragraphs
' Print the text of the current paragraph
Console.WriteLine(paragraph.Text)
' Add a blank line for better separation (optional)
Console.WriteLine()
Next
End UsingImports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sampleText.png")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Retrieve list of detected paragraphs
Dim paragraphs As Paragraph() = ocrResult.Paragraphs
' Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---")
For Each paragraph As Paragraph In paragraphs
' Print the text of the current paragraph
Console.WriteLine(paragraph.Text)
' Add a blank line for better separation (optional)
Console.WriteLine()
Next
End Using出力

コンソール出力はIronOCRが段落テキストを一行ずつ正確に抽出していることを示しています。 このエンジンは段落境界を自動的に検出するため、複数のテキストブロックがある複雑な文書の処理に最適です。
検出されたテキストの位置座標を取得する方法は?
抽出されたテキストに加えて、OcrResultは詳細な位置データを提供します。 この空間情報は、レイアウトの忠実度を維持したり、特定のドキュメント領域から的を絞ったテキスト抽出を行う必要があるアプリケーションにとって非常に重要です。 座標系は、ページの左上隅からの標準ピクセル寸法を使用します。
座標を基にした操作の精度を向上するために、特定の領域に集中するOCR地域ターゲティングを使用するか、コンピュータビジョン機能を利用してテキスト地域を自動的に識別します。
次のコードは、各段落を繰り返し処理し、その座標(XとY)をコンソールに表示することを示します。
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();
}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();
}Imports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sampleText.png")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Retrieve list of detected paragraphs
Dim paragraphs As Paragraph() = ocrResult.Paragraphs
Console.WriteLine("--- All Detected Coordinates of Paragraphs ---")
' Use a 'for' loop to get an index for each paragraph
For i As Integer = 0 To paragraphs.Length - 1
' Get the current paragraph
Dim paragraph As 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()
Next
End UsingImports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sampleText.png")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Retrieve list of detected paragraphs
Dim paragraphs As Paragraph() = ocrResult.Paragraphs
Console.WriteLine("--- All Detected Coordinates of Paragraphs ---")
' Use a 'for' loop to get an index for each paragraph
For i As Integer = 0 To paragraphs.Length - 1
' Get the current paragraph
Dim paragraph As 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()
Next
End Using出力

出力は、3つの段落に対応する3組の座標を示しています。これらの座標は、バウンディングボックスの描画、特定領域の抽出、テキスト要素間の空間的関係の維持に使用できます。
OCR結果にはどのような他の属性がありますか?
IronOCRはテキストとテキスト座標に加えて、追加情報を提供します。 各テキスト要素(段落、行、単語、個々の文字)について、以下の情報があります:
- テキスト: 文字列としての実際のテキスト。
- X: ページの左端からの位置(ピクセル単位)。
- Y: ページの上端からの位置(ピクセル単位)。
- 幅: ピクセル単位での幅。
- 高さ: ピクセル単位での高さ。
- テキストの方向:テキストが読まれる方向(左から右、または上から下)。
- 位置: このテキストがページ上のどこにあるかをピクセル単位で示す矩形。
これらの属性は、実装時に特に役立ちます:
- テキストハイライトと注釈システム
- フォームフィールドの自動検出
- 文書変換におけるレイアウト保持
- データ抽出のための空間テキスト分析
デバッグおよび視覚化のために、テキストハイライト機能を使用して検出された領域の精度を視覚的に確認します。
段落、行、単語、文字はどのように比較されますか?
IronOCRの階層的なテキスト構造により、開発者は特定のユースケースに適した詳細レベルで作業することができます。 これらの要素の違いを理解することは、アプリケーションに適した粒度を選択するのに役立ちます。
以下は、検出された段落、行、単語、文字の比較です。
![]() Paragraph | ![]() Line |
![]() Word | ![]() Character |
それぞれの粒度レベルには異なる目的があります:
- 段落: ドキュメント構造分析と大量テキスト抽出に最適
- 行: 読み順の維持と表形式データの処理に役立つ
- 単語: 検索機能とテキスト分析に理想的
- 文字: スペルチェックや正確なテキスト編集アプリケーションに最適
IronOCRはバーコードとQRコードを読み取れますか?
はい、IronOCRはバーコードとQRコードを読むことができます。 この機能はIronBarcodeほど信頼できないかもしれませんが、IronOCRは一般的なバーコードタイプのサポートを提供しています。バーコード検出を有効にするには、Configuration.ReadBarCodesプロパティをtrueに設定します。 この統合された機能により、IronOCRはインボイス、出荷ラベル、製品カタログのようなテキストとBarCodeの両方を含む文書の処理に最適です。
また、検出されたバーコードから、IronSoftware.Drawing.Rectangleオブジェクトとしてフォーマット、値、座標(x, y)、高さ、幅、および位置などの貴重な情報を抽出できます。 IronDrawingのこのRectangleクラスは、ドキュメント上の正確な配置を可能にします。
より高度なバーコード読み取りシナリオについては、当社のドキュメントにある包括的なバーコード読み取り例をチェックしてください。
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);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 UsingImports 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バーコード検出の出力はどのように見えますか?
IronOCRのバーコード検出機能はテキスト抽出と統合され、テキストコンテンツとバーコードデータの両方を含む統一された結果を提供します。 この2つの機能は、両方の情報タイプの抽出と相関が必要な自動文書処理ワークフローにとって価値があります。

出力は、IronOCRが複数のバーコードタイプを同時に検出し、フォーマット識別(QRCodeやEAN8など)、デコードされた値、検出された各コードの正確な座標情報を提供する能力を示しています。 この包括的なデータにより、開発者は、さまざまな種類のコンテンツを効率的に処理する高度な文書処理アプリケーションを構築することができます。
よくある質問
IronOCRでのOcrResultオブジェクトとは何ですか?
IronOCRのOcrResultオブジェクトには、段落、行、単語、文字などの検出された要素の正確な座標、寸法、テキスト方向、階層構造を含む、抽出されたテキストと詳細なメタデータが含まれています。
IronOCRを使って最初の単語のテキストを抽出するにはどうすればいいですか?
IronTesseractのReadメソッドを使用して画像をOCRし、Wordsコレクションを使用して最初の単語のテキストを抽出することができます。例: new IronTesseract().Read("file.jpg").Words[0].Text。
IronOCRを使用してOCR結果からどのデータを抽出できますか?
IronOCRはPDFおよび画像ドキュメントからテキスト、ページ、段落、行、単語、文字、バーコードを抽出でき、また位置座標やテキスト方向などのメタデータも含まれます。
IronOCRは複数の言語に対応できますか?
はい、IronOCRは多言語ドキュメントをサポートし、126のサポートする言語すべてで構造化された結果形式を維持します。これにより、複数言語を必要とするアプリケーションに最適です。
IronOCRで検出されたテキストの位置座標を取得するにはどうすればいいですか?
OcrResultオブジェクトは、各検出されたテキスト要素のX座標、Y座標、幅、高さを含む詳細な位置データを提供し、ページの左上隅からピクセル単位で測定される座標系を使用しています。
IronOCRを使用してOCR結果で利用可能な属性は何ですか?
各テキスト要素について、IronOCRはテキスト、X座標、Y座標、幅、高さ、テキスト方向、位置を提供します。これらの属性は、テキストのハイライト、フォームフィールドの検出、ドキュメントレイアウトの維持のようなアプリケーションを容易にします。
IronOCR はバーコードと QR コードを読み取ることができますか?
はい、IronOCRはQRコードを含む一般的なバーコードタイプを読み取ることができ、設定でバーコード検出を有効にすることができます。この統合により、テキストとバーコードの両方を含むドキュメントの包括的な処理が可能になります。
階層的テキスト分析にIronOCRはどのように役立ちますか?
IronOCRの階層的テキスト構造により、開発者は段落、行、単語、文字など、さまざまな粒度レベルで作業することができ、ドキュメント構造分析やテキスト検索機能のようなさまざまなアプリケーションに適しています。
座標ベースの操作に対してIronOCRはどのような高度な機能を提供しますか?
IronOCRは、フォーカスされたテキスト抽出のためのOCR領域ターゲティングと、コンピュータビジョン機能によりテキスト領域を自動的に識別する機能を提供し、座標ベースのアプリケーションにおける精度を向上させます。
IronOCRにおけるバーコード検出の出力はどのように見えますか?
IronOCRのバーコード検出出力には、フォーマットの識別、デコードされた値、正確な座標が含まれ、テキスト抽出と統合されて混在するコンテンツドキュメントの効率的な処理が可能になります。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。



