JavaでPDFに背景を追加し、前景をオーバーレイする方法
PDFに背景を追加すると、既存のPDFのコンテンツの後ろに画像や別のPDFドキュメントを挿入でき、レターヘッド、透かし、デザイン要素などで強化することができます。 前景を重ねることで、注釈、スタンプ、署名などの追加コンテンツをPDFの上に配置できます。
IronPDF for Javaは、どちらも簡単に実現する方法を提供します。 レンダリング済みまたは既存のPDFを背景または前景のオーバーレイとして使用することができ、すべてのページまたは特定のページに変更を適用する柔軟性があります。 このガイドでは、JavaでIronPDFを使用して背景を追加し、前景をオーバーレイする方法を示します。
JavaでPDFに背景を追加し、前景をオーバーレイする方法
- 背景と前景を追加するためのJavaライブラリをインストールする
- ターゲットPDFをインポートしてください。
- 背景または前景をレンダリングまたはインポートする
addBackgroundPdf
メソッドを使用して背景を追加します。addForegroundPdf
メソッドを使用して前景を追加します
PDFに背景を追加する
既存または新しくレンダリングされたPDFに背景を追加するには、addBackgroundPdf
メソッドを使用します。 この例では、PDFをインポートし、背景をレンダリングして、その背景をPDFに適用する方法を示しています。
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load the PDF file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));
// Load the background PDF
PdfDocument background = PdfDocument.renderHtmlAsPdf("<body style='background-color: cyan;'></body>");
// Add the background to all pages
pdf.addBackgroundPdf(background);
// Save the modified PDF
pdf.saveAs(Paths.get("addBackground.pdf"));
PDFを出力
生成された出力PDFファイルは次のようになります。
特定のページに背景を追加
同じaddBackgroundPdf
メソッドを使用して、選択した任意のページに背景を追加することもできます。 これは、表紙や特定のブランディングレイアウトなど、カスタムデザインを適用するのに役立ちます。 PageSelection クラスは必要であり、allPages
、singlePage
、pageRange
などの便利なメソッドを含んでいます。
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.edit.PageSelection;
// Load the PDF file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));
// Load the background PDF
PdfDocument background = PdfDocument.fromFile(Paths.get("background.pdf"));
// Add background only to the first page of the target PDF
// The second parameter (0) refers to the first page of the background PDF
pdf.addBackgroundPdf(background, 0, PageSelection.firstPage());
// Save the modified PDF
pdf.saveAs(Paths.get("addBackgroundToSpecificPage.pdf"));
backgroundPdfPageIndex パラメーターは、背景ページとして使用する背景PDFのページを指定します。 このパラメーターは、背景/前景PDFからコピーするページを示すために0ベースのインデックスを使用し、デフォルトは0に設定されています。
PDFに前景を追加
addForegroundPdf
メソッドは、既存のPDFページの上にコンテンツをオーバーレイするために使用できます。 これは、透かしやその他の視覚的指標などの要素を追加するのに役立ちます。 背景セクションと同様に、前景をレンダリングし、それをPDFドキュメントに適用します。
import com.ironsoftware.ironpdf.*;
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load the PDF file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));
// Create the foreground PDF using HTML content
PdfDocument foreground = PdfDocument.renderHtmlAsPdf("<h1 style='transform: rotate(-45deg); opacity: 0.5;'>Foreground Example</h1>");
// Add the foreground to all pages
pdf.addForegroundPdf(foreground);
// Save the modified PDF
pdf.saveAs(Paths.get("overlayForeground.pdf"));
出力
出力PDFファイルは次のとおりです:
特定のページに前景を追加
PageSelection.pageRange
メソッドを使用して、特定のページ範囲に前景をオーバーレイできます。 次に、ページ2から8に前景を適用する方法を説明します。
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.edit.PageSelection;
// Load the PDF file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));
// Create the foreground PDF using HTML content
PdfDocument foreground = PdfDocument.renderHtmlAsPdf("<h1 style='transform: rotate(-45deg); opacity: 0.5;'>Foreground Example</h1>");
// Add the foreground to a specific page range (from page 2 to page 8)
pdf.addForegroundPdf(foreground, PageSelection.pageRange(2, 8));
// Save the modified PDF
pdf.saveAs(Paths.get("overlayForeground.pdf"));
PageSelectionクラスを探索する
前景と背景を扱う際、IronPDFはPageSelectionクラスのメソッドを使用して、それらを適用すべきページを柔軟に指定する方法を提供します。 オプションは次のとおりです。
firstPage()
: PDF の最初のページに変更を適用します。lastPage()
: PDFの最終ページに変更を適用します。singlePage(int index)
: 指定したインデックス(0から始まる)に基づいて特定のページをターゲットにします。pageRange(int startIndex, int endIndex)
: startIndexからendIndexまでの範囲のページを対象とします(両端を含む)。pageRange(List<int> pageList)
: 特定のページのリストに変更を適用し、非連続ページの選択が可能です。