通过 Java 添加水印

如何使用 Java 在 PDF 中添加水印

使用 Java 库添加水印

在 PDF 文件中添加水印,我们将使用 Aspose.PDF for Java API,这是一款适用于 Java 平台的功能丰富、功能强大且易于使用的转换 API。你可以直接从 [Maven](https://repository.aspose.com/webapp/ #/artifacts/browse/Tree/General/repo/com/aspose/aspose-PDF)下载其最新版本,然后通过在 pom.xml 中添加以下配置将其安装在基于 Maven 的项目中。

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

使用 Java 添加水印


你需要 Aspose.PDF for Java 库 才能在你的环境中试用代码。

1.加载带有文档实例的 PDF。 1.创建 WatermarkArtifact 的实例。 1.设置 WatermarkArtifact 对象的属性。 1.使用方法 Add of Aspose.Pdf.Page.Artics 集合类添加水印。 1.保存 PDF 文件

在 PDF 中添加水印-Java。

<% watermark.code-block.subtitle %>

Input file:

File not added

Output format:

Output file:

    // Open document
    Document doc = new Document(_dataDir + "text.pdf");
    FormattedText formattedText = new FormattedText("Watermark", java.awt.Color.BLUE,FontStyle.Courier, EncodingType.Identity_h, true, 72.0F);
    WatermarkArtifact artifact = new WatermarkArtifact();
    artifact.setText(formattedText);
    artifact.setArtifactHorizontalAlignment (HorizontalAlignment.Center);
    artifact.setArtifactVerticalAlignment (VerticalAlignment.Center);
    artifact.setRotation (45);
    artifact.setOpacity (0.5);
    artifact.setBackground (true);
    doc.getPages().get_Item(1).getArtifacts().add(artifact);
    doc.save(_dataDir + "watermark.pdf");