Java 経由でウォーターマークを追加

Java ライブラリの Aspose.PDF を使用して、プログラムで PDF ドキュメントにウォーターマークを追加します

Java ツールを使用して PDF ファイルにウォーターマークを追加

PDF ファイルにウォーターマークを追加するには、Java プラットフォーム向けの機能豊富で強力で使いやすい変換 API である Aspose.PDF for 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. Document のインスタンスを使用して PDF をロードします。
  2. ウォーターマークアーティファクトのインスタンスを作成します。
  3. WatermarkArtifact オブジェクトのプロパティを設定します。
  4. Aspose.Pdf.Page.Artifacts コレクションクラスの Add メソッドを使用してウォーターマークを追加します。
  5. PDF ファイルを保存

PDF にウォーターマークを追加-Java

Document pdfDocument = new Document(DATA_DIR.resolve("sample.pdf").toString());
com.aspose.pdf.WatermarkArtifact artifact = new com.aspose.pdf.WatermarkArtifact();
artifact.setImage(DATA_DIR.resolve("watermark.jpg").toString());
artifact.setArtifactHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);
artifact.setArtifactVerticalAlignment(com.aspose.pdf.VerticalAlignment.Center);
artifact.setRotation(15);
artifact.setOpacity(1);
artifact.setBackground(true);
pdfDocument.getPages().get_Item(1).getArtifacts().add(artifact);

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