通过 Python 管理 PDF 中的注释

管理 PDF 文档中的注释。使用 Aspose.PDF for Python for .NET 以编程方式修改 PDF 文件

如何使用 Python for .NET 庫管理批注

為了在PDF檔中添加文本註釋,我們將使用[Aspose.PDF for Python](https://products.aspose.com/pdf/python-net)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於 python-net 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

Console

pip install aspose-pdf

通過Python在 PDF 文件中創建批注


你需要 通过.NET for Python 的 Aspose.PDF 才能在你的环境中试用代码。

  1. 在 Document 類別的實例中載入 PDF。
  2. 建立要新增到 PDF 的註解。
  3. 將註解加入到 Page 物件的 Annotations 集合中。
  4. 儲存 PDF 檔案。

PDF 文本註釋 - Python

Example: Python

import aspose.pdf as apdf

from os import path

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)

appearance = apdf.annotations.DefaultAppearance()
appearance.font_size = 12
appearance.font_name = "Arial"

freeTextAnnotation = apdf.annotations.FreeTextAnnotation(
    document.pages[1],
    apdf.Rectangle(299.988, 703.664, 508.708, 720.769, True),
    appearance
)
freeTextAnnotation.contents = "This is a free text annotation."
freeTextAnnotation.name = "FreeText1"
freeTextAnnotation.subject = "Revision 01"
freeTextAnnotation.title = "Free Text Annotation"
freeTextAnnotation.popup = apdf.annotations.PopupAnnotation(
    document.pages[1], apdf.Rectangle(299.988, 713.664, 308.708, 720.769, True)
)
freeTextAnnotation.popup.open = True
document.pages[1].annotations.add(freeTextAnnotation, consider_rotation=False)
document.save(path_outfile)