Basic text extraction in C++

Extract text from all pages, the first page, or detected paragraphs using Aspose.PDF for C++.

Extract text from all pages

Collect document text into one output file.

  1. Open the input PDF as a Document.
  2. Create a TextAbsorber.
  3. Pass the absorber to the page collection’s Accept() method.
  4. Read the absorber’s Text property.
  5. Write the extracted text to the output file.

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

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

void ExtractTextFromAllThePages(System::String path_infile, System::String path_outfile) {


    // Open document
    auto document = MakeObject<Document>(path_infile);

    // Create TextAbsorber object to extract text
    auto textAbsorber = MakeObject<TextAbsorber>();
    // Accept the absorber for all the pages
    document->get_Pages()->Accept(textAbsorber);
    // Get the extracted text
    auto extractedText = textAbsorber->get_Text();

    System::IO::File::WriteAllText(path_outfile, extractedText);
}