Python: 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 delete bookmarks in PDF files using Spire.PDF for Python.

Install Spire.PDF for Python

This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.PDF

If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows

Add Bookmarks to a PDF Document in Python

Spire.PDF for Python provides a method to add bookmarks to a PDF document: PdfDocument. Bookmarks.Add(). You can use this method to create primary bookmarks for the PDF document and use the PdfBookmarkCollection.Add() method to add sub-bookmarks to the primary bookmarks. Additionally, the PdfBookmark class offers other methods to set properties such as destination, text color, and text style for the bookmarks. The following are the detailed steps for adding bookmarks to a PDF document.

  • Create a PdfDocument class instance.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Add a parent bookmark to the document using PdfDocument.Bookmarks.Add() method.
  • Create a PdfDestination class object and set the destination of the parent bookmark using PdfBookmark.Action property.
  • Set the text color and style of the parent bookmark.
  • Create a PdfBookmarkCollection class object to add sub-bookmark to the parent bookmark using PdfBookmarkCollection.Add() method.
  • Use the above methods to set the destination, text color, and text style of the sub-bookmark.
  • Save the document using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
doc = PdfDocument()

# Load a PDF file
doc.LoadFromFile("Terms of service.pdf")

# Loop through the pages in the PDF file
for i in range(doc.Pages.Count):
    page = doc.Pages[i]
    
    # Set the title and destination for the bookmark
    bookmarkTitle = "Bookmark-{0}".format(i+1)
    bookmarkDest = PdfDestination(page, PointF(0.0, 0.0))
    
    # Create and configure the bookmark
    bookmark = doc.Bookmarks.Add(bookmarkTitle)
    bookmark.Color = PdfRGBColor(Color.get_SaddleBrown())
    bookmark.DisplayStyle = PdfTextStyle.Bold
    bookmark.Action = PdfGoToAction(bookmarkDest)
    
    # Create a collection to hold child bookmarks
    bookmarkColletion = PdfBookmarkCollection(bookmark)
    
    # Set the title and destination for the child bookmark
    childBookmarkTitle = "Sub-Bookmark-{0}".format(i+1)
    childBookmarkDest = PdfDestination(page, PointF(0.0, 100.0))
    
    # Create and configure the child bookmark
    childBookmark = bookmarkColletion.Add(childBookmarkTitle)
    childBookmark.Color = PdfRGBColor(Color.get_Coral())
    childBookmark.DisplayStyle = PdfTextStyle.Italic
    childBookmark.Action = PdfGoToAction(childBookmarkDest)

# Save the PDF file
outputFile = "Bookmark.pdf"
doc.SaveToFile(outputFile)

# Close the document
doc.Close()

Python: Add, Edit, or Delete bookmarks in PDF

Edit Bookmarks in a PDF Document

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 class instance.
  • Load a PDF document 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.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
doc = PdfDocument()

# Load a PDF file
doc.LoadFromFile("Bookmark.pdf")

# Get the first bookmark
bookmark = doc.Bookmarks[0]

# Change the title of the bookmark
bookmark.Title = "Modified BookMark"

# Set the color of the bookmark
bookmark.Color = PdfRGBColor(Color.get_Black())

# Set the outline text style of the bookmark
bookmark.DisplayStyle = PdfTextStyle.Bold

# Edit child bookmarks of the parent bookmark
pBookmark = PdfBookmarkCollection(bookmark)
for i in range(pBookmark.Count):
    childBookmark = pBookmark.get_Item(i)
    childBookmark.Color = PdfRGBColor(Color.get_Blue())
    childBookmark.DisplayStyle = PdfTextStyle.Regular
    
# Save the PDF document
outputFile = "EditBookmark.pdf"

# Close the document
doc.SaveToFile(outputFile)

Python: Add, Edit, or Delete bookmarks in PDF

Delete Bookmarks from a PDF Document

Spire.PDF for Python also provides methods to delete any bookmark in a PDF document. PdfDocument.Bookmarks.RemoveAt() method is used to remove a specific primary bookmark, PdfDocument.Bookmarks.Clear() method is used to remove all bookmarks, and PdfBookmarkCollection.RemoveAt() method is used to remove a specific sub-bookmark of a primary bookmark. The detailed steps of removing bookmarks form a PDF document are as follows.

  • Create a PdfDocument class instance.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the first bookmark using PdfDocument.Bookmarks[] property.
  • Remove a specified sub-bookmark of the first bookmark using PdfBookmarkCollection.RemoveAt() method.
  • Remove a specified bookmark including its sub-bookmarks using PdfDocument.Bookmarks.RemoveAt() method.
  • Remove all bookmarks in the PDF file using PdfDocument.Bookmarks.Clear() method.
  • Save the document using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
doc = PdfDocument()

# Load a PDF file
doc.LoadFromFile("Bookmark.pdf")

# # Delete the first bookmark
# doc.Bookmarks.RemoveAt(0)

# # Get the first bookmark
# bookmark = doc.Bookmarks[0]

# # Remove the first child bookmark from first parent bookmark
# pBookmark = PdfBookmarkCollection(bookmark)
# pBookmark.RemoveAt(0)

#Remove all bookmarks
doc.Bookmarks.Clear()

# Save the PDF document
output = "DeleteAllBookmarks.pdf"
doc.SaveToFile(output)

# Close the document
doc.Close()

Python: 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.