Get, update, and expand PDF bookmarks in C++

Use these examples to inspect outline properties or change existing bookmarks. The update example requires a first top-level entry with at least one child.

Configure the Aspose.PDF for C++ SDK and create the output directory before running examples that save a file. The snippets retain the source includes, function parameters, coordinates, and action settings, with the project runner header omitted.

Read top-level bookmarks

Print each top-level bookmark’s title, italic flag, bold flag, and color.

  1. Open the input PDF with Document.
  2. Iterate over the one-based OutlineCollection indexes.
  3. Read and print the OutlineItemCollection title and style properties.
#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Outlines/OutlineCollection.h>
#include <Aspose.PDF.Cpp/Outlines/OutlineItemCollection.h>

using namespace System;
using namespace Aspose::Pdf;

void GetBookmarks(System::String inputFile)
{
    auto document = MakeObject<Document>(inputFile);
    for (int i = 1; i <= document->get_Outlines()->get_Count(); i++) {
        auto outlineItem = document->get_Outlines()->idx_get(i);
        std::cout << outlineItem->get_Title() << std::endl;
        std::cout << outlineItem->get_Italic() << std::endl;
        std::cout << outlineItem->get_Bold() << std::endl;
        std::cout << outlineItem->get_Color().ToString() << std::endl;
    }
}

Read child bookmarks

Print top-level bookmark properties and the same properties for their immediate children. The example does not recurse into deeper levels.

  1. Open the input PDF with Document.
  2. Iterate over the document Outlines.
  3. Read each OutlineItemCollection and print its title and style.
  4. Enumerate the item’s immediate children and print their properties.
#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/Bookmarks.h>
#include <Aspose.PDF.Cpp/Outlines/OutlineCollection.h>
#include <Aspose.PDF.Cpp/Outlines/OutlineItemCollection.h>

using namespace System;
using namespace Aspose::Pdf;

void GetChildBookmarks(System::String inputFile)
{
    auto document = MakeObject<Document>(inputFile);
    for (int i = 1; i <= document->get_Outlines()->get_Count(); i++) {
        auto outlineItem = document->get_Outlines()->idx_get(i);
        std::cout << outlineItem->get_Title() << std::endl;
        std::cout << outlineItem->get_Italic() << std::endl;
        std::cout << outlineItem->get_Bold() << std::endl;
        std::cout << outlineItem->get_Color().ToString() << std::endl;
        int count = outlineItem->get_Count();
        if (count > 0) {
            std::cout << "Child Bookmarks" << std::endl;
            for (int j = 1; j <= outlineItem->get_Count(); j++) {
                auto childOutlineItem = outlineItem->idx_get(j);
                std::cout << childOutlineItem->get_Title() << std::endl;
                std::cout << childOutlineItem->get_Italic() << std::endl;
                std::cout << childOutlineItem->get_Bold() << std::endl;
                std::cout << childOutlineItem->get_Color().ToString() << std::endl;
            }
        }
    }
}

Read bookmark page numbers

Use PdfBookmarkEditor to extract bookmarks and print the title, target page, and action. Indentation is built from each bookmark’s Level.

  1. Create a PdfBookmarkEditor and bind the input PDF.
  2. Call ExtractBookmarks().
  3. Read each Bookmark level, title, page number, and action and print them.
  4. Close the PdfBookmarkEditor.
#include <iostream>
#include <Aspose.PDF.Cpp/Facades/Bookmark.h>
#include <Aspose.PDF.Cpp/Facades/Bookmarks.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfBookmarkEditor.h>
#include <Aspose.PDF.Cpp/Page.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void GetBookmarkPageNumber(System::String inputFile)
{
    auto bookmarkEditor = MakeObject<PdfBookmarkEditor>();

    bookmarkEditor->BindPdf(inputFile);
    for (const auto& bookmark : bookmarkEditor->ExtractBookmarks()) {
        String levelSeparator = u"";
        for (int i = 0; i < bookmark->get_Level(); i++) {
            levelSeparator += u"----";
        }

        std::cout << levelSeparator << " Title: " << bookmark->get_Title() << std::endl;
        std::cout << levelSeparator << " Page Number: " << bookmark->get_PageNumber() << std::endl;
        std::cout << levelSeparator << " Page Action: " << bookmark->get_Action() << std::endl;
    }

    bookmarkEditor->Close();
}

Update a child bookmark

Select the first child of the first top-level bookmark, rename it Updated Outline, and enable bold and italic text. The input must contain that hierarchy.

  1. Open the input PDF with Document.
  2. Get the first OutlineItemCollection from the document outline tree.
  3. Select its first child using idx_get().
  4. Set the child title, italic flag, and bold flag.
  5. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Outlines/OutlineCollection.h>
#include <Aspose.PDF.Cpp/Outlines/OutlineItemCollection.h>

using namespace System;
using namespace Aspose::Pdf;

void UpdateBookmarks(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto outline = document->get_Outlines()->idx_get(1);
    auto childOutline = outline->idx_get(1);
    childOutline->set_Title(u"Updated Outline");
    childOutline->set_Italic(true);
    childOutline->set_Bold(true);

    document->Save(outputFile);
}

Expand top-level bookmarks

Set the document page mode to UseOutlines and mark each top-level bookmark as open. The example does not recursively set Open on every descendant.

  1. Open the input PDF with Document.
  2. Set Document::set_PageMode() to UseOutlines.
  3. Iterate over the top-level OutlineCollection.
  4. Set OutlineItemCollection::set_Open() to true for each item.
  5. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/DOM/PageMode.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Outlines/OutlineCollection.h>
#include <Aspose.PDF.Cpp/Outlines/OutlineItemCollection.h>

using namespace System;
using namespace Aspose::Pdf;

void ExpandedBookmarks(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    document->set_PageMode(PageMode::UseOutlines);
    for (int i = 1; i <= document->get_Outlines()->get_Count(); i++) {
        auto item = document->get_Outlines()->idx_get(i);
        item->set_Open(true);
    }
    document->Save(outputFile);
}