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
- Add Bookmarks in PDF Files using C++
- Add Nested Bookmarks in PDF Files
- Read Bookmarks from a PDF File using C++
- Modify Bookmark Title using C++
- Delete a Particular Bookmark using C++
- Delete all Bookmarks in a PDF File using C++
- Get a Free License
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.
- Firstly, create an instance of the PdfBookmarkEditor class.
- Load the PDF document using the PdfBookmarkEditor->BindPdf(System::SharedPtr<Aspose::Pdf::Document> srcDoc) method.
- Create the bookmark using the PdfBookmarkEditor->CreateBookmarkOfPage(System::String bookmarkName, int32_t pageNumber) method.
- Save the PDF document using the PdfBookmarkEditor->Save(System::String destFile) method.
The following sample code demonstrates how to add a bookmark in a PDF file using C++.
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.
- Create an instance of the PdfBookmarkEditor class.
- Load the PDF document using the PdfBookmarkEditor->BindPdf(System::SharedPtr<Aspose::Pdf::Document> srcDoc) method.
- Create bookmarks using the Bookmark class.
- Add the child bookmarks to the instance of the Bookmarks class and set it as a child of the parent bookmark using the bookmark->set_ChildItems(System::SharedPtr<Bookmarks> value) method.
- Add the bookmarks to the PDF document using PdfBookmarkEditor->CreateBookmarks(System::SharedPtr<Bookmark> bookmark) method.
- Finally, save the PDF document using the PdfBookmarkEditor->Save(System::String destFile) method.
The following sample code shows how to add nested bookmarks in a PDF file using C++.
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.
- Create an instance of the PdfBookmarkEditor class.
- Load the PDF document using the PdfBookmarkEditor->BindPdf(System::SharedPtr<Aspose::Pdf::Document> srcDoc) method.
- Retrieve all bookmarks using the PdfBookmarkEditor->ExtractBookmarks() method.
- Loop through the bookmarks to retrieve individual bookmarks.
- Read the properties of the bookmarks.
The following sample code shows how to read bookmarks from a PDF file using C++.
Modify Bookmark Title using C++
The following are the steps to modify the title of an existing bookmark in a PDF file.
- Create an instance of the PdfBookmarkEditor class.
- Load the PDF document using the PdfBookmarkEditor->BindPdf(System::SharedPtr<Aspose::Pdf::Document> srcDoc) method.
- Modify the title of the bookmark using the PdfBookmarkEditor->ModifyBookmarks(System::String sTitle, System::String dTitle) method.
- Save the PDF document using the PdfBookmarkEditor->Save(System::String destFile) method.
The following sample code demonstrates how to modify the title of an existing bookmark in a PDF file.
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.
- Create an instance of the PdfBookmarkEditor class.
- Load the PDF document using the PdfBookmarkEditor->BindPdf(System::SharedPtr<Aspose::Pdf::Document> srcDoc) method.
- Delete the desired bookmark using the PdfBookmarkEditor->DeleteBookmarks(System::String title) method.
- Save the PDF document using the PdfBookmarkEditor->Save(System::String destFile) method.
The following sample code shows how to delete a particular bookmark using C++.
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.
- Create an instance of the PdfBookmarkEditor class.
- Load the PDF document using the PdfBookmarkEditor->BindPdf(System::SharedPtr<Aspose::Pdf::Document> srcDoc) method.
- Delete all the bookmarks using the PdfBookmarkEditor->DeleteBookmarks() method.
- Save the PDF document using the PdfBookmarkEditor->Save(System::String destFile) method.
The following is the sample code to delete all bookmarks in a PDF file.
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.