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.firstPage(), PageSelection.lastPage(), 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/add-foreground-page-range.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"));// Render the foreground overlayPdfDocument foreground = PdfDocument.renderHtmlAsPdf( "<h1 style='transform: rotate(-45deg); opacity: 0.5;'>Foreground Example</h1>");// Apply the foreground to pages 2 through 8 (zero-based index: 1 to 7)pdf.addForegroundPdf(foreground, PageSelection.pageRange(2, 8));pdf.saveAs(Paths.get("overlayForeground.pdf"));
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/background-foreground/add-foreground-page-range.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"));
// Render the foreground overlay
PdfDocument foreground = PdfDocument.renderHtmlAsPdf(
"<h1 style='transform: rotate(-45deg); opacity: 0.5;'>Foreground Example</h1>"
);
// Apply the foreground to pages 2 through 8 (zero-based index: 1 to 7)
pdf.addForegroundPdf(foreground, PageSelection.pageRange(2, 8));
pdf.saveAs(Paths.get("overlayForeground.pdf"));
Java
重要: 在某些重載中,pageRange索引是基於1的,在其他重載中是基於0的,具體取決於您調用的重載版本。 確認在您的版本中索引約定的具體重載簽名,請查閱IronPDF Java API Reference。
什麼時候應該為前景使用頁面範圍?
頁面特定的前景應用在一些生產情況中很有用:
文件部分:僅在附錄或補充頁上打上"DRAFT"覆疊。
版本跟踪:對已更新頁面標註修訂標記而不改動未更改的頁面。
選擇性機密:僅在包含敏感財務或醫療資料的頁面上應用"RESTRICTED"覆疊。
多語言文件:在雙語文件的相關頁面上放置語言特定的通知。
合同管理:對過期合同頁應用"VOID"覆疊,無需更改整個文件。
有哪些常見的頁面選擇模式?
以下模式涵蓋了大多數選擇性覆疊場景:
//: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.