Przycinanie regionów i prostokątów za pomocą IronOCR
Jak ustawić obszary treści na PDF-ach za pomocą IronOCR?
ObszaryTreści i PDFy
Metody OcrInput.LoadPdf i LoadPdfPage mają opcję dodania ObszaruTreści.
Pytanie - Skąd wiem, jak duży jest mój obszar treści, skoro PDF-y nie są rozmiarowane w pikselach, a obszary treści są generalnie mierzone w nich?
Opcja 1
Domyślna wartość OcrInput.TargetDPI to 225 - wyznacza ona rozmiar obrazu PDF w pikselach. IronOCR to odczyta.
Opcja 2 (idealny przypadek użycia)
- Użyj
OcrInput.LoadPdf()z szablonem PDF. - Użyj
OcrInput.GetPages(), aby uzyskać szerokość i wysokość wejścia. - Użyj
OcrInput.GetPages().First().ToBitmap(), aby uzyskać dokładny obraz, który silnik OCR będzie czytał. - Teraz można zmierzyć ObszaryTreści w pikselach z wyeksportowanego obrazu.
- Docelowe współrzędne mogą być użyte dla konkretnego regionu OCR (zobacz w Finalnym Wyniku).
Aby uzyskać swoje informacje:
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
Finalny wynik:
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
Dokumentacja API: OcrInput|OcrInput.Page

