PNG
JPG
BMP
TIFF
PDF
Get Page Annotations from PDF via Python SDK
API for getting annotations from PDF documents using Cloud Python SDK.
Get StartedHow to get page annotations from PDF via Cloud Python SDK
To get page annotations from PDF, 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 get page annotations from PDF via Python
Aspose.PDF Cloud developers can easily load & get page annotations from PDF in just a few lines of code.
- Install Python SDK.
- Uploads the PDF to cloud storage.
- Retrieves all annotations from a specified page.
- Logs details of each.
- Returns the ID of the first Text annotation (for further actions like editing or deletion).
Get Page Annotations from PDF using Python
from asposepdfcloud import PdfApi, ApiClient, AnnotationsInfoResponse
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 PdfGetAnnotations:
"""Class for managing PDF annotations using Aspose PDF Cloud API."""
def __init__(self):
"""Initialize the API client."""
credentials_file = Path(r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json")
with credentials_file.open("r", encoding="utf-8") as file:
credentials = json.load(file)
api_key, app_id = credentials.get("key"), credentials.get("id")
self.pdf_api = PdfApi(ApiClient(api_key, app_id))
def request_annotations(self):
"""Get annotations from the page in the PDF document."""
localFolder = "C:\Samples"
storageDocumentName = "sample.pdf"
storageTempFolder = "TempPdfCloud"
# 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
}
response: AnnotationsInfoResponse = self.pdf_api.get_page_annotations(storageDocumentName, page_number = 1, **args)
if response.code == 200:
for annotation in response.annotations.list:
if annotation.annotation_type == "Text":
logging.info(f"get_annotations(): annotation id={annotation.id} type={annotation.annotation_type} with '{annotation.contents}' content get from the document '{storageDocumentName}' on 1 page.")
else:
logging.error(f"get_annotations(): Failed to get annotation in the document. Response code: {response.code}")