使用 IronOCR 處理作物區域和矩形

Curtis Chau
Curtis Chau
已更新: 2026年4月22日

如何使用 IronOCR 設定 PDF 的內容區域?

內容區域和 PDF

OcrInput.LoadPdf 和 LoadPdfPage 方法均可以選擇添加ContentArea。

問題是-PDF 檔案的大小不是以像素為單位的,但內容區域通常是以像素為單位的,我該如何知道我的內容區域有多大?

選項 1

OcrInput.TargetDPI 預設值是225—這決定了PDF圖像的像素大小。 IronOCR 會看到這則訊息。

選項 2(理想使用情境)

  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。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 6,411,693版本:2026.11剛剛發布