Region-based text extraction in C++
Contents
[
Hide
]
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.
- Open the input PDF as a Document.
- Create a TextAbsorber and TextSearchOptions using the supplied Rectangle.
- Enable LimitToPageBounds and assign the search options.
- Select pageNumber and call Accept() with the absorber.
- 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());
}