Crop Regions and Rectangles with IronOCR

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

Wie lege ich Inhaltsbereiche in PDFs mit IronOCR fest?

Inhaltsbereiche und PDFs

OcrInput.LoadPdf und LoadPdfPage Methoden haben alle die Möglichkeit, ein ContentArea hinzuzufügen.

Die Frage - Wie weiß ich, wie groß mein Inhaltsbereich ist, da PDFs nicht in Pixeln dimensioniert sind, sondern Inhaltsbereiche allgemein in ihnen gemessen werden?

Option 1

OcrInput.TargetDPI Standard ist 225 - das bestimmt die Größe des PDF-Bildes in Pixeln. IronOCR wird dies lesen.

Option 2 (idealer Anwendungsfall)

  1. Verwenden Sie OcrInput.LoadPdf() mit Ihrer PDF-Vorlage.
  2. Verwenden Sie OcrInput.GetPages(), um die Breite und Höhe des Eingangs zu erhalten.
  3. Verwenden Sie OcrInput.GetPages().First().ToBitmap(), um das genaue Bild zu erhalten, das die OCR-Engine lesen wird.
  4. Sie können nun Inhaltbereiche in Pixeln vom exportierten Bild messen.
  5. Die angepeilten Koordinaten könnten für einen bestimmten OCR-Bereich verwendet werden (siehe im Endergebnis).

Um Ihre Informationen zu erhalten:

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

Endergebnis:

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

API-Referenz: OcrInput| OcrInput.Page

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen
Bereit anzufangen?
Nuget Downloads 5,044,537 | Version: 2025.11 gerade veröffentlicht