Add and delete PDF bookmarks in C++
PDF bookmarks are entries in a document’s outline tree. These examples add a bookmark targeting page 1, construct a parent-child hierarchy, and remove outline entries.
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.
Add a bookmark
Create a bold, italic Test Outline bookmark targeting page 1.
- Open the input PDF with Document.
- Create an OutlineItemCollection in the document outline tree.
- Set its title, italic flag, and bold flag.
- Assign a GoToAction targeting page 1.
- Add the item with OutlineCollection::Add().
- Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/GoToAction.h>
#include <Aspose.PDF.Cpp/Outlines/OutlineCollection.h>
#include <Aspose.PDF.Cpp/Outlines/OutlineItemCollection.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Annotations;
void AddBookmark(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto pdfOutline = MakeObject<OutlineItemCollection>(document->get_Outlines());
pdfOutline->set_Title(u"Test Outline");
pdfOutline->set_Italic(true);
pdfOutline->set_Bold(true);
pdfOutline->set_Action(MakeObject<GoToAction>(document->get_Pages()->idx_get(1)));
document->get_Outlines()->Add(pdfOutline);
document->Save(outputFile);
}
Add a child bookmark
Create Parent Outline with a nested Child Outline, both bold and italic. This sample builds the hierarchy without assigning a destination or action to either item.
- Open the input PDF with Document.
- Create parent and child OutlineItemCollection objects.
- Set their titles and text styles.
- Add the child to the parent with OutlineItemCollection::Add().
- Add the parent to the document Outlines.
- 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 AddChildBookmark(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto pdfOutline = MakeObject<OutlineItemCollection>(document->get_Outlines());
pdfOutline->set_Title(u"Parent Outline");
pdfOutline->set_Italic(true);
pdfOutline->set_Bold(true);
auto pdfChildOutline = MakeObject<OutlineItemCollection>(document->get_Outlines());
pdfChildOutline->set_Title(u"Child Outline");
pdfChildOutline->set_Italic(true);
pdfChildOutline->set_Bold(true);
pdfOutline->Add(pdfChildOutline);
document->get_Outlines()->Add(pdfOutline);
document->Save(outputFile);
}
Delete a bookmark by title
Delete the outline entry named Child Outline using the title-based overload.
- Open the input PDF with Document.
- Get the document Outlines.
- Call OutlineCollection::Delete() with Child Outline.
- 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 DeleteBookmark(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
document->get_Outlines()->Delete(u"Child Outline");
document->Save(outputFile);
}
Delete all bookmarks
Remove the document outline entries with the no-argument Delete() overload.
- Open the input PDF with Document.
- Call OutlineCollection::Delete() on the document outline collection.
- 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 DeleteBookmarks(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
document->get_Outlines()->Delete();
document->Save(outputFile);
}