背景和前景

import com.ironsoftware.ironpdf.*;  
import java.io.IOException;  
import java.nio.file.Paths;

// Load background and foreground PDFs from the filesystem (or create them programmatically)  
PdfDocument backgroundPdf = PdfDocument.fromFile(Paths.get("assets/MyBackground.pdf"));  
PdfDocument foregroundPdf = PdfDocument.fromFile(Paths.get("assets/MyForeground.pdf"));  
  
// Render content (HTML, URL, etc) as a PDF Document  
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.nuget.org/packages/IronPdf");  
  
// Add the background and foreground PDFs to the newly-rendered document.  
pdf.addBackgroundPdf(backgroundPdf);  
pdf.addForegroundPdf(foregroundPdf);  
  
pdf.saveAs(Paths.get("assets/BackgroundForegroundPdf.pdf"));
mvn install
背景和前景

背景和前景

要向PDF文档添加特定背景或前景元素,IronPDF提供了addForeground方法。 这些方法使开发人员能够使用一个 PDF 的内容作为另一个 PDF 的背景或前景。 这些方法对于基于通用设计模板生成成组的 PDF 特别有用。

由于这些方法使用fromFile方法从现有文件中获取它们,或使用可用的PDF渲染方法之一重新生成它们。

addForeground将默认使用多页PDF文档的第一页作为背景或前景。 要使用包含多页的 PDF 的其他页面,请在方法调用中将所需页面的索引添加为第二个参数。

要将PDF覆盖为正在处理的PDF上的背景或前景,请使用PageSelection对象指定要覆盖的页面。 下面的示例显示了如何处理 PDF 文档的单页和多个页面。

为PDF文档添加水印时,使用addBackground的替代方案,以便更轻松地控制背景定位和不透明度。

有关 PDF 操作的更多信息,请访问 IronPDF 的功能和文档。

学习用 Java 编辑 PDF 背景和前景
在 GitHub 上查看

准备开始了吗?

版本:2026.10刚刚发布