HTML JPG PDF XML DOCX
  Product Family
PDF

Work with Annotations in PDF via Python SDK

API for manage annotations in PDF documents with Python.

Get Started

How to work with annotations in PDF documents using Cloud Python SDK

In order to work with annotations in PDF documents 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 add annotations using Python SDK

Aspose.PDF Cloud developers can easily load & append annotations to PDF documents in just a few lines of code.

  1. Uploads the PDF.
  2. Creates a styled text box (with fonts, colors, alignment).
  3. Submits the annotation to the specified page.
  4. Downloads the modified document for local use.
 

This sample code shows adding annotations to PDF document


    from asposepdfcloud import PdfApi, ApiClient, TextAnnotation, Rectangle, TextIcon, Color, AnnotationFlags, HorizontalAlignment
    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 PdfAddTextAnnotations:
        """Class for adding PDF text annotations using Aspose PDF Cloud API."""
        def append_text_annotation(self):
            """Append a new text annotation to the PDF document."""
            localFolder = "C:\Samples"
            storageDocumentName = "sample.pdf"
            storageTempFolder = "TempPdfCloud"
            outputFileName = "output_add_text_annotation.pdf"

            # 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
                }

                new_annotation = TextAnnotation(
                    rect = Rectangle(llx=100, lly=350, urx=450, ury=400),
                    color = Color(a=0xFF, r=0xFF, g=0, b=0),
                    name = 'Free Text Annotation',
                    flags = [AnnotationFlags.DEFAULT],
                    horizontal_alignment = HorizontalAlignment.CENTER,
                    rich_text = "Your annotation text here",
                    subject = "Your annotation subject here",
                    contents = "Your annotation contents here",
                    title = "Your annotation title here",
                    z_index = 1,
                    icon = TextIcon.STAR
                )
                
                try:
                    response = self.pdf_api.post_page_text_annotations(storageDocumentName, page_number=1, annotations=[new_annotation], **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_text_annotation(): annotation added to the document '{outputFileName}'.")
                    else:
                        logging.error(f"append_text_annotation(): Failed to add annotation to the document. Response code: {response.code}")
                except Exception as e:
                    logging.error(f"append_text_annotation(): Error while adding annotation: {e}")