使用 Python 的水印注释

本文展示了如何使用 Aspose.PDF for Python via .NET 在 PDF 文档中处理水印批注。

示例脚本演示了三种常见的工作流程:

  • 添加水印注释
  • 获取水印注释矩形
  • 删除水印注释

添加水印注释

此示例在 PDF 文档的首页添加水印注释。水印使用文本状态来控制字体设置,并应用自定义不透明度以实现半透明外观。

打开 PDF 并获取目标页面

document = ap.Document(infile)
page = document.pages[1]

创建水印注释

定义注释矩形并将其追加到页面注释集合中。

watermark_annotation = ap.annotations.WatermarkAnnotation(
    page,
    ap.Rectangle(100, 100, 400, 200, True),
)

page.annotations.append(watermark_annotation)

配置文本外观

创建一个 TextState 用于控制文本颜色、字体大小和字体族的对象。

text_state = ap.text.TextState()
text_state.foreground_color = ap.Color.blue
text_state.font_size = 25
text_state.font = ap.text.FontRepository.find_font("Arial")

设置不透明度和水印文本

示例使用 50% 不透明度,并将三行文本写入水印批注。

watermark_annotation.opacity = 0.5
watermark_annotation.set_text_and_state(["HELLO", "Line 1", "Line 2"], text_state)

保存 PDF

document.save(outfile)

完整示例

def watermark_add(infile, outfile):
    document = ap.Document(infile)
    page = document.pages[1]

    watermark_annotation = ap.annotations.WatermarkAnnotation(
        page,
        ap.Rectangle(100, 100, 400, 200, True),
    )

    page.annotations.append(watermark_annotation)

    text_state = ap.text.TextState()
    text_state.foreground_color = ap.Color.blue
    text_state.font_size = 25
    text_state.font = ap.text.FontRepository.find_font("Arial")

    watermark_annotation.opacity = 0.5
    watermark_annotation.set_text_and_state(["HELLO", "Line 1", "Line 2"], text_state)

    document.save(outfile)

获取水印注释

要检查现有的水印注释,请通过以下方式过滤第一页的注释 WATERMARK 键入并打印它们的矩形。

加载文档并收集水印注释

document = ap.Document(infile)
watermark_annotations = [
    a
    for a in document.pages[1].annotations
    if a.annotation_type == ap.annotations.AnnotationType.WATERMARK
]

打印注释矩形

for watermark_annotation in watermark_annotations:
    print(watermark_annotation.rect)

完整示例

def watermark_get(infile, outfile):
    document = ap.Document(infile)
    watermark_annotations = [
        a
        for a in document.pages[1].annotations
        if a.annotation_type == ap.annotations.AnnotationType.WATERMARK
    ]

    for watermark_annotation in watermark_annotations:
        print(watermark_annotation.rect)

删除水印批注

此工作流会从第一页删除所有水印注释并保存更新后的 PDF。

查找要删除的水印批注

document = ap.Document(infile)
watermark_annotations = [
    a
    for a in document.pages[1].annotations
    if a.annotation_type == ap.annotations.AnnotationType.WATERMARK
]

删除注释并保存 PDF

for watermark_annotation in watermark_annotations:
    document.pages[1].annotations.delete(watermark_annotation)

document.save(outfile)

完整示例

def watermark_delete(infile, outfile):
    document = ap.Document(infile)
    watermark_annotations = [
        a
        for a in document.pages[1].annotations
        if a.annotation_type == ap.annotations.AnnotationType.WATERMARK
    ]

    for watermark_annotation in watermark_annotations:
        document.pages[1].annotations.delete(watermark_annotation)

    document.save(outfile)

相关主题