通过 Java 添加水印

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

使用 Java 库添加水印

將浮浮水印添加到PDF檔中,我們將使用Java的[Aspose.PDF](https://products.aspose.com/pdf/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](https://releases.aspose.com/pdf/java)來嘗試環境中的代碼。

  1. 載入包含文件實例的 PDF。
  2. 建立浮浮浮浮水印藝術實例。
  3. 設定浮浮浮浮水印藝術品屬性。
  4. 使用添加方法添加浮浮水印。
  5. 儲存檔

在 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");