在 Python 中添加 PDF 背景
Contents
[
Hide
]
向 PDF 添加背景图像
背景图像可用于在文档中添加水印或其他细微的设计。在 Aspose.PDF for Python via .NET 中,每个 PDF 文档都是页面的集合,每个页面包含一组工件。该 BackgroundArtifact 类可用于向页面对象添加背景图像。
当您需要在主 PDF 内容后面放置装饰性图像,而不将其转换为可搜索的文本文档时,此方法非常有用。
以下代码片段展示了如何使用 BackgroundArtifact 对象在 Python 中向 PDF 页面添加背景图像。
- 加载 PDF 文档。
- 创建背景 artifact。
- 加载图像文件。
- 将工件附加到页面。
- 保存更新后的文档。
from os import path
from io import FileIO
import aspose.pdf as ap
import sys
def add_background_image_to_pdf(infile, imagefile, outfile):
"""Add a background image to a PDF document as an artifact."""
with ap.Document(infile) as document:
artifact = ap.BackgroundArtifact()
artifact.background_image = FileIO(imagefile, "rb")
document.pages[1].artifacts.append(artifact)
document.save(outfile)
向 PDF 添加带透明度的背景图像
使用 Aspose.PDF for Python 向 PDF 页面添加半透明背景图像。
通过应用不透明度,背景图像会变得部分透明,使原始页面内容(文字、图像等)仍然清晰可见。这在以下情况下尤其有用:
- 水印
- 品牌覆盖层
- 细微的设计增强
背景作为工件添加,确保它位于所有页面内容的后面。
- 加载 PDF 文档。
- 创建背景 artifact。
- 加载图像文件。
- 设置不透明度级别。
- 将工件附加到页面。
- 保存更新后的文档。
from os import path
from io import FileIO
import aspose.pdf as ap
import sys
def add_background_image_with_opacity_to_pdf(infile, imagefile, outfile):
"""Add a background image with opacity to a PDF document as an artifact."""
with ap.Document(infile) as document:
artifact = ap.BackgroundArtifact()
artifact.background_image = FileIO(imagefile, "rb")
artifact.opacity = 0.5
document.pages[1].artifacts.append(artifact)
document.save(outfile)
为 PDF 添加背景颜色
使用 Aspose.PDF for Python 为 PDF 页面应用纯色背景。
- 加载 PDF 文档。
- 创建背景 artifact。
- 设置背景颜色。
- 将工件附加到页面。
- 保存更新后的文档。
from os import path
from io import FileIO
import aspose.pdf as ap
import sys
def add_background_color_to_pdf(infile, outfile):
"""Add a solid color background to a PDF document as an artifact."""
with ap.Document(infile) as document:
artifact = ap.BackgroundArtifact()
artifact.background_color = ap.Color.dark_khaki
document.pages[1].artifacts.append(artifact)
document.save(outfile)
从 PDF 中移除背景
使用 Aspose.PDF for Python 从 PDF 页面中移除背景伪影。 PDF 中的背景通常以伪影形式存储,此方法会选择性地识别并仅移除被分类为背景元素的伪影。
- 加载 PDF 文档。
- 访问页面伪影。
- 过滤背景伪影。
- 收集背景元素。
- 删除背景伪影。
- 保存更新后的文档。
from os import path
from io import FileIO
import aspose.pdf as ap
import sys
def remove_background(infile, outfile):
with ap.Document(infile) as document:
backgrounds = [
artifact
for artifact in document.pages[1].artifacts
if artifact.type == ap.Artifact.ArtifactType.PAGINATION
and artifact.subtype == ap.Artifact.ArtifactSubtype.BACKGROUND
]
for background in backgrounds:
document.pages[1].artifacts.delete(background)
document.save(outfile)