C#/VB.NET: Add, Edit, or Delete Bookmarks in PDF

PDF bookmarks are navigational aids that allow users to quickly locate and jump to specific sections or pages in a PDF document. Through a simple click, users can arrive at the target location, which eliminates the need to manually scroll or search for specific content in a lengthy document. In this article, you will learn how to programmatically add, modify and remove bookmarks in PDF 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 

Add Bookmarks to a PDF Document in C# and VB.NET

Spire.PDF for .NET provides the PdfDocument.Bookmarks.Add() method to add bookmarks to a PDF document. After a bookmark is created, you can also use the PdfBookmark.Add() method to add sub-bookmarks for it.  The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Loop through all pages in the PDF file to add bookmarks and set their styles.
  • Add a parent bookmark to the document using PdfDocument.Bookmarks.Add() method.
  • Create a PdfDestination object and set the destination of the parent bookmark using PdfBookmark.Action property.
  • Set the text color and style of the parent bookmark.
  • Add a sub-bookmark to the parent bookmark using PdfBookmark.Add() method.
  • Set the destination, text color, and text style of the sub-bookmark.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Actions;
using Spire.Pdf.Bookmarks;
using Spire.Pdf.General;

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

            //Load a sample PDF file
            pdf.LoadFromFile("template.pdf");

            //Loop through the pages in the PDF file
            for (int i = 0; i < pdf.Pages.Count; i++)
            {
                PdfPageBase page = pdf.Pages[i];

                //Add a bookmark
                PdfBookmark bookmark = pdf.Bookmarks.Add(string.Format("Bookmark-{0}", i + 1));

                //Set the destination page and location of the bookmark
                PdfDestination destination = new PdfDestination(page, new PointF(0, 0));
                bookmark.Action = new PdfGoToAction(destination);

                //Set the text color and style of the bookmark
                bookmark.Color = new PdfRGBColor(Color.Black);
                bookmark.DisplayStyle = PdfTextStyle.Bold;

                //Add a child bookmark
                PdfBookmark childBookmark = bookmark.Add(string.Format("Sub-Bookmark-{0}", i + 1));

                //Set the destination page and location of the child bookmark
                PdfDestination childDestination = new PdfDestination(page, new PointF(0, 100));
                childBookmark.Action = new PdfGoToAction(childDestination);

                //Set the text color and style of the child bookmark
                childBookmark.Color = new PdfRGBColor(Color.Brown);
                childBookmark.DisplayStyle = PdfTextStyle.Italic;
            }

            // Save the result file
            pdf.SaveToFile("AddBookmarks.pdf");
        }
    }
}

C#/VB.NET: Add, Edit, or Delete Bookmarks in PDF

Edit Bookmarks in a PDF Document in C# and VB.NET

If you need to update the existing bookmarks, you can use the methods of PdfBookmark class to rename the bookmarks and change their text color, text style. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Get a specified bookmark using PdfDocument.Bookmarks[] property.
  • Change the title of the bookmark using PdfBookmark.Title property.
  • Change the font color of the bookmark using PdfBookmark.Color property.
  • Change the text style of the bookmark using PdfBookmark.DisplayStyle property.
  • Change the text color and style of the sub-bookmark using the above methods.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Bookmarks;
using System.Drawing;


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

            //Load a sample PDF file
            pdf.LoadFromFile("AddBookmarks.Pdf");

            //Get the first bookmark
            PdfBookmark bookmark = pdf.Bookmarks[0];
            
            //Change the title of the bookmark
            bookmark.Title = "Modified BookMark";

            //Change text color and style of the bookmark
            bookmark.Color = Color.Red;
            bookmark.DisplayStyle = PdfTextStyle.Italic;

            //Edit sub-bookmarks of the first bookmark
            foreach (PdfBookmark childBookmark in bookmark)
            {
                childBookmark.Color = Color.Blue;
                childBookmark.DisplayStyle = PdfTextStyle.Bold;
            }
        
            //Save the result file
            pdf.SaveToFile("EditBookmarks.Pdf");
        }
    }
}

C#/VB.NET: Add, Edit, or Delete Bookmarks in PDF

Delete Bookmarks from a PDF Document in C# and VB.NET

Spire.PDF for .NET allows to remove a specified bookmark as well as all bookmarks in a PDF file. Furthermore, removing only a specified sub-bookmark can also be achieved. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Get the first bookmark using PdfDocument.Bookmarks[] property.
  • Remove a specified sub-bookmark of the first bookmark using PdfBookmark.RemoveAt() method.
  • Remove a specified bookmark including its sub-bookmarks using PdfDocument.Bookmarks.RemoveAt() method. Or you can remove all bookmarks in the PDF file using PdfDocument.Bookmarks.Clear() method.
  • Save the document using PdfDocument.saveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Bookmarks;

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

            //Load a sample PDF file
            pdf.LoadFromFile("AddBookmarks.Pdf");

            //Get the first bookmark
            PdfBookmark bookmark = pdf.Bookmarks[0];

            //Delete the first sub-bookmark of the first bookmark
            bookmark.RemoveAt(0);

            //Delete the second bookmark including its sub-bookmarks
            pdf.Bookmarks.RemoveAt(1);

            //Delete all bookmarks
            //pdf.Bookmarks.Clear();

            //Save the result file
            pdf.SaveToFile("DeleteBookmarks.pdf");
        }
    }
}

C#/VB.NET: Add, Edit, or Delete Bookmarks in PDF

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.