IronOCR でトリミング領域と長方形を作成する

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

IronOCR を使用して PDF のコンテンツ領域を設定するにはどうすればいいですか?

コンテンツエリアとPDF

OcrInput.LoadPdfLoadPdfPageメソッドにはすべて、 ContentArea を追加するオプションがあります。

質問 - PDF のサイズはピクセル単位で決まるのではなく、コンテンツ領域は通常ピクセル単位で測定されるため、コンテンツ領域の大きさを知るにはどうすればよいでしょうか?

オプション1

OcrInput.TargetDPIデフォルトは 225 です。これは PDF 画像のサイズをピクセル単位で指定します。 IronOCR がこれを読み取ります。

オプション2(理想的な使用例)

  1. PDF テンプレートでOcrInput.LoadPdf()を使用します。
  2. OcrInput.GetPages()を使用して、入力の幅と高さを取得します。
  3. OcrInput.GetPages().First().ToBitmap()を使用して、OCR エンジンが読み取る正確な画像を取得します。
  4. エクスポートされた画像からピクセル単位でコンテンツ領域を測定できるようになりました。
  5. ターゲット座標は、特定の OCR 領域に使用できます (最終結果を参照)。

情報を取得するには:

using System.Linq;  // Needed for First()
using IronOcr;

var ocr = new IronTesseract();
using (var input = new OcrInput())
{
    // Load the PDF document
    input.LoadPdf("example.pdf");

    // Save the first page as a bitmap to measure it
    input.GetPages().First().ToBitmap().SaveAs("measure-me.bmp");

    // Get the dimensions of the first page
    var width = input.GetPages().First().Width;
    var height = input.GetPages().First().Height;

    // Optionally, output the dimensions to understand the scale
    Console.WriteLine($"Width: {width}px, Height: {height}px");
}
using System.Linq;  // Needed for First()
using IronOcr;

var ocr = new IronTesseract();
using (var input = new OcrInput())
{
    // Load the PDF document
    input.LoadPdf("example.pdf");

    // Save the first page as a bitmap to measure it
    input.GetPages().First().ToBitmap().SaveAs("measure-me.bmp");

    // Get the dimensions of the first page
    var width = input.GetPages().First().Width;
    var height = input.GetPages().First().Height;

    // Optionally, output the dimensions to understand the scale
    Console.WriteLine($"Width: {width}px, Height: {height}px");
}
Imports System.Linq ' Needed for First()
Imports IronOcr

Private ocr = New IronTesseract()
Using input = New OcrInput()
	' Load the PDF document
	input.LoadPdf("example.pdf")

	' Save the first page as a bitmap to measure it
	input.GetPages().First().ToBitmap().SaveAs("measure-me.bmp")

	' Get the dimensions of the first page
	Dim width = input.GetPages().First().Width
	Dim height = input.GetPages().First().Height

	' Optionally, output the dimensions to understand the scale
	Console.WriteLine($"Width: {width}px, Height: {height}px")
End Using
$vbLabelText   $csharpLabel

最終結果:

using IronOcr;
using IronSoftware.Drawing; // Needed for Rectangle

var ocr = new IronTesseract();
using (var input = new OcrInput())
{
    // Define the content area rectangle with specific pixel coordinates
    var contentArea = new Rectangle
    { 
        X = 215, 
        Y = 1250, 
        Height = 280, 
        Width = 1335 
    };  //<-- the area you want in px

    // Load the specific content area of the PDF
    input.LoadPdf("example.pdf", contentArea: contentArea);

    // Perform OCR on the defined content area
    var result = ocr.Read(input);

    // Optionally, print the OCR result
    Console.WriteLine(result.Text);
}
using IronOcr;
using IronSoftware.Drawing; // Needed for Rectangle

var ocr = new IronTesseract();
using (var input = new OcrInput())
{
    // Define the content area rectangle with specific pixel coordinates
    var contentArea = new Rectangle
    { 
        X = 215, 
        Y = 1250, 
        Height = 280, 
        Width = 1335 
    };  //<-- the area you want in px

    // Load the specific content area of the PDF
    input.LoadPdf("example.pdf", contentArea: contentArea);

    // Perform OCR on the defined content area
    var result = ocr.Read(input);

    // Optionally, print the OCR result
    Console.WriteLine(result.Text);
}
Imports IronOcr
Imports IronSoftware.Drawing ' Needed for Rectangle

Private ocr = New IronTesseract()
Using input = New OcrInput()
	' Define the content area rectangle with specific pixel coordinates
	Dim contentArea = New Rectangle With {
		.X = 215,
		.Y = 1250,
		.Height = 280,
		.Width = 1335
	}

	' Load the specific content area of the PDF
	input.LoadPdf("example.pdf", contentArea:= contentArea)

	' Perform OCR on the defined content area
	Dim result = ocr.Read(input)

	' Optionally, print the OCR result
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

API リファレンス: OcrInput|OcrInput.ページ

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はできましたか?
Nuget ダウンロード 5,167,857 | Version: 2025.11 リリース