Java を使って PDF 内のウォーターマークを操作する

Aspose.PDF for {ProductName}} ライブラリを使用して、プログラムで PDF ドキュメント内のウォーターマークを操作する

Java のウォーターマークを使った最も人気のあるアクション

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. Document のインスタンスを持つ PDF を読み込みます。
  2. WatermarkArtifact のインスタンスを作成します。
  3. WatermarkArtifact オブジェクトのプロパティを設定します。
  4. Aspose.Pdf.Page.ArtifactsコレクションクラスのメソッドAddを使用してウォーターマークを追加します。
  5. PDF ファイルを保存する

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

このサンプルコードは、PDF ページにウォーターマークを追加する方法を示しています-Java

Input file:

File not added

Output format:

Output file:

// Open document
Document pdfDocument = new Document("sample.pdf");
FormattedText formattedText = new com.aspose.pdf.facades.FormattedText("Watermark Example");
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);
pdfDocument.getPages().get_Item(1).getArtifacts().add(artifact);
pdfDocument.save(DATA_DIR.resolve("watermark.pdf").toString());
pdfDocument.close();