通过 Python 添加水印

使用适用于 Python for .NET 库的 Aspose.PDF 以编程方式向 PDF 文档添加水印

使用 Python for .NET 工具向 PDF 文件添加水印

為了在 PDF 檔案中添加浮水印,我們將使用 Aspose.PDF for Python,這是一個強大且易於使用的 API。打開 PyPI,搜尋 aspose-pdf 並安裝它。或者,執行以下命令:

Console

pip install aspose-pdf

通过 Python 添加水印


要在你的环境中试用这些代码,你需要 Aspose.PDF for Python

  1. 在 PDF 中加载文档实例。
  2. 创建 WatermarkArtifact 的实例。
  3. 设置 WatermarkArtifact 对象的属性。
  4. 使用 Add of Aspose.Pdf.Page.Artifacts 集合类的方法添加水印。
  5. 保存 PDF 文件

向 PDF 添加水印-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)

doc = apdf.Document(path_infile)

artifact = apdf.WatermarkArtifact()
artifact.set_image("watermark.jpg")

artifact.artifact_horizontal_alignment = apdf.HorizontalAlignment.CENTER
artifact.artifact_vertical_alignment = apdf.VerticalAlignment.CENTER
artifact.rotation = 15
artifact.opacity = 1
artifact.is_background = True
doc.pages[1].artifacts.append(artifact)

# save result pdf to file
doc.save(path_outfile)