Crop Regions and Rectangles with IronOCR
How do I set content areas on PDFs with IronOCR?
ContentAreas and PDFs
OcrInput.LoadPdf
and LoadPdfPage
methods all have the option to add a ContentArea.
The question - How do I know how big my content area is as PDFs are not sized in pixels, but content areas are generally measured in them?
Option 1
OcrInput.TargetDPI
default is 225 - this dictates the size of the PDF image in pixels. IronOCR will read this.
Option 2 (ideal use case)
- Use
OcrInput.LoadPdf()
with your PDF template. - Use
OcrInput.GetPages()
to get the input's width and height. - Use
OcrInput.GetPages().First().ToBitmap()
to get the exact image the OCR engine will read. - You can now measure ContentAreas in pixels from the exported image.
- The targeted coordinates could be used for a specific OCR region (see in Final Result).
To get your info:
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
Final Result:
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
API Reference: OcrInput | OcrInput.Page