IronOCR でトリミング領域と長方形を作成する
IronOCR を使用して PDF のコンテンツ領域を設定するにはどうすればいいですか?
コンテンツエリアとPDF
OcrInput.LoadPdfとLoadPdfPageメソッドにはすべて、 ContentArea を追加するオプションがあります。
質問 - PDF のサイズはピクセル単位で決まるのではなく、コンテンツ領域は通常ピクセル単位で測定されるため、コンテンツ領域の大きさを知るにはどうすればよいでしょうか?
オプション1
OcrInput.TargetDPIデフォルトは 225 です。これは PDF 画像のサイズをピクセル単位で指定します。 IronOCR がこれを読み取ります。
オプション2(理想的な使用例)
- PDF テンプレートで
OcrInput.LoadPdf()を使用します。 OcrInput.GetPages()を使用して、入力の幅と高さを取得します。OcrInput.GetPages().First().ToBitmap()を使用して、OCR エンジンが読み取る正確な画像を取得します。- エクスポートされた画像からピクセル単位でコンテンツ領域を測定できるようになりました。
- ターゲット座標は、特定の 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最終結果:
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 UsingAPI リファレンス: OcrInput|OcrInput.ページ






