背景與前景

為將特定背景或前景元素添加到您的 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 的功能和文檔