使用 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 會看到這則訊息。

方案二(理想使用場景)

  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 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

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。

準備好開始了嗎?
Nuget 下載 5,299,091 | 版本: 2025.12 剛剛發布