在 Java 中添加 PDF 背景

背景伪像允许您在主页面内容后面放置非内容的视觉元素,而不更改文档的逻辑文本。

向 PDF 添加背景图像

当页面需要将图像显示为背景伪像时,请使用此示例。

  1. 打开源 PDF Document 以及图像输入流。
  2. 创建一个 BackgroundArtifact 并分配图像流。
  3. 将工件添加到目标页面并保存输出 PDF。
public static void addBackgroundImageToPdf(Path inputFile, Path imageFile, Path outputFile) throws Exception {
    try (Document document = new Document(inputFile.toString());
         InputStream imageStream = Files.newInputStream(imageFile)) {
        BackgroundArtifact artifact = new BackgroundArtifact();
        artifact.setBackgroundImage(imageStream);
        document.getPages().get_Item(1).getArtifacts().add(artifact);
        document.save(outputFile.toString());
    }
}

添加带有不透明度的背景图像

此示例将在页面内容后面放置一个半透明的背景图像。

  1. 打开源 PDF Document 以及图像流。
  2. 创建一个 BackgroundArtifact,分配图像,并设置透明度。
  3. 将工件添加到页面并保存文档。
public static void addBackgroundImageWithOpacityToPdf(Path inputFile, Path imageFile, Path outputFile)
        throws Exception {
    try (Document document = new Document(inputFile.toString());
         InputStream imageStream = Files.newInputStream(imageFile)) {
        BackgroundArtifact artifact = new BackgroundArtifact();
        artifact.setBackgroundImage(imageStream);
        artifact.setOpacity(0.5);
        document.getPages().get_Item(1).getArtifacts().add(artifact);
        document.save(outputFile.toString());
    }
}

为 PDF 添加背景颜色

在页面应使用纯色背景而不是图像时,请使用此示例。

  1. 打开源 PDF Document。
  2. 创建一个 BackgroundArtifact 并分配背景颜色。
  3. 将该工件添加到页面并保存输出文件。
public static void addBackgroundColorToPdf(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        BackgroundArtifact artifact = new BackgroundArtifact();
        artifact.setBackgroundColor(Color.getDarkKhaki().toRgb());
        document.getPages().get_Item(1).getArtifacts().add(artifact);
        document.save(outputFile.toString());
    }
}

删除背景伪影

当需要从页面中删除现有的背景伪影时,请使用此方法。

  1. 打开源 PDF Document。
  2. 以相反顺序遍历页面工件集合。
  3. 删除类型为 pagination 且子类型为 background 的工件,然后保存文档。
public static void removeBackground(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (int i = document.getPages().get_Item(1).getArtifacts().size(); i >= 1; i--) {
            Artifact artifact = document.getPages().get_Item(1).getArtifacts().get_Item(i);
            if (artifact.getType() == Artifact.ArtifactType.Pagination
                    && artifact.getSubtype() == Artifact.ArtifactSubtype.Background) {
                document.getPages().get_Item(1).getArtifacts().delete(artifact);
            }
        }

        document.save(outputFile.toString());
    }
}