使用 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 並與之交互。如有任何歧義,請隨時在論壇上與我們聯繫。

也可以看看