使用 IronOCR 处理作物区域和矩形

Curtis Chau
Curtis Chau
Updated: 2026年6月29日

如何使用 IronOCR 设置 PDF 的内容区域?

内容区域和 PDF

LoadPdfPage方法都可以选择添加一个ContentArea。

问题是——PDF 文件的大小不是以像素为单位的,但内容区域通常是以像素为单位的,我该如何知道我的内容区域有多大?

选项 1

OcrInput.TargetDPI默认值为225 - 它决定PDF图像的像素大小。 IronOCR 会看到这条信息。

方案二(理想使用场景)

  1. 使用OcrInput.LoadPdf()与您的PDF模板。
  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 IronOcr;
using IronSoftware.Drawing; // Needed for Rectangle
using System;

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);
}

API 参考: OcrInput|OcrInput.Page

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

阅读更多

准备开始了吗?

Nuget Downloads 6,357,159版本:2026.11刚刚发布