0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaでPDF文書の背景色と背景画像を設定する方法

Last updated at Posted at 2023-06-16

PDF文書の作成において、ページ背景はあまり注目されないページ要素です。しかし、ページ背景は、文書の視覚効果、読書体験、専門性において大きな役割を果たすことができます。適切なページ背景を使用することで、文書の見栄えを良くし、視覚的な疲労を和らげ、文書をより専門的に見せることができます。この記事では、無料のFree Spire.PDF for Javaを使用して、PDF文書のページの背景色または背景画像を設定する方法を紹介します。

依存関係の追加

Spire.PDF for Javaは、公式サイトから手動でダウンロードしてプロジェクトに導入するか、Mavenで以下のコードを入力してインストールすることが可能です。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>9.5.6</version>
    </dependency>
</dependencies>

PDF文書の背景色の設定

Free Spire.PDF for Javaでは、PDFページの背景として色を設定する PdfPageBase.setBackgroundColor() と、背景の不透明度を設定する PdfPageBase.setBackgroundOpacity() メソッドを提供しています。詳しい手順は以下の通りです。

  • PdfDocumentのオブジェクトを作成します。
    PdfDocument pdf = new PdfDocument();

  • PDF 文書を読み込みます。
    pdf.loadFromFile("サンプル.pdf");

  • PDF文書内のページをループして、各ページに背景色を追加します。
    page.setBackgroundColor(Color.ORANGE);

  • 背景の不透明度を設定することもできます。
    page.setBackgroudOpacity(0.2f);

  • 文書を保存します。
    pdf.saveToFile("背景色.pdf");

フルコード

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

import java.awt.*;

public class SetPDFBackgroundColor {
    public static void main(String[] args) {

        //PdfDocumentのオブジェクトを作成する
        PdfDocument pdf = new PdfDocument();

        //PDFファイルを読み込む
        pdf.loadFromFile("サンプル.pdf");

        //PDFドキュメント内のページをループする
        for (PdfPageBase page : (Iterable<? extends PdfPageBase>) pdf.getPages()
             ) {
            //各ページの背景色を設定する
            page.setBackgroundColor(Color.ORANGE);
            //背景の不透明度を設定する
            page.setBackgroudOpacity(0.2f);
        }

        //PDFファイルを保存する
        pdf.saveToFile("背景色.pdf");
    }
}

【結果ファイル】
PDF文書の背景色の設定

PDF文書の背景画像の設定

Free Spire.PDF for Javaは、PDFページの背景として画像を設定する PdfPageBase.setBackgroundImage() を提供します。PDF文書に画像の背景を追加する詳細な手順は以下の通りです。

  • PdfDocumentのオブジェクトを作成します。
    PdfDocument pdf = new PdfDocument();

  • PDF 文書を読み込みます。
    pdf.loadFromFile("サンプル.pdf");

  • 画像を読み込みます。
    BufferedImage background = ImageIO.read(new File("背景.jpg"));

  • PDF文書内のページをループして、各ページに背景画像を追加します。
    page.setBackgroundImage(background);

  • 背景の不透明度を設定することもできます。
    page.setBackgroudOpacity(0.2f);

  • 文書を保存します。
    pdf.saveToFile("背景色.pdf");

フルコード

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SetPDFBackgroundImage {
    public static void main(String[] args) throws IOException {

        //PdfDocumentのオブジェクトを作成する
        PdfDocument pdf = new PdfDocument();

        //PDFファイルを読み込む
        pdf.loadFromFile("サンプル.pdf");

        //画像を読み込む
        BufferedImage background = ImageIO.read(new File("背景.jpg"));

        //PDFドキュメント内のページをループする
        for (PdfPageBase page : (Iterable<? extends PdfPageBase>) pdf.getPages()
             ) {
            //各ページの背景画像を設定する
            page.setBackgroundImage(background);
            //背景の不透明度を設定する
            page.setBackgroudOpacity(0.2f);
        }

        //PDFファイルを保存する
        pdf.saveToFile("背景画像.pdf");
    }
}

PDF文書の背景画像の設定

上記は、Free Spire.PDF for Javaを使用して、PDFドキュメントの背景色と背景画像を設定する方法の紹介です。 Free Spire.PDF for Javaは、他にも多くの機能をサポートしていますので、Spire.PDF for Javaチュートリアルでご覧ください。

機能の例です:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?