使用 Python 注释 PDF 文档

您可以使用 Python 在云上以编程方式注释任何 PDF 文档。它可以是关于现有数据的任何附加信息,形式为图像、评论、注释或文档中其他类型的外部注释。在本文中,您将学习如何使用 Python 中的 REST API 对 PDF 文档进行注释。

本文应涵盖以下主题:

文档注释 REST API 和 Python SDK

为了注释 PDF 文档,我将使用 GroupDocs.Annotation Cloud 的 Python SDK API。它允许以编程方式在线构建文档注释工具。您可以向所有流行格式的业务文档添加注释、水印叠加、文本替换、密文和文本标记。它还为云 API 提供 .NET、Java、PHP、Ruby 和 Node.js SDK 作为其 文档注释系列成员

您可以在控制台中使用以下命令将 GroupDocs.Annotation Cloud 安装到您的 Python 项目:

pip install groupdocs_annotation_cloud

在开始执行步骤和可用代码示例之前,请从 仪表板 获取您的客户端 ID 和客户端密码。获得 ID 和密码后,添加如下所示的代码:

client_id = "659fe7da-715b-4744-a0f7-cf469a392b73"
client_secret = "b377c36cfa28fa69960ebac6b6e36421"

configuration = groupdocs_annotation_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = ""

在 Python 中使用 REST API 注释 PDF 文档

您可以按照以下简单步骤向 PDF 文档添加注释:

  1. 上传PDF文件至云端
  2. 使用 Python 注释 PDF 文档
  3. 下载注释文件

上传文件

首先,使用以下代码示例将 PDF 文件上传到云端:

# 创建 api 实例
file_api = groupdocs_annotation_cloud.FileApi.from_config(configuration)
my_storage = ""

# 上传示例文件
request = groupdocs_annotation_cloud.UploadFileRequest("sample.pdf", "C:\\Files\\sample.pdf", my_storage)
response = file_api.upload_file(request)

因此,上传的 PDF 文件将在云端仪表板的 文件部分 中可用。

使用 Python 注释 PDF 文档

请按照下面提到的步骤以编程方式向 PDF 文档添加多个注释。

  • 创建 AnnotateApi 实例
  • 创建 AnnotationInfo 的第一个实例
  • 为第一个实例设置注释属性,例如位置、类型、文本等。
  • 创建 AnnotationInfo 第二个实例
  • 为第二个实例设置注释属性,例如位置、类型、文本等。
  • 创建AnnotationInfo第三个实例
  • 为第三个实例设置注释属性,例如位置、类型、文本等。
  • 创建一个 FileInfo 实例并设置输入文件路径
  • 创建 AnnotateOptions 的实例并将文件信息设置为 AnnotateOptions
  • 将第一、第二和第三个注释分配给 AnnotateOptions
  • 通过调用 AnnotateRequest 方法创建请求
  • 调用AnnotateApi.annotate()方法获取结果

以下代码示例展示了如何使用 REST API 对 PDF 文档进行注释和添加多个注释。

# 初始化接口
api = groupdocs_annotation_cloud.AnnotateApi.from_config(configuration)

# 距离标注
a1 = groupdocs_annotation_cloud.AnnotationInfo()
a1.annotation_position = groupdocs_annotation_cloud.Point()
a1.annotation_position.x = 1
a1.annotation_position.y = 1
a1.box = groupdocs_annotation_cloud.Rectangle()
a1.box.x = 100
a1.box.y = 100
a1.box.width = 200
a1.box.height = 200
a1.page_number = 0
a1.pen_color = 1201033
a1.pen_style = "Solid"
a1.pen_width = 3
a1.opacity = 1
a1.type = "Distance"
a1.text = "This is 距离标注"
a1.creator_name = "Anonym A."

# 区域标注
a2 = groupdocs_annotation_cloud.AnnotationInfo()
a2.annotation_position = groupdocs_annotation_cloud.Point()
a2.annotation_position.x = 1
a2.annotation_position.y = 1
a2.box = groupdocs_annotation_cloud.Rectangle()
a2.box.x = 100
a2.box.y = 400
a2.box.width = 200
a2.box.height = 100
a2.page_number = 0
a2.pen_color = 1201033
a2.pen_style = "Solid"
a2.pen_width = 3
a2.opacity = 1
a2.type = "Area"
a2.text = "This is 区域标注"
a2.creator_name = "Anonym A."

# 箭头注释
a3 = groupdocs_annotation_cloud.AnnotationInfo()
a3.annotation_position = groupdocs_annotation_cloud.Point()
a3.annotation_position.x = 1
a3.annotation_position.y = 1
a3.box = groupdocs_annotation_cloud.Rectangle()
a3.box.x = 100
a3.box.y = 250
a3.box.width = 100
a3.box.height = 50
a3.page_number = 0
a3.pen_color = 1201033
a3.pen_style = "Solid"
a3.pen_width = 1
a3.opacity = 0.7
a3.type = "Arrow"
a3.text = "This is 箭头注释"
a3.creator_name = "Anonym A."

# 输入文件
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "sample.pdf"

# 定义注释选项
options = groupdocs_annotation_cloud.AnnotateOptions()
options.file_info = file_info
options.annotations = [a1, a2, a3]
options.output_path = "output.pdf"

# 创建注释请求
request = groupdocs_annotation_cloud.AnnotateRequest(options)
result = api.annotate(request)         

print("AddMultipleAnnotations: Multiple Annotations added: " + result['href'])
在 Python 中使用 REST API 向 PDF 文档添加多个注释

在 Python 中使用 REST API 向 PDF 文档添加多个注释

您可以在文档的 adding annotations 部分下阅读有关支持的注释类型的更多信息。

下载注释文件

上面的代码示例会将带注释的 PDF 文件保存在云端。您可以使用以下代码示例下载它:

# 接口初始化
file_api = groupdocs_annotation_cloud.FileApi.from_config(configuration)
my_storage = ""

# 创建下载请求
request = groupdocs_annotation_cloud.DownloadFileRequest("output.pdf", my_storage)
response = file_api.download_file(request)

# 将下载的文件移动到您的工作目录
shutil.move(response, "C:\\Files\\")

使用 Python 添加 TextField 注释

您可以按照以下步骤以编程方式在 PDF 文档中添加文本字段注释:

  • 创建 AnnotateApi 实例
  • 创建 AnnotationInfo 的实例
  • 定义注释位置
  • 定义矩形位置、高度和宽度
  • 设置各种注释属性,例如文本、高度、宽度等。
  • 将注释类型设置为 TextField
  • 创建一个 FileInfo 实例并设置输入文件路径
  • 创建 AnnotateOptions 的实例并将文件信息设置为 AnnotateOptions
  • 将注释分配给 AnnotateOptions
  • 通过调用 AnnotateRequest 方法创建请求
  • 调用AnnotateApi.annotate()方法获取结果

以下代码示例显示了如何使用 REST API 在 PDF 文档中添加文本字段注释。请按照前面提到的步骤上传和下载文件。

# 初始化接口
api = groupdocs_annotation_cloud.AnnotateApi.from_config(configuration)

# 提供注释信息
a1 = groupdocs_annotation_cloud.AnnotationInfo()
a1.annotation_position = groupdocs_annotation_cloud.Point()
a1.annotation_position.x = 1
a1.annotation_position.y = 1
a1.box = groupdocs_annotation_cloud.Rectangle()
a1.box.x = 380
a1.box.y = 300
a1.box.width = 100
a1.box.height = 50
a1.page_number = 0
a1.font_color = 1201033
a1.font_size = 12
a1.opacity = 0.7
a1.type = "TextField"           
a1.text = "Text field text"
a1.creator_name = "Anonym A."

# 输入文件路径
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "sample.pdf"

# 定义注释选项
options = groupdocs_annotation_cloud.AnnotateOptions()
options.file_info = file_info
options.annotations = [a1]
options.output_path = "output.pdf"

# 创建注释请求
request = groupdocs_annotation_cloud.AnnotateRequest(options)
result = api.annotate(request)         

print("AddTextFieldAnnotation: Text Field Annotation added: " + result['href'])
使用 Python 添加文本字段注释

使用 Python 添加文本字段注释

使用 Python 添加图像注释

您可以按照以下步骤以编程方式在 PDF 文档中添加图像注释:

  • 创建 AnnotateApi 实例
  • 创建 AnnotationInfo 的实例
  • 定义矩形并设置其位置、高度和宽度
  • 设置各种注释属性,例如位置、文本、高度、宽度等。
  • 将注释类型设置为图像
  • 创建一个 FileInfo 实例并设置输入文件路径
  • 创建 AnnotateOptions 的实例并将文件信息设置为 AnnotateOptions
  • 将注释分配给 AnnotateOptions
  • 通过调用 AnnotateRequest 方法创建请求
  • 调用AnnotateApi.annotate()方法获取结果

以下代码示例展示了如何使用 REST API 在 PDF 文档中添加图像注释。请按照前面提到的步骤上传和下载文件。

# 初始化接口
api = groupdocs_annotation_cloud.AnnotateApi.from_config(configuration)

# 定义注解
a1 = groupdocs_annotation_cloud.AnnotationInfo()
a1.box = groupdocs_annotation_cloud.Rectangle()
a1.box.x = 300
a1.box.y = 300
a1.box.width = 200
a1.box.height = 100
a1.page_number = 0
a1.pen_color = 1201033
a1.pen_style = "Solid"
a1.pen_width = 1
a1.opacity = 0.7
a1.type = "Image"
a1.text = "This is Image annotation"
a1.creator_name = "Anonym A."
a1.image_path = "JohnSmith.png"

# 输入文件路径
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "sample.pdf"

# 定义注解 options
options = groupdocs_annotation_cloud.AnnotateOptions()
options.file_info = file_info
options.annotations = [a1]
options.output_path = "output_img.pdf"

# 创建注释请求
request = groupdocs_annotation_cloud.AnnotateRequest(options)
result = api.annotate(request)         

print("AddImageAnnotation: Image Annotation added: " + result['href'])
使用 Python 添加图像注释

使用 Python 添加图像注释

您可以按照以下步骤以编程方式在 PDF 文档中添加超链接注释:

  • 创建 AnnotateApi 实例
  • 创建 AnnotationInfo 的实例
  • 定义注释点并为每个点设置位置
  • 设置各种注释属性,例如文本、高度、宽度等。
  • 将注释类型设置为链接
  • 创建一个 FileInfo 实例并设置输入文件路径
  • 创建 AnnotateOptions 的实例并将文件信息设置为 AnnotateOptions
  • 将注释分配给 AnnotateOptions
  • 通过调用 AnnotateRequest 方法创建请求
  • 调用AnnotateApi.annotate()方法获取结果

以下代码示例显示了如何使用 REST API 在 PDF 文档中添加超链接注释。请按照前面提到的步骤上传和下载文件。

# 初始化接口
api = groupdocs_annotation_cloud.AnnotateApi.from_config(configuration)

# 提供注释信息
a1 = groupdocs_annotation_cloud.AnnotationInfo()
p1 = groupdocs_annotation_cloud.Point()
p1.x = 80
p1.y = 710
p2 = groupdocs_annotation_cloud.Point()
p2.x = 240
p2.y = 710
p3 = groupdocs_annotation_cloud.Point()
p3.x = 80
p3.y = 650
p4 = groupdocs_annotation_cloud.Point()
p4.x = 240
p4.y = 650
a1.points = [p1, p2, p3, p4]
a1.page_number = 0
a1.type = "Link"
a1.text = "This is Link annotation"
a1.creator_name = "Anonym A."
a1.url = "https://www.groupdocs.com/"

# 输入文件路径
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "sample.pdf"

# 定义注释选项
options = groupdocs_annotation_cloud.AnnotateOptions()
options.file_info = file_info
options.annotations = [a1]
options.output_path = "output.pdf"

# 创建注释请求
request = groupdocs_annotation_cloud.AnnotateRequest(options)
result = api.annotate(request)         

print("AddLinkAnnotation: Link Annotation added: " + result['href'])
使用 Python 使用链接注释进行注释

使用 Python 使用链接注释进行注释

在线试用

请试用以下使用上述API开发的免费在线PDF注释工具。 https://products.groupdocs.app/annotation/pdf

结论

在本文中,您了解了如何为云端 PDF 文档添加各种类型的注释。此外,您还学习了如何以编程方式将 PDF 文件上传到云端,然后从云端下载带注释的文件。您可以使用 文档 了解有关 GroupDocs.Annotation Cloud API 的更多信息。我们还提供了一个 API 参考 部分,让您可以直接通过浏览器可视化我们的 API 并与之交互。如有任何歧义,请随时在论坛上与我们联系。

也可以看看