Region-based text extraction in C++

Extract text from a rectangle

Limit extraction to a supplied region on a selected page.

Supply a valid page number and rectangle in the page coordinate system.

  1. Open the input PDF as a Document.
  2. Create a TextAbsorber and TextSearchOptions using the supplied Rectangle.
  3. Enable LimitToPageBounds and assign the search options.
  4. Select pageNumber and call Accept() with the absorber.
  5. Write the extracted text to the output file.

#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Text/TextAbsorber.h>
#include <Aspose.PDF.Cpp/Text/TextOptions/TextSearchOptions.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;


void ExtractTextFromRegion(
    System::String inputFile,
    System::String outputFile,
    int pageNumber,
    System::SharedPtr<Rectangle> rectangle)
{
    auto document = MakeObject<Document>(inputFile);
    auto absorber = MakeObject<TextAbsorber>();
    auto options = MakeObject<TextSearchOptions>(rectangle);
    options->set_LimitToPageBounds(true);
    absorber->set_TextSearchOptions(options);
    document->get_Pages()->idx_get(pageNumber)->Accept(absorber);
    System::IO::File::WriteAllText(outputFile, absorber->get_Text());
}