JavaでPDFの背景を追加する

バックグラウンドアーティファクトを使用すると、論理的なドキュメントテキストを変更せずに、メインページコンテンツの背後にコンテンツではない視覚要素を配置できます。

PDF にバックグラウンド画像を追加する

ページに画像をバックグラウンドアーティファクトとして表示させる場合は、この例を使用してください。

  1. Document で元の PDF を開き、画像ストリームを開いてください。
  2. BackgroundArtifact オブジェクトを作成し、画像ストリームを設定してください。
  3. アーティファクトを対象ページに追加し、出力 PDFを保存してください。
public static void addBackgroundImageToPdf(Path inputFile, Path imageFile, Path outputFile) throws Exception {
    try (Document document = new Document(inputFile.toString());
         InputStream imageStream = Files.newInputStream(imageFile)) {
        BackgroundArtifact artifact = new BackgroundArtifact();
        artifact.setBackgroundImage(imageStream);
        document.getPages().get_Item(1).getArtifacts().add(artifact);
        document.save(outputFile.toString());
    }
}

不透明度を設定した背景画像を追加する

この例は、ページコンテンツの背後に半透明の背景画像を配置します。

  1. Document で元の PDF を開き、画像ストリームを開いてください。
  2. BackgroundArtifact オブジェクトを作成し、画像と不透明度を設定してください。
  3. アーティファクトをページに追加し、ドキュメントを保存してください。
public static void addBackgroundImageWithOpacityToPdf(Path inputFile, Path imageFile, Path outputFile)
        throws Exception {
    try (Document document = new Document(inputFile.toString());
         InputStream imageStream = Files.newInputStream(imageFile)) {
        BackgroundArtifact artifact = new BackgroundArtifact();
        artifact.setBackgroundImage(imageStream);
        artifact.setOpacity(0.5);
        document.getPages().get_Item(1).getArtifacts().add(artifact);
        document.save(outputFile.toString());
    }
}

PDF に背景色を追加する

ページが画像の代わりに単色の背景色を使用すべき場合に、この例を使用してください。

  1. Document で元の PDF を開いてください。
  2. BackgroundArtifact オブジェクトを作成し、背景色を設定してください。
  3. アーティファクトをページに追加し、出力ファイルを保存してください。
public static void addBackgroundColorToPdf(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        BackgroundArtifact artifact = new BackgroundArtifact();
        artifact.setBackgroundColor(Color.getDarkKhaki().toRgb());
        document.getPages().get_Item(1).getArtifacts().add(artifact);
        document.save(outputFile.toString());
    }
}

関連トピック