PNG
JPG
BMP
TIFF
PDF
如何通过云 Python SDK 替换 PDF 中的文本注释
要在 PDF 中替换文本注释,我们将使用 Aspose.PDF Cloud Python SDK。此云 SDK 帮助 Python 程序员使用 Python 编程语言通过 Aspose.PDF REST API 开发基于云的 PDF 创建、注释、编辑和转换应用程序。只需在 Aspose for Cloud 创建一个账户并获取您的应用程序信息。一旦您拥有 App SID 和 key,您就可以开始使用 Aspose.PDF Cloud Python SDK。如果 Python 包托管在 Github 上,您可以直接从 Github 安装:
从 Github 安装
pip install git+https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python.git
包管理器控制台命令
pip install asposepdfcloud
通过 Python 替换 PDF 中文本注释的步骤
Aspose.PDF Cloud 开发人员可以轻松地在 PDF 中加载和替换文本注释,只需几行代码。
- 安装 Python SDK。
- 使用 get_text_annotation() 获取注释。
- 修改注释内容和图标。
- 提交更新。
- 验证响应。
- 下载更新后的文档。
使用 Python 替换 PDF 中的文本注释
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}")