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 annotations_helper import Config, PdfAnnotationsHelper, logging
from asposepdfcloud import PdfApi, TextAnnotationResponse
class PdfReplaceAnnotations:
"""Class for managing PDF annotations using Aspose PDF Cloud API."""
def __init__(self, pdf_api: PdfApi, helper: PdfAnnotationsHelper):
self.pdfApi = pdf_api
self.helper = helper
def _get_annotation(self, annotation_id):
"""Get annotation from the page in the PDF document."""
if self.pdfApi:
args = {
"folder": Config.REMOTE_FOLDER
}
response: TextAnnotationResponse = self.pdfApi.get_text_annotation(Config.PDF_DOCUMENT_NAME, annotation_id, **args)
if response.code == 200:
logging.info(f"get_annotationn(): annotation '{annotation_id}' successfully found '{response.annotation.contents}' in the document '{Config.PDF_DOCUMENT_NAME}'.")
return response.annotation
else:
logging.error(f"get_annotation(): Failed to get annotation in the document. Response code: {response.code}")
return None
def modify_annotation(self):
if self.pdfApi:
if Config.ANNOTATION_ID:
self.helper.uploadFile(Config.PDF_DOCUMENT_NAME, Config.LOCAL_FOLDER, Config.REMOTE_FOLDER)
args = {
"folder": Config.REMOTE_FOLDER
}
annotation = self._get_annotation(Config.ANNOTATION_ID)
annotation.contents = Config.REPLACED_CONTENT
annotation.icon = "Star"
response = self.pdfApi.put_text_annotation(Config.PDF_DOCUMENT_NAME, Config.ANNOTATION_ID, annotation, **args)
if response.code == 200:
logging.info(f"modify_annotation(): annotation '{annotation.id}' successfully modified in the document '{Config.PDF_DOCUMENT_NAME}'.")
self.helper.downloadFile(Config.PDF_DOCUMENT_NAME, Config.LOCAL_RESULT_DOCUMENT_NAME, Config.LOCAL_FOLDER, Config.REMOTE_FOLDER, "replaced_annotatiom_")
else:
logging.error(f"modify_annotation(): Failed to modify annotation in the document. Response code: {response.code}")