Bookmarks in a PDF file are helpful to mark points of interest in the document. These enable you to jump to a specific point in the document quickly. You might find yourself in situations where you need to add, read, modify or delete bookmarks in a PDF file programmatically. For that, this article will teach you how to work with bookmarks in PDF files using C++.

C++ API for Working with Bookmarks in PDF Files

Aspose.PDF for C++ is a C++ library that allows you to create, read and update PDF documents. Furthermore, the API supports adding, reading, modifying, and removing bookmarks from PDF files. You can either install the API through NuGet or download it directly from the downloads section.

PM> Install-Package Aspose.PDF.Cpp

Add Bookmarks in PDF Files using C++

The following are the steps to add a bookmark in a PDF file.

The following sample code demonstrates how to add a bookmark in a PDF file using C++.

// Create an instance of the PdfBookmarkEditor class
System::SharedPtr<Aspose::Pdf::Facades::PdfBookmarkEditor> editor = System::MakeObject<Aspose::Pdf::Facades::PdfBookmarkEditor>();
// Load the PDF document
editor->BindPdf(u"SourceDirectory\\Sample 1.pdf");
// Create the Bookmark
editor->CreateBookmarkOfPage(u"Bookmark for page 1", 1);
// Save the PDF document
editor->Save(u"OutputDirectory\\AddBookmark_out.pdf");
Output generated by the sample code

Output generated by the sample code

Add Nested Bookmarks in PDF Files

You can also add nested bookmarks using the Aspose.PDF for C++ API. The following are the steps for adding nested bookmarks.

The following sample code shows how to add nested bookmarks in a PDF file using C++.

// Create an instance of the PdfBookmarkEditor class
System::SharedPtr<Aspose::Pdf::Facades::PdfBookmarkEditor> editor = System::MakeObject<Aspose::Pdf::Facades::PdfBookmarkEditor>();
// Load the PDF document
editor->BindPdf(u"SourceDirectory\\Sample 1.pdf");
// Create 1st Child Bookmark
System::SharedPtr<Aspose::Pdf::Facades::Bookmark> bm1 = System::MakeObject<Aspose::Pdf::Facades::Bookmark>();
bm1->set_PageNumber(1);
bm1->set_Title(u"First child");
// Create 2nd Child Bookmark
System::SharedPtr<Aspose::Pdf::Facades::Bookmark> bm2 = System::MakeObject<Aspose::Pdf::Facades::Bookmark>();
bm2->set_PageNumber(2);
bm2->set_Title(u"Second child");
// Create Parent Bookmark
System::SharedPtr<Aspose::Pdf::Facades::Bookmark> bm = System::MakeObject<Aspose::Pdf::Facades::Bookmark>();
bm->set_Action(u"GoTo");
bm->set_PageNumber(1);
bm->set_Title(u"Parent");
// Set the Child Bookmarks
System::SharedPtr<Aspose::Pdf::Facades::Bookmarks> bms = System::MakeObject<Aspose::Pdf::Facades::Bookmarks>();
bms->Add(bm1);
bms->Add(bm2);
bm->set_ChildItem(bms);
// Add the Bookmarks to the PDF file
editor->CreateBookmarks(bm);
// Save the PDF document
editor->Save(u"OutputDirectory\\AddChildBookmark_out.pdf");
Output generated by the sample code

Output generated by the sample code

Read Bookmarks from a PDF File using C++

In order to read the bookmarks, use the PdfBookmarkEditor->ExtractBookmarks() method. The following are the steps to read bookmarks from a PDF file.

The following sample code shows how to read bookmarks from a PDF file using C++.

// Create an instance of the PdfBookmarkEditor class
System::SharedPtr<Aspose::Pdf::Facades::PdfBookmarkEditor> editor = System::MakeObject<Aspose::Pdf::Facades::PdfBookmarkEditor>();
// Load the PDF document
editor->BindPdf(u"SourceDirectory\\BookmarkSample.pdf");
// Retrieve all the Bookmarks
System::SharedPtr <Bookmarks> bms = editor->ExtractBookmarks();
// Loop through the Bookmarks
for (System::SharedPtr<Bookmark> bm : bms) {
// Display the title of the Bookmark
Console::WriteLine(bm->get_Title());
// Display the destination of the Bookmark
Console::WriteLine(bm->get_Destination());
}

Modify Bookmark Title using C++

The following are the steps to modify the title of an existing bookmark in a PDF file.

The following sample code demonstrates how to modify the title of an existing bookmark in a PDF file.

// Create an instance of the PdfBookmarkEditor class
System::SharedPtr<Aspose::Pdf::Facades::PdfBookmarkEditor> editor = System::MakeObject<Aspose::Pdf::Facades::PdfBookmarkEditor>();
// Load the PDF document
editor->BindPdf(u"SourceDirectory\\BookmarkSample.pdf");
// Modify Bookmark title
editor->ModifyBookmarks(u"bookmark for page 1", u"Page 1 Bookmark");
// Save the PDF document
editor->Save(u"OutputDirectory\\ModifyBookmark_out.pdf");

Delete a Particular Bookmark using C++

The API provides the PdfBookmarkEditor->DeleteBookmarks(System::String title) method to delete a particular bookmark from a PDF file. The following are the steps to delete a specific bookmark.

The following sample code shows how to delete a particular bookmark using C++.

// Create an instance of the PdfBookmarkEditor class
System::SharedPtr<Aspose::Pdf::Facades::PdfBookmarkEditor> editor = System::MakeObject<Aspose::Pdf::Facades::PdfBookmarkEditor>();
// Load the PDF document
editor->BindPdf(u"SourceDirectory\\BookmarkSample.pdf");
// Delete a particular Bookmark
editor->DeleteBookmarks(u"bookmark for page 1");
// Save the PDF document
editor->Save(u"OutputDirectory\\DeleteParticularBookmark_out.pdf");

Delete all Bookmarks in a PDF File using C++

Instead of deleting bookmarks one at a time, you can remove all the bookmarks at once using the PdfBookmarkEditor->DeleteBookmarks() method. The following are the steps to delete all bookmarks in a PDF file.

The following is the sample code to delete all bookmarks in a PDF file.

// Create an instance of the PdfBookmarkEditor class
System::SharedPtr<Aspose::Pdf::Facades::PdfBookmarkEditor> editor = System::MakeObject<Aspose::Pdf::Facades::PdfBookmarkEditor>();
// Load the PDF document
editor->BindPdf(u"SourceDirectory\\BookmarkSample.pdf");
// Delete all Bookmarks
editor->DeleteBookmarks();
// Save the PDF document
editor->Save(u"OutputDirectory\\DeleteAllBookmarks_out.pdf");

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to work with bookmarks in PDF files using C++. Specifically, you have learned how to add, read, modify and delete bookmarks in a PDF file. Furthermore, you have seen how to add nested bookmarks using the Aspose.PDF for C++ API. The API provides many additional features for working with PDF files that you can explore in detail by using the official documentation. If you have any questions, please feel free to contact us on the free support forum.

See Also