使用 IronOCR 处理作物区域和矩形
This article was translated from English: Does it need improvement?
TranslatedView the article in English
如何使用 IronOCR 设置 PDF 的内容区域?
内容区域和 PDF
OcrInput.LoadPdf和LoadPdfPage方法都提供了添加ContentArea 的选项。
问题是——PDF 文件的大小不是以像素为单位的,但内容区域通常是以像素为单位的,我该如何知道我的内容区域有多大?
选项 1
OcrInput.TargetDPI默认值为 225 - 这决定了 PDF 图像的像素大小。 IronOCR 会看到这条信息。
方案二(理想使用场景)
- 使用
OcrInput.LoadPdf()加载您的 PDF 模板。 - 使用
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");
}$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);
}$vbLabelText $csharpLabel
API 参考: OcrInput|OcrInput.Page
准备开始了吗?
Nuget 下载 5,299,091 | 版本: 2025.12 刚刚发布






