PNG JPG BMP TIFF PDF
Aspose.PDF  for Python

通过 Python SDK 从 PDF 获取页面注释

使用云 Python SDK 从 PDF 文档获取注释的 API。

Get Started

如何通过云 Python SDK 从 PDF 获取页面注释

要从 PDF 获取页面注释,我们将使用 Aspose.PDF Cloud Python SDK。此云 SDK 协助 Python 程序员使用 Aspose.PDF REST API 开发基于云的 PDF 创建、注释、编辑和转换应用程序。只需在 Aspose for Cloud 上创建一个帐户并获取您的应用程序信息。一旦拥有 App SID 和密钥,您就可以开始使用 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 的页面注释。

  1. 安装 Python SDK
  2. 将 PDF 上传到云存储。
  3. 从指定页面检索所有注释。
  4. 记录每个的详细信息。
  5. 返回第一个文本注释的 ID(用于进一步操作,如编辑或删除)。
 

使用 Python 从 PDF 获取页面注释


    from annotations_helper import Config, PdfAnnotationsHelper, logging
    from asposepdfcloud import PdfApi, AnnotationsInfoResponse

    class PdfGetAnnotations:
        """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 request_annotations(self):
            """Get annotations from the page in the PDF document."""
            if self.pdfApi:
                self.helper.uploadFile(Config.PDF_DOCUMENT_NAME, Config.LOCAL_FOLDER, Config.REMOTE_FOLDER)

                args = {
                    "folder": Config.REMOTE_FOLDER
                }
                annotation_result = ''
                response: AnnotationsInfoResponse = self.pdfApi.get_page_annotations(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER, **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} with '{annotation.contents}' content get from the document '{Config.PDF_DOCUMENT_NAME}' on {annotation.page_index} page.")
                            annotation_result = annotation.id
                    return annotation_result
                else:
                    logging.error(f"get_annotations(): Failed to get annotation in the document. Response code: {response.code}")
                    return annotation_result