如何用 Java 在 PDF 上添加背景和覆盖前景

This article was translated from English: Does it need improvement?
Translated
View the article in English

作者:Mehr Muhammad Hamza

为 PDF 添加背景可让您在现有 PDF 内容后面插入图片或其他 PDF 文档,通过信纸、水印或设计功能等元素增强 PDF 的效果。 覆盖前景可让您在 PDF 的顶部放置附加内容,如注释、印章或签名。

IronPDF for Java 提供了实现这两点的简便方法。 您可以将渲染过的或现有的 PDF 用作背景或前景叠加,还可以灵活地对所有页面或特定页面进行更改。 在本指南中,我们将演示如何使用 IronPDF for Java 添加背景和叠加前景。

为 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"));
JAVA

输出 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"));
JAVA

backgroundPdfPageIndex 参数指定将背景 PDF 中的哪一页用作背景页。 该参数使用基于零的索引来指示从背景/前景 PDF 中复制的页面,默认设置为 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"));
JAVA

输出

输出 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"));
JAVA

探索 PageSelection 类

在处理前景和背景时,IronPDF 提供了灵活的方法,可使用 PageSelection 类中的方法指定应用前景和背景的页面。 以下是选项:

  • 首页()`:将更改应用于 PDF 的第一页。
  • 最后一页()`:将更改应用到 PDF 的最后一页。
  • 单页(int index)`:根据索引锁定特定页面(从 0 开始).
  • 页面范围(int startIndex, int endIndex)`:目标页面范围从 startIndex 到 endIndex(包容性).
  • 页面范围(列表page列表)`:将更改应用于特定页面列表,允许非顺序页面选择。