Add Bookmark to PDF via C++

Manipulate Bookmarks in PDF document. Use Aspose.PDF for C++ to modify PDF documents programmatically

How to work with Bookmarks in PDF Document with C++ Library

Use the Aspose.PDF for C++ for working with bookmarks. With bookmarks in PDF, you can quickly navigate to specific sections or pages, making it easier to search and access relevant content. Bookmarks in PDF improve the user interface by allowing you to navigate long or complex documents efficiently. Well-structured bookmarks in PDF can improve the appearance of a professional document. This is especially useful for textbooks, manuals, scientific papers, and reports. In presentations, bookmarks allow the user to seamlessly navigate between slides or sections. This feature can be useful for interactive and non-linear presentations. Working with bookmarks in PDF documents improves navigation, accessibility, and overall user experience. Use this feature in educational materials, scientific papers, manuals, or presentations. Bookmarks in PDF improve the quality, efficiency, and browsing experience of your documents. In order to add bookmarks in PDF file, we’ll use Aspose.PDF for C++ API which is a feature-rich, powerful and easy to use document manipulation API for cpp platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

Steps to work with Bookmarks via C++


You need Aspose.PDF for C++ to try the code in your environment.

  1. Open a PDF document using Document object.
  2. Create a bookmark and define its properties.
  3. Add the OutlineItemCollection collection to the Outlines collection.
  4. Save the file again

This example opens a PDF document and creates a bookmark with certain properties via C++. It sets the target page for the bookmark using a ‘GoToAction’ object. After, add it to the document structure using ‘OutlineItemCollection’, and then save the modified document bookmark to a new PDF file. This is the best solution for improving the documents navigation functions.

Add a Bookmark to PDF Document - C++

This sample code shows how to add bookmark to PDF - C++

auto pdfOutline = MakeObject<OutlineItemCollection>(pdfDocument->get_Outlines());
pdfOutline->set_Title(u"Test Outline");
pdfOutline->set_Italic(true);
pdfOutline->set_Bold(true);

// Set the destination page number
pdfOutline->set_Action(
    MakeObject<Aspose::Pdf::Annotations::GoToAction>
        (pdfDocument->get_Pages()->idx_get(2))
    );

// Add a bookmark in the document's outline collection.
pdfDocument->get_Outlines()->Add(pdfOutline);

// Save the update document
pdfDocument->Save(_dataDir + u"AddBookmark_out.pdf");