IRONSOFTWAREHOME

使用IronOCR裁剪區域和矩形

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

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

內容區域和PDF

OcrInput.LoadPdfLoadPdfPage 方法都可以選擇新增內容區域

問題 - 如何知道我的內容區域有多大,因為PDF不是以像素為單位,但內容區域通常用它們來測量?

選項 1

OcrInput.TargetDPI 的預設值是225 - 這決定了PDF圖像的像素大小。 IronOCR會讀取這個。

選項 2(理想使用案例)

  1. 在您的PDF模板中使用 OcrInput.LoadPdf()
  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

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 Reference: OcrInput | OcrInput.Page

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

準備開始了嗎?

Nuget Downloads 6,236,385版本:2026.9剛剛發布

立即獲取您的30天試用金鑰
無需信用卡或帳戶建立
C# PDF的NuGet程式庫
使用NuGet安裝

版本: 2026.9

PM > Install-Package IronOcr
nuget.org/packages/IronOcr/
  1. 在解決方案資源管理器中,右鍵點擊參考,管理NuGet包
  2. 選擇瀏覽並搜尋"IronOCR"
  3. 選擇包並安裝
C# PDF DLL
下載 DLL

版本: 2026.9

這裡下載Windows安裝程式。

  1. 下載並解壓IronOCR至您的方案目錄下的~/Libs等位置
  2. 在Visual Studio解決方案資源管理器中,右鍵點擊參考。選擇瀏覽,"IronOCR.dll"

授權從$999

有問題嗎?聯絡我們的開發團隊。

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
獲取您的無義務諮詢
填寫以下表格或發送電子郵件至sales@ironsoftware.com
您的詳細資訊將始終保密。
被全球數百萬工程師信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立