Recadrer les régions et les rectangles avec IronOCR
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 l'option 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 de OcrInput.TargetDPI est 225 - cela dicte la taille de l'image PDF en pixels. IronOCR lira ceci.
Option 2 (cas d'utilisation idéal)
- Utilisez
OcrInput.LoadPdf()avec votre modèle PDF. - Utilisez
OcrInput.GetPages()pour obtenir la largeur et la hauteur de l'entrée. - Utilisez
OcrInput.GetPages().First().ToBitmap()pour obtenir l'image exacte que le moteur OCR lira. - Vous pouvez désormais mesurer les zones de contenu en pixels à partir de l'image exportée.
- 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");
}Imports System.Linq ' Needed for First()
Imports IronOcr
Dim ocr As New IronTesseract()
Using input As 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 UsingRé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);
}Imports IronOcr
Imports IronSoftware.Drawing ' Needed for Rectangle
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Define the content area rectangle with specific pixel coordinates
Dim contentArea As New Rectangle With {
.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
Dim result = ocr.Read(input)
' Optionally, print the OCR result
Console.WriteLine(result.Text)
End UsingRéférence API : OcrInput|OcrInput.Page

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 et créer des manuels bien structurés et visuellement attrayants.