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.

  1. Open the input PDF with Document.
  2. Create an OutlineItemCollection in the document outline tree.
  3. Set its title, italic flag, and bold flag.
  4. Assign a GoToAction targeting page 1.
  5. Add the item with OutlineCollection::Add().
  6. 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.

  1. Open the input PDF with Document.
  2. Create parent and child OutlineItemCollection objects.
  3. Set their titles and text styles.
  4. Add the child to the parent with OutlineItemCollection::Add().
  5. Add the parent to the document Outlines.
  6. 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.

  1. Open the input PDF with Document.
  2. Get the document Outlines.
  3. Call OutlineCollection::Delete() with Child Outline.
  4. 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.

  1. Open the input PDF with Document.
  2. Call OutlineCollection::Delete() on the document outline collection.
  3. 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);
}