How to Read Bookmarks in PDF File using C++

In this tutorial, we will learn how to read bookmarks in PDF file using C++. You can extract the title and different other properties by iterating through all the bookmarks one after another using simple API calls in C++. The feature does not need installation of Adobe Acrobat or any application on Windows or Linux platforms.

Steps to Read Bookmarks in PDF File using C++

  1. Install the Aspose.Pdf for C++ library from NuGet package manager
  2. Add the reference to Aspose::Pdf namespace
  3. Initialize an object of PdfBookmarkEditor class to get bookmarks
  4. Load the input PDF document for extracting bookmarks
  5. Retrieve the Bookmarks from the PDF file
  6. Iterate through all the Bookmarks and read properties

You can read bookmarks in pdf file using C++ with few simple lines of code. It can retrieve bookmarks in pdf file using C++ along with the different properties of each bookmark.

Code to Read Bookmarks in PDF File using C++

#pragma once
#include <iostream>
#include <system/smart_ptr.h>
#include <Aspose.PDF.Cpp/License.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Facades/PdfBookmarkEditor.h>
#include <Aspose.PDF.Cpp/Facades/Bookmark.h>
#include <Aspose.PDF.Cpp/Facades/Bookmarks.h>
#include <system/console.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
class ReadBookmarkEx{
public:
void ReadPDFBookmark()
{
// Set the license for Aspose.PDF for C++ to Read the bookmarks in PDF file
SharedPtr<License> ReadPdfBookmarkLicense = System::MakeObject<License>();
ReadPdfBookmarkLicense->SetLicense(u"Aspose.Pdf.lic");
// Initialize an object of PdfBookmarkEditor class to get bookmarks
SharedPtr<PdfBookmarkEditor> BookmarkEditor = System::MakeObject<PdfBookmarkEditor>();
// Load the input PDF document for extracting bookmarks
BookmarkEditor->BindPdf(u"BookmarkSample.pdf");
// Retrieve the Bookmarks from the PDF
SharedPtr <Bookmarks> bookmarks = BookmarkEditor->ExtractBookmarks();
// Iterate through all the Bookmarks
for (SharedPtr<Bookmark> bookmark : bookmarks)
{
// Read different properties of the bookmark
Console::WriteLine(bookmark->get_Title());
Console::WriteLine(bookmark->get_ItalicFlag());
Console::WriteLine(bookmark->get_BoldFlag());
Console::WriteLine(bookmark->get_TitleColor().ToString());
}
}
};

In the previous topic, we learned How to Read PDF File in C++. This topic covers how to extract bookmarks in pdf file using C++.

 English