IronPDF for Javaは、既存のページの背後または上に追加のPDFコンテンツを配置するaddForegroundPdfメソッドを提供します。 これらのメソッドは、実用的なオーバーレイスシナリオ全体をカバーしています:レターヘッド、カラーフィル、ウォーターマーク、承認スタンプ、改訂インジケータ。 このライブラリは内部的にHTMLをPDFにレンダリングするため、有効なCSS表現(グラデーション、画像、タイポグラフィ)はマニュアルのPDFストリーム操作なしでバックグラウンドまたはフォアグラウンドレイヤーになります。
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/background-foreground/quickstart.javaimport com.ironsoftware.ironpdf.*;import com.ironsoftware.ironpdf.edit.PageSelection;import java.nio.file.Paths;// Load the target PDFPdfDocument pdf = PdfDocument.fromFile(Paths.get("document.pdf"));// Render a background from HTML and apply it to all pagesPdfDocument background = PdfDocument.renderHtmlAsPdf("<body style='background-color: #f0f0f0;'></body>");pdf.addBackgroundPdf(background);// Render a foreground overlay and apply it to all pagesPdfDocument foreground = PdfDocument.renderHtmlAsPdf("<h1 style='opacity: 0.3;'>DRAFT</h1>");pdf.addForegroundPdf(foreground);pdf.saveAs(Paths.get("output.pdf"));
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/background-foreground/quickstart.java
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.edit.PageSelection;
import java.nio.file.Paths;
// Load the target PDF
PdfDocument pdf = PdfDocument.fromFile(Paths.get("document.pdf"));
// Render a background from HTML and apply it to all pages
PdfDocument background = PdfDocument.renderHtmlAsPdf("<body style='background-color: #f0f0f0;'></body>");
pdf.addBackgroundPdf(background);
// Render a foreground overlay and apply it to all pages
PdfDocument foreground = PdfDocument.renderHtmlAsPdf("<h1 style='opacity: 0.3;'>DRAFT</h1>");
pdf.addForegroundPdf(foreground);
pdf.saveAs(Paths.get("output.pdf"));
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/background-foreground/add-background.javaimport com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument;import java.nio.file.Paths;// Activate the licenseLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");// Load the target PDFPdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));// Render a background from an HTML color definitionPdfDocument background = PdfDocument.renderHtmlAsPdf("<body style='background-color: cyan;'></body>");// Apply the background to all pagespdf.addBackgroundPdf(background);pdf.saveAs(Paths.get("addBackground.pdf"));
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/background-foreground/add-background.java
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import java.nio.file.Paths;
// Activate the license
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load the target PDF
PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));
// Render a background from an HTML color definition
PdfDocument background = PdfDocument.renderHtmlAsPdf("<body style='background-color: cyan;'></body>");
// Apply the background to all pages
pdf.addBackgroundPdf(background);
pdf.saveAs(Paths.get("addBackground.pdf"));
PageSelection引数を受け取ります。 ページの任意の部分集合をターゲットにするためにPageSelection.pageRange(int start, int end)を使用します。 完全なPageSelectionドキュメントはIronPDF for Java API Referenceにあります。
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/background-foreground/add-background-specific-page.javaimport com.ironsoftware.ironpdf.*;import com.ironsoftware.ironpdf.edit.PageSelection;import java.nio.file.Paths;// Load the target PDFPdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));// Load a background from an existing PDF filePdfDocument background = PdfDocument.fromFile(Paths.get("background.pdf"));// Apply only the first page of the background PDF to the first page of the targetpdf.addBackgroundPdf(background, 0, PageSelection.firstPage());pdf.saveAs(Paths.get("addBackgroundToSpecificPage.pdf"));
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/background-foreground/add-background-specific-page.java
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.edit.PageSelection;
import java.nio.file.Paths;
// Load the target PDF
PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));
// Load a background from an existing PDF file
PdfDocument background = PdfDocument.fromFile(Paths.get("background.pdf"));
// Apply only the first page of the background PDF to the first page of the target
pdf.addBackgroundPdf(background, 0, PageSelection.firstPage());
pdf.saveAs(Paths.get("addBackgroundToSpecificPage.pdf"));
addForegroundPdfメソッドは、既存のページコンテンツの上にPDFレイヤーを合成します。 レンダリングされたフォアグラウンドは、各ターゲットページのすべての既存のテキスト、画像、およびグラフィックの上に表示されます。 このメソッドは、基礎の内容に関係なく表示され続ける必要があるウォーターマーク、承認スタンプ、および改訂インジケータの標準的なアプローチです。 動作するコードサンプルについては、背景とフォールグラウンド for Java例をご覧ください。
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/background-foreground/add-foreground.javaimport com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument;import java.nio.file.Paths;// Activate the licenseLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");// Load the target PDFPdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));// Render a diagonal text stamp as the foreground layerPdfDocument foreground = PdfDocument.renderHtmlAsPdf( "<h1 style='transform: rotate(-45deg); opacity: 0.5;'>Foreground Example</h1>");// Apply the foreground to all pagespdf.addForegroundPdf(foreground);pdf.saveAs(Paths.get("overlayForeground.pdf"));
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/background-foreground/add-foreground.java
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import java.nio.file.Paths;
// Activate the license
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// Load the target PDF
PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));
// Render a diagonal text stamp as the foreground layer
PdfDocument foreground = PdfDocument.renderHtmlAsPdf(
"<h1 style='transform: rotate(-45deg); opacity: 0.5;'>Foreground Example</h1>"
);
// Apply the foreground to all pages
pdf.addForegroundPdf(foreground);
pdf.saveAs(Paths.get("overlayForeground.pdf"));
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/background-foreground/page-selection-patterns.javaimport com.ironsoftware.ironpdf.*;import com.ironsoftware.ironpdf.edit.PageSelection;import java.util.ArrayList;import java.util.List;import java.nio.file.Paths;PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));PdfDocument foreground = PdfDocument.renderHtmlAsPdf("<h1 style='opacity: 0.4;'>DRAFT</h1>");PdfDocument background1 = PdfDocument.fromFile(Paths.get("cover-background.pdf"));PdfDocument background2 = PdfDocument.fromFile(Paths.get("end-background.pdf"));// Apply foreground to all even-numbered pages (zero-based)List<Integer> evenPages = new ArrayList<>();for (int i = 1; i < pdf.getPageCount(); i += 2) { evenPages.add(i);}pdf.addForegroundPdf(foreground, PageSelection.pageRange(evenPages));// Apply different backgrounds to the first and last pagespdf.addBackgroundPdf(background1, 0, PageSelection.firstPage());pdf.addBackgroundPdf(background2, 0, PageSelection.lastPage());// Apply foreground to all pages except the first and lastif (pdf.getPageCount() > 2) { pdf.addForegroundPdf(foreground, PageSelection.pageRange(1, pdf.getPageCount() - 2));}pdf.saveAs(Paths.get("selective-overlay.pdf"));
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/background-foreground/page-selection-patterns.java
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.edit.PageSelection;
import java.util.ArrayList;
import java.util.List;
import java.nio.file.Paths;
PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));
PdfDocument foreground = PdfDocument.renderHtmlAsPdf("<h1 style='opacity: 0.4;'>DRAFT</h1>");
PdfDocument background1 = PdfDocument.fromFile(Paths.get("cover-background.pdf"));
PdfDocument background2 = PdfDocument.fromFile(Paths.get("end-background.pdf"));
// Apply foreground to all even-numbered pages (zero-based)
List<Integer> evenPages = new ArrayList<>();
for (int i = 1; i < pdf.getPageCount(); i += 2) {
evenPages.add(i);
}
pdf.addForegroundPdf(foreground, PageSelection.pageRange(evenPages));
// Apply different backgrounds to the first and last pages
pdf.addBackgroundPdf(background1, 0, PageSelection.firstPage());
pdf.addBackgroundPdf(background2, 0, PageSelection.lastPage());
// Apply foreground to all pages except the first and last
if (pdf.getPageCount() > 2) {
pdf.addForegroundPdf(foreground, PageSelection.pageRange(1, pdf.getPageCount() - 2));
}
pdf.saveAs(Paths.get("selective-overlay.pdf"));
Can IronPDF handle CSS for designing PDF overlays?
Yes, IronPDF uses an HTML-to-PDF engine that supports modern CSS, including gradients, typography, and images. This capability allows you to use any CSS that works in a browser for your PDF overlays.
How do you choose between backgrounds and foregrounds for PDF overlays?
Choose backgrounds for designs beneath existing content, like watermarks and letterheads, and foregrounds for layers above existing content, like approval stamps and annotations, which should be visible regardless of the underlying material.
How can I use IronPDF to compress PDFs after applying overlays?
After applying high-resolution overlays, you can use IronPDF's PDF compression features to reduce file sizes, which is useful for distribution or archiving. Refer to IronPDF's compression examples for implementation.
What is required to start using IronPDF in a Java project?
To use IronPDF in a Java project, include it as a dependency in your `pom.xml` from Maven Central, and activate your license key before executing API calls. Full setup details are available in IronPDF's documentation.