PNG
JPG
BMP
TIFF
PDF
Replace Text Annotations in PDF via Python SDK
API for replacing annotations in PDF documents using Cloud Python SDK.
Get StartedHow to replace text annotations in PDF via Cloud Python SDK
To replace text annotations in 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 replace text annotations in PDF via Python
Aspose.PDF Cloud developers can easily load & replace text annotations in PDF in just a few lines of code.
- Install Python SDK.
- Get Annotation using get_text_annotation().
- Modify Annotation Contents and Icon.
- Submit the Update.
- Validate the Response.
- Downloads the updated document.
Replace Text Annotations in PDF using Python
from asposepdfcloud import PdfApi, ApiClient, TextAnnotationResponse, 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 PdfReplaceAnnotations:
"""Class for managing PDF annotations using Aspose PDF Cloud API."""
def update_annotation(self):
localFolder = "C:\Samples"
storageDocumentName = "sample.pdf"
storageTempFolder = "TempPdfCloud"
outputFileName = "output_replace_annotation.pdf"
annotationID = "GE5TAOZTHA2CYMRZGUWDIMBZFQZTEMA"
# 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: TextAnnotationResponse = self.pdf_api.get_text_annotation(storageDocumentName, annotationID, **args)
if response.code == 200:
annotation = response.annotation
annotation.contents = "Your annotation contents here"
annotation.color = Color(a=0xFF, r=0xFF, g=0, b=0)
annotation.icon = TextIcon.KEY
response2 = self.pdf_api.put_text_annotation(storageDocumentName, annotationID, annotation, **args)
if response2.code == 200:
temp_file = self.pdf_api.download_file(storageTempFolder + '/' + storageDocumentName)
local_path = localFolder + '/' + outputFileName
shutil.move(temp_file, local_path)
logging.info(f"replace_annotation(): annotation successfully chnaged in the document '{outputFileName}'.")
else:
logging.error(f"modify_annotation(): Failed to modify annotation in the document. Response code: {response.code}")