背景與前景

若要將特定的背景或前景元素添加到您的 PDF 文件中,IronPDF 提供了 addBackgroundaddForeground 方法。這些方法使開發人員可以將一個 PDF 的內容用作另一個 PDF 的背景或前景。這些方法對於基於通用設計模板生成一組 PDF 特別有用。

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);
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());
JAVA

要對您的 PDF 文件加上浮水印,可以使用 addWatermark 方法,這比使用 addBackground 更容易控制背景位置和透明度。