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クラスのメソッドを使用して、それらを適用すべきページを柔軟に指定する方法を提供します。 オプションは次のとおりです。
最初のページ()
: PDF の最初のページに変更を適用します。-
lastPage()
: PDFの最後のページに変更を適用します。singlePage(インデックス)
: インデックスに基づいて特定のページをターゲットにする(0から始める).ページ範囲(int startIndex, int endIndex)`: startIndexからendIndexまでの範囲のページを対象とします。(包括的).
ページ範囲(リスト
pageリスト)`: 特定のページのリストに変更を適用し、連続しないページの選択を可能にします。