Recadrer les régions et les rectangles avec IronOCR

This article was translated from English: Does it need improvement?
Translated
View the article in English

Comment définir les zones de contenu des PDF avec IronOCR ?

Zones de contenu et PDF

Les méthodes OcrInput.LoadPdf et LoadPdfPage ont toutes la possibilité d'ajouter un ContentArea .

La question est la suivante : comment puis-je connaître la taille de ma zone de contenu, car les fichiers PDF ne sont pas dimensionnés en pixels, alors que les zones de contenu sont généralement mesurées en pixels ?

Option 1

La valeur par défaut OcrInput.TargetDPI est de 225 ; cela détermine la taille de l'image PDF en pixels. IronOCR lira ceci.

Option 2 (cas d'utilisation idéal)

  1. Utilisez OcrInput.LoadPdf() avec votre modèle PDF.
  2. Utilisez OcrInput.GetPages() pour obtenir la largeur et la hauteur de l'entrée.
  3. Utilisez OcrInput.GetPages().First().ToBitmap() pour obtenir l'image exacte que le moteur OCR lira.
  4. Vous pouvez désormais mesurer les zones de contenu en pixels à partir de l'image exportée.
  5. Les coordonnées ciblées pourraient être utilisées pour une région OCR spécifique (voir dans le résultat final).

Pour obtenir vos informations :

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");
}
Imports System.Linq ' Needed for First()
Imports IronOcr

Private ocr = New IronTesseract()
Using 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
	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
$vbLabelText   $csharpLabel

Résultat final :

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);
}
Imports IronOcr
Imports IronSoftware.Drawing ' Needed for Rectangle

Private ocr = New IronTesseract()
Using input = New OcrInput()
	' Define the content area rectangle with specific pixel coordinates
	Dim contentArea = New Rectangle With {
		.X = 215,
		.Y = 1250,
		.Height = 280,
		.Width = 1335
	}

	' 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 Using
$vbLabelText   $csharpLabel

Référence API : OcrInput|OcrInput.Page

Curtis Chau
Rédacteur technique

Curtis Chau détient un baccalauréat en informatique (Université de Carleton) et se spécialise dans le développement front-end avec expertise en Node.js, TypeScript, JavaScript et React. Passionné par la création d'interfaces utilisateur intuitives et esthétiquement plaisantes, Curtis aime travailler avec des frameworks modernes ...

Lire la suite
Prêt à commencer?
Nuget Téléchargements 5,167,857 | Version: 2025.11 vient de sortir