Recortar regiones y rectángulos con IronOCR

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

¿Cómo configuro las áreas de contenido en PDF con IronOCR?

Áreas de contenido y archivos PDF

Los métodos OcrInput.LoadPdf y LoadPdfPage tienen la opción de añadir una ContentArea.

La pregunta: ¿Cómo sé qué tan grande es mi área de contenido ya que los PDFs no se dimensionan en píxeles, pero las áreas de contenido generalmente se miden en ellos?

Opción 1

OcrInput.TargetDPI por defecto es 225 - esto dicta el tamaño de la imagen PDF en píxeles. IronOCR leerá esto.

Opción 2 (caso de uso ideal)

  1. Usa OcrInput.LoadPdf() con tu plantilla PDF.
  2. Usa OcrInput.GetPages() para obtener el ancho y alto de la entrada.
  3. Usa OcrInput.GetPages().First().ToBitmap() para obtener la imagen exacta que el motor OCR leerá.
  4. Ahora puedes medir ContentAreas en píxeles desde la imagen exportada.
  5. Las coordenadas objetivo podrían usarse para una región específica de OCR (ver en Resultado Final).

Para obtener información:

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

Resultado 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

Referencia de API: OcrInput|OcrInput.Page

Curtis Chau
Escritor Técnico

Curtis Chau tiene una licenciatura en Ciencias de la Computación (Carleton University) y se especializa en el desarrollo front-end con experiencia en Node.js, TypeScript, JavaScript y React. Apasionado por crear interfaces de usuario intuitivas y estéticamente agradables, disfruta trabajando con frameworks modernos y creando manuales bien ...

Leer más
¿Listo para empezar?
Nuget Descargas 5,167,857 | Version: 2025.11 recién lanzado