C#/VB.NET: Get Bookmarks of PDF Documents

Bookmark is a helpful feature of PDF, especially for PDF documents with lots of pages. By clicking on a bookmark, the reader can jump to the corresponding location of the document quickly. A series of organized bookmarks can be used as contents as well. This article demonstrates how to get bookmarks from PDF documents using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Get the Bookmarks of a PDF Document

The detailed steps are as follows:

  • Create a PdfDocument instance.
  • Load a PDF document from disk using PdfDocument.LoadFromFile() method.
  • Get bookmarks collection in the PDF document using PdfDocument.Bookmarks property.
  • Get the bookmarks’ content and save to a TXT file using custom method GetBookmarks().
  • C#
  • VB.NET
using System;
using System.IO;
using System.Text;
using Spire.Pdf;
using Spire.Pdf.Bookmarks;

namespace GetBookmark
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a PDF document from disk
            pdf.LoadFromFile(@"D:\testp\test.pdf");

            //Get bookmark collection of the Pdf document
            PdfBookmarkCollection bookmarks = pdf.Bookmarks;

            //Get the bookmarks and save them to a TXT file
            String result = "GetPdfBookmarks.txt";
            GetBookmarks(bookmarks, result);

        }

        public static void GetBookmarks(PdfBookmarkCollection bookmarks, string result)
        {
            //Create an object of StringBuilder
            StringBuilder content = new StringBuilder();

            //Get PDF bookmarks’ information
            if (bookmarks.Count > 0)
            {
                content.AppendLine("Pdf bookmarks:");
                foreach (PdfBookmark parentBookmark in bookmarks)
                {
                    //Get the title
                    content.AppendLine(parentBookmark.Title);

                    //Get the text style
                    string textStyle = parentBookmark.DisplayStyle.ToString();
                    content.AppendLine(textStyle);
                    GetChildBookmark(parentBookmark, content);
                }
            }

            //Save to a TXT file
            File.WriteAllText(result, content.ToString());
        }
        public static void GetChildBookmark(PdfBookmark parentBookmark, StringBuilder content)
        {
            if (parentBookmark.Count > 0)
            {
                foreach (PdfBookmark childBookmark in parentBookmark)
                {
                    //Get the title
                    content.AppendLine(childBookmark.Title);

                    //Get the text style
                    string textStyle = childBookmark.DisplayStyle.ToString();
                    content.AppendLine(textStyle);
                    GetChildBookmark(childBookmark, content);
                }
            }

        }
    }
}

C#/VB.NET: Get Bookmarks of PDF Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.