通过 Java 添加水印

如何通过 Java 在 PDF 中添加水印

使用 Java 库向 PDF 文件添加水印

将水印添加到 PDF 文件,我们将使用 Aspose.PDF for Java API,这是一款适用于 Java 平台的功能丰富、强大且易于使用的转换 API。你可以直接从 Maven 下载其最新版本,然后将以下配置添加到 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.Artifacts 集合类的方法添加水印。 1。保存 PDF 文件

在 PDF 中添加水印-Java。


    doc = new com.aspose.pdf.Document("1.pdf");

    artifact = new com.aspose.pdf.WatermarkArtifact();
    artifact.setImage("1.jpg");

    artifact.setArtifactHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);
    artifact.setArtifactVerticalAlignment(com.aspose.pdf.VerticalAlignment.Center);
    artifact.setRotation(15);
    artifact.setOpacity(1);
    artifact.setBackground(true);
    doc.getPages().get_Item(1).getArtifacts().add(artifact);

    //save result pdf to file
    doc.save("add_watermark.pdf", com.aspose.pdf.SaveFormat.Pdf);