背景和前景

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

addBackground(PdfDocument backgroundPdf);
addForeground(PdfDocument foregroundPdf);
addBackground(PdfDocument backgroundPdf);
addForeground(PdfDocument foregroundPdf);
JAVA

由于这些方法与PdfDocument对象一起工作,开发人员可以使用fromFile方法从现有文件中获取它们,或使用一种可用的PDF 渲染方法重新生成它们。

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

// Use the third page of the background PDF as the background of every page  
// in the working PDF  
pdf.addBackground(backgroundPdf, 2);  

// Use the second page of the foreground PDF as the foreground of every page  
// of the working PDF  
pdf.addForeground(foregroundPdf, 1);
// Use the third page of the background PDF as the background of every page  
// in the working PDF  
pdf.addBackground(backgroundPdf, 2);  

// Use the second page of the foreground PDF as the foreground of every page  
// of the working PDF  
pdf.addForeground(foregroundPdf, 1);
JAVA

要将 PDF 叠加为工作 PDF 的特定页面的背景或前景,请使用 PageSelection 对象指定要叠加的页面。 以下示例展示了如何对PDF文档的单页和多页范围进行操作。

// Add the background to page 5 of the working PDF  
pdf.addBackground(backgroundPdf, PageSelection.singlePage(6));  

// Add a different background on pages 7 through 16 of the working PDF  
pdf.addBackground(backgroundPdf, PageSelection.pageRange(6, 15));  

// Add another background to just the first page.  
pdf.addBackground(backgroundPdf, PageSelection.firstPage());
// Add the background to page 5 of the working PDF  
pdf.addBackground(backgroundPdf, PageSelection.singlePage(6));  

// Add a different background on pages 7 through 16 of the working PDF  
pdf.addBackground(backgroundPdf, PageSelection.pageRange(6, 15));  

// Add another background to just the first page.  
pdf.addBackground(backgroundPdf, PageSelection.firstPage());
JAVA

要为您的 PDF 文档添加水印,请使用 addWatermark 方法,作为使用 addBackground 的替代方法,以更轻松地控制背景位置和不透明度。

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