Add Bookmark to PDF using Python

Manipulate Bookmarks in PDF document. Use Aspose.PDF for Python for .NET to modify PDF documents programmatically

How to work with Bookmarks in PDF Document with Python Library

Use the Aspose.PDF for Python via .NET for working with bookmarks. With bookmarks in PDF, you can quickly navigate to specific sections or pages, making it easier to search and access relevant content. Bookmarks in PDF improve the user interface by allowing you to navigate long or complex documents efficiently. Well-structured bookmarks in PDF can improve the appearance of a professional document. This is especially useful for textbooks, manuals, scientific papers, and reports. In presentations, bookmarks allow the user to seamlessly navigate between slides or sections. This feature can be useful for interactive and non-linear presentations. Working with bookmarks in PDF documents improves navigation, accessibility, and overall user experience. Use this feature in educational materials, scientific papers, manuals, or presentations. Bookmarks in PDF improve the quality, efficiency, and browsing experience of your documents. In order to add bookmarks in PDF file, we’ll use Aspose.PDF for Python via .NET API which is a feature-rich, powerful and easy to use document manipulation API for python-net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Console

pip install aspose-pdf

Steps to work with Bookmarks via Python


You need Aspose.PDF for Python via .NET to try the code in your environment.

  1. Open a PDF document using Document object.
  2. Create a bookmark and define its properties.
  3. Add the OutlineItemCollection collection to the Outlines collection.
  4. Save the file again

This example opens a PDF document and creates a bookmark with certain properties via Python. It sets the target page for the bookmark using a ‘GoToAction’ object. After, add it to the document structure using ‘OutlineItemCollection’, and then save the modified document bookmark to a new PDF file. This is the best solution for improving the documents navigation functions.

Add a Bookmark to PDF Document - Python

This sample code shows how to add bookmark to PDF - Python

import aspose.pdf as apdf

from os import path

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

# Open PDF document
document = apdf.Document(path_infile)

# Create a bookmark object
pdf_outline = apdf.OutlineItemCollection(document.outlines)
pdf_outline.title = "Test Outline"
pdf_outline.italic = True
pdf_outline.bold = True

# Set the destination page number
pdf_outline.action = apdf.annotations.GoToAction(document.pages[1])

# Add bookmark to the document's outline collection
outlines = document.outlines
outlines.append(pdf_outline)

# Save PDF document
document.save(path_outfile)