使用 IronOCR 处理作物区域和矩形
Curtis Chau
Updated: 2026年6月29日
如何使用 IronOCR 设置 PDF 的内容区域?
内容区域和 PDF
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");
}Imports System.Linq ' Needed for First()
Imports IronOcr
Dim ocr As New IronTesseract()
Using input As 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);
}Imports IronOcr
Imports IronSoftware.Drawing ' Needed for Rectangle
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Define the content area rectangle with specific pixel coordinates
Dim contentArea As New Rectangle With {
.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
Dim result = ocr.Read(input)
' Optionally, print the OCR result
Console.WriteLine(result.Text)
End UsingAPI 参考: OcrInput|OcrInput.Page

技术作家
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。
...
阅读更多