通过 Python 向 PDF 添加文本图章

使用 Aspose.PDF 为 Python for .NET 库以编程方式创建文本图章

如何使用 Python for .NET 庫向 PDF 添加文本圖章

為了在 PDF 檔案中使用文字戳記,請使用 Aspose.PDF for Python via .NET,這是一個功能強大且易於使用的 API。開啟 PyPI,搜尋 aspose-pdf 並安裝。或者,執行以下命令:

Console

pip install aspose-pdf

將文字圖章添加到 PDF 文件 Python


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

  1. 載入包含文件實例的 PDF。
  2. 使用文件對象打開 PDF 文件。
  3. 建立文字戳並定義其屬性。
  4. 使用添加時間戳方法將文字戳添加到頁面

使用 Python 向 PDF 添加文本图章

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)
document.pages.add()
# Create text stamp
textStamp = apdf.TextStamp("Sample Stamp")
# Set whether stamp is background
textStamp.background = True
# Set origin
textStamp.x_indent = 100
textStamp.y_indent = 100
# Rotate stamp
textStamp.rotate = apdf.Rotation.ON90
# Set text properties
textStamp.text_state.font = apdf.text.FontRepository.find_font("Arial")
textStamp.text_state.font_size = 14
textStamp.text_state.font_style = apdf.text.FontStyles(
    apdf.text.FontStyles.BOLD | apdf.text.FontStyles.ITALIC )
textStamp.text_state.foreground_color = apdf.Color.aqua
# Add stamp to particular page
document.pages[1].add_stamp(textStamp)

# Save output document
document.save(path_outfile)