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.
- Open the input PDF with Document.
- Iterate over the one-based OutlineCollection indexes.
- 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.
- Open the input PDF with Document.
- Iterate over the document Outlines.
- Read each OutlineItemCollection and print its title and style.
- 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.
- Create a PdfBookmarkEditor and bind the input PDF.
- Call ExtractBookmarks().
- Read each Bookmark level, title, page number, and action and print them.
- 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.
- Open the input PDF with Document.
- Get the first OutlineItemCollection from the document outline tree.
- Select its first child using idx_get().
- Set the child title, italic flag, and bold flag.
- 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.
- Open the input PDF with Document.
- Set Document::set_PageMode() to UseOutlines.
- Iterate over the top-level OutlineCollection.
- Set OutlineItemCollection::set_Open() to true for each item.
- 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);
}