Regiões de cultivo e retângulos com IronOCR

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

Como faço para definir áreas de conteúdo em PDFs com o IronOCR?

Áreas de conteúdo e PDFs

OcrInput.LoadPdf e LoadPdfPage métodos todos têm a opção de adicionar uma ContentArea.

A pergunta é: como sei qual o tamanho da minha área de conteúdo, já que os PDFs não são dimensionados em pixels, mas as áreas de conteúdo geralmente são medidas neles?

Opção 1

O padrão OcrInput.TargetDPI é 225 - isso dita o tamanho da imagem PDF em pixels. O IronOCR conseguirá ler isto.

Opção 2 (caso de uso ideal)

  1. Use OcrInput.LoadPdf() com seu modelo de PDF.
  2. Use OcrInput.GetPages() para obter a largura e altura da entrada.
  3. Use OcrInput.GetPages().First().ToBitmap() para obter a imagem exata que o motor OCR vai ler.
  4. Agora você pode medir as áreas de conteúdo em pixels a partir da imagem exportada.
  5. As coordenadas selecionadas podem ser usadas para uma região OCR específica (ver em Resultado Final).

Para obter suas informações:

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");
}
$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);
}
$vbLabelText   $csharpLabel

Referência da API: OcrInput | OcrInput.Página

Curtis Chau
Redator Técnico

Curtis Chau é bacharel em Ciência da Computação (Universidade Carleton) e se especializa em desenvolvimento front-end, com experiência em Node.js, TypeScript, JavaScript e React. Apaixonado por criar interfaces de usuário intuitivas e esteticamente agradáveis, Curtis gosta de trabalhar com frameworks modernos e criar manuais ...

Leia mais
Pronto para começar?
Nuget Downloads 5,525,971 | Versão: 2026.3 acaba de ser lançado
Still Scrolling Icon

Ainda está rolando a tela?

Quer provas rápidas? PM > Install-Package IronOcr
executar um exemplo Veja sua imagem se transformar em texto pesquisável.