HTML JPG PDF XML DOCX
  Product Family
PDF

Work with Bookmarks in PDF in Python SDK

Manipulate Bookmarks in PDF Document using server-side Python API.

Get Started

How to work with Bookmarks via Cloud Python SDK

In order to work with Bookmarks in PDF via Cloud Python SDK , we’ll use Aspose.PDF Cloud Python SDK This Cloud SDK assists Python programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using Python programming language via Aspose.PDF REST API. Simply create an account at Aspose for Cloud and get your application information. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud Python SDK. If the python package is hosted on Github, you can install directly from Github:

Installation from Github


     
    pip install git+https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python.git

Package Manager Console Command

     
    pip install asposepdfcloud

Steps to create a Bookmark via Python SDK

Aspose.PDF Cloud developers can easily load & create bookmark in PDF in just a few lines of code.

  1. Install Python SDK
  2. Upload a PDF document to the Aspose Cloud server
  3. Download the processed PDF document from the Aspose Cloud server
  4. Append a new bookmark link to a specific page in the PDF document
 

This sample code shows creating a Bookmark in PDF documents


    from asposepdfcloud import PdfApi, ApiClient, Bookmark, Color
    import shutil
    import os
    import json
    from pathlib import Path
    import logging

    # Configure logging
    logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

    class PdfBookmarksAdd:
        """Class for managing PDF annotations using Aspose PDF Cloud API."""

        def append_bookmark(self):
            """Append a new highlight text annotation to the PDF document."""
            localFolder = "C:\Samples"
            storageDocumentName = "sample.pdf"
            storageTempFolder = "TempPdfCloud"
            outputFileName = "output_add_bookmark.pdf"
            parentBookmark = ""                 #The parent bookmark path. Specify an empty string when adding a bookmark to the root.

            # Get your AppSid and AppSecret from https://dashboard.aspose.cloud (free registration required). 
            self.pdf_api = PdfApi(ApiClient(AppSecret, AppSid))

            if self.pdf_api:
                file_path = localFolder + "/" + storageDocumentName
                self.pdf_api.upload_file(os.path.join(storageTempFolder, storageDocumentName), file_path)
                    
                args = {
                    "folder": storageTempFolder
                }

                newBookmark = Bookmark(
                    title = "Your bookmark title here",
                    italic = True,
                    bold = True,
                    color = Color(a=255,r=0,g=255,b=0),
                    level = 1,
                    page_display_left = 89,
                    page_display_top = 564,
                    page_display_zoom = 2,
                    page_number = 3
                )

                try:
                    response = self.pdf_api.post_bookmark(storageDocumentName, parentBookmark, [newBookmark], **args)

                    if response.code == 200:
                        temp_file = self.pdf_api.download_file(storageTempFolder + '/' + storageDocumentName)
                        local_path = localFolder + '/' + outputFileName
                        shutil.move(temp_file, local_path)
                        logging.info(f"append_bookmark(): Bookmark '{response.bookmarks.list[0].action}' successfully added to the document '{outputFileName}'.")
                    else:
                        logging.error(f"append_bookmark(): Failed to add bookmark in the document. Response code: {response.code}")
                except Exception as e:
                    logging.error(f"append_bookmark(): Error while adding bookmark: {e}")
 

With our Python SDK you can

  • Add PDF document’s header & footer in text or image format.
  • Add tables & text or image stamps to PDF documents.
  • Append multiple PDF documents to an existing file.
  • Work with PDF attachments, annotations, & form fields.
  • Apply encryption or decryption to PDF documents & set a password.
  • Delete all stamps & tables from a page or entire PDF document.
  • Delete a specific stamp or table from the PDF document by its ID.
  • Replace single or multiple instances of text on a PDF page or from the entire document.
  • Extensive support for converting PDF documents to various other file formats.
  • Extract various elements of PDF files & make PDF documents optimized.
  • You can try out our free App to insert the bookmarks in PDF files online and test the functionality.

  •