Inspect tagged PDF content in C++

Access the tagged content container and inspect the logical structure of PDF documents with Aspose.PDF for C++. The examples show metadata initialization, structure root access, and inspection and modification of child elements.

Get the tagged content container

Access tagged content and define the document metadata.

This example creates a new document; it does not extract text from an existing PDF.

  1. Create a Document and get its ITaggedContent with get_TaggedContent().
  2. Set the document title and language with SetTitle() and SetLanguage().
  3. Save the document with Save().

#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Tagged/ITaggedContent.h>
#include <Aspose.PDF.Cpp/Tagged/LogicalStructure/StructureElement.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Tagged;
using namespace Aspose::Pdf::LogicalStructure;

void GetTaggedContent(System::String outputFile)
{
    auto document = MakeObject<Document>();
    auto taggedContent = document->get_TaggedContent();
    taggedContent->SetTitle(u"Simple Tagged Pdf Document");
    taggedContent->SetLanguage(u"en-US");
    document->Save(outputFile);
}

Access the structure roots

Retrieve the structure-tree root and the document root element.

  1. Create a Document and get its ITaggedContent with get_TaggedContent().
  2. Set the document title and language with SetTitle() and SetLanguage().
  3. Get the structure-tree root with get_StructTreeRootElement().
  4. Get the document root with get_RootElement().
  5. Print the string representations of both roots.
  6. Save the document with Save().

#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Tagged/ITaggedContent.h>
#include <Aspose.PDF.Cpp/Tagged/LogicalStructure/Element.h>
#include <Aspose.PDF.Cpp/Tagged/LogicalStructure/ElementList.h>
#include <Aspose.PDF.Cpp/Tagged/LogicalStructure/StructTreeRootElement.h>
#include <Aspose.PDF.Cpp/Tagged/LogicalStructure/StructureElement.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Tagged;
using namespace Aspose::Pdf::LogicalStructure;

void GetRootStructure(System::String outputFile)
{
    auto document = MakeObject<Document>();
    auto taggedContent = document->get_TaggedContent();
    taggedContent->SetTitle(u"Tagged Pdf Document");
    taggedContent->SetLanguage(u"en-US");

    std::cout << "StructTreeRootElement: " << taggedContent->get_StructTreeRootElement()->ToString() << std::endl;
    std::cout << "RootElement: " << taggedContent->get_RootElement()->ToString() << std::endl;

    document->Save(outputFile);
}

Inspect and update child elements

Read accessibility properties from an existing structure tree and update nested elements.

The sample assumes that the root contains a child at index 1. Inspect the input structure before applying the same indexing to another document.

  1. Open the tagged input PDF as a Document and get its ITaggedContent.
  2. Read the structure-tree root children with get_ChildElements().
  3. Cast each applicable child to StructureElement and print its title, language, actual text, expansion text, and alternative text.
  4. Select the root child at index 1 and iterate through its children.
  5. Update the metadata of each applicable child.
  6. Save the document with Save().

#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Tagged/ITaggedContent.h>
#include <Aspose.PDF.Cpp/Tagged/LogicalStructure/Element.h>
#include <Aspose.PDF.Cpp/Tagged/LogicalStructure/ElementList.h>
#include <Aspose.PDF.Cpp/Tagged/LogicalStructure/StructTreeRootElement.h>
#include <Aspose.PDF.Cpp/Tagged/LogicalStructure/StructureElement.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Tagged;
using namespace Aspose::Pdf::LogicalStructure;

void AccessChildElements(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto taggedContent = document->get_TaggedContent();

    auto elementList = taggedContent->get_StructTreeRootElement()->get_ChildElements();
    for (const auto& element : elementList) {
        if (auto structureElement = System::DynamicCast_noexcept<StructureElement>(element)) {
            std::cout << "StructureElement properties - "
            << "title: " << structureElement->get_Title()
            << ", language: " << structureElement->get_Language()
            << ", actual_text: " << structureElement->get_ActualText()
            << ", expansion_text: " << structureElement->get_ExpansionText()
            << ", alternative_text: " << structureElement->get_AlternativeText() << std::endl;
        }
    }

    auto firstChild = taggedContent->get_RootElement()->get_ChildElements()->idx_get(1);
    for (const auto& element : firstChild->get_ChildElements()) {
        if (auto structureElement = System::DynamicCast_noexcept<StructureElement>(element)) {
            structureElement->set_Title(u"title");
            structureElement->set_Language(u"fr-FR");
            structureElement->set_ActualText(u"actual text");
            structureElement->set_ExpansionText(u"exp");
            structureElement->set_AlternativeText(u"alt");
        }
    }

    document->Save(outputFile);
}