IronPDF for Java提供addForegroundPdf方法,能够在现有页面的后面或上面添加额外的PDF内容。 这些方法涵盖了广泛的实用叠加场景:信头、颜色填充、水印、批准印章和修订指示器。 库内部将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"));
//: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"));
可以。addBackgroundPdf和addForegroundPdf都接受一个可选的PageSelection参数。使用PageSelection.firstPage()、PageSelection.lastPage()、PageSelection.singlePage(int index)或PageSelection.pageRange(int start, int end)来定位页面子集。
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.