Get, Update and Expand a Bookmark

The following code snippet also work with Aspose.PDF.Drawing library.

Get Bookmarks

The Document object’s OutlineCollection collection contains all a PDF file’s bookmarks. This article explains how to get bookmarks from a PDF file, and how to get which page a particular bookmark is on.

To get the bookmarks, loop through the OutlineCollection collection and get each bookmark in the OutlineItemCollection. The OutlineItemCollection provides access to all the bookmark’s attributes. The following code snippet shows you how to get bookmarks from the PDF file.

Getting a Bookmark’s Page Number

Once you have added a bookmark you can find out what page it is on by getting the destination PageNumber associated with the Bookmark object.

Get Child Bookmarks from a PDF Document

Bookmarks can be organized in a hierarchical structure, with parents and children. To get all bookmarks, loop through the Document object’s Outlines collections. However, to get child bookmarks as well, also loop through all the bookmarks in each OutlineItemCollection object obtained in the first loop. The following code snippets show how to get child bookmarks from a PDF document.

Update Bookmarks in a PDF Document

To update a bookmark in a PDF file, first, get the particular bookmark from the Document object’s OutlineColletion collection by specifying the bookmark’s index. Once you have retrieved the bookmark into OutlineItemCollection object, you can update its properties and then save the updated PDF file using the Save method. The following code snippets show how to update bookmarks in a PDF document.

Update Child Bookmarks in a PDF Document

To update a child bookmark:

  1. Retrieve the child bookmark you want to update from the PDF file by first getting the parent bookmark and then the child bookmark using appropriate index values.
  2. Save the updated PDF file using the Save method.

The following code snippet shows you how to update child bookmarks in a PDF document.

Expanded Bookmarks when viewing document

Bookmarks are held in the Document object’s OutlineItemCollection collection, itself in the OutlineCollection collection. However, we may have a requirement to have all the bookmarks expanded when viewing the PDF file.

In order to accomplish this requirement, we can set open status for each outline/bookmark item as Open. The following code snippet shows you how to set the open status for each bookmark as expanded in a PDF document.