Add Bookmark to PDF via 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.

Python Package Manager 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

    def bookmark_add(self, infile, outfile):

        path_infile = self.dataDir + infile
        path_outfile = self.dataDir + outfile

        # Open document
        pdfDocument = Document(path_infile)

        # Create a bookmark object
        pdfOutline = OutlineItemCollection(pdfDocument.Outlines)
        pdfOutline.Title = "Test Outline"
        pdfOutline.Italic = True
        pdfOutline.Bold = True
        # Set the destination page number
        pdfOutline.Action = GoToAction(pdfDocument.Pages[1])
        # Add bookmark in the document's outline collection.
        pdfDocument.Outlines.Add(pdfOutline)

        # Save new output
        pdfDocument.Save(path_outfile)