IRONSOFTWAREHOME
Backgrounds & Foregrounds

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
Backgrounds & Foregrounds

Backgrounds & Foregrounds

若要在 PDF 文件中新增特定的背景或前景元素,IronPDF 提供了 addBackground 和 addForeground 方法。 這些方法讓開發人員能夠將一個 PDF 的內容用作另一個 PDF 的背景或前景。 這些方法特別適用於根據共同的設計範本生成多份 PDF 檔案。

由於這些方法適用於 PdfDocument 物件,開發人員可透過 fromFile 方法從現有檔案載入這些物件,或使用現有的 PDF 渲染方法重新生成它們。

addBackground 和 addForeground 預設會將多頁 PDF 文件的第一頁用作背景或前景。 若要從包含多頁的 PDF 檔案中選取特定頁面,請在方法呼叫中將目標頁面的索引編號作為第二個參數傳入。

若要在現有 PDF 的特定頁面中,將 PDF 作為背景或前景疊加,請使用 PageSelection 物件指定要疊加的頁面。 以下範例示範如何針對 PDF 文件的單一頁面及頁面範圍進行此操作。

若要為 PDF 文件新增浮水印,建議使用 addWatermark 方法,而非 addBackground,以便更輕鬆地控制背景位置與不透明度。

如需更多關於 PDF 處理的資訊,請參閱 IronPDF 的功能與文件。

學習如何在 Java 中編輯 PDF 的背景與前景
在GitHub上查看

準備好開始了嗎?

版本:2026.6剛剛發布

Key in blue circle

立即免費取得 30 天試用金鑰。

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

OR
bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
預訂您的免費 即時演示
Booking Badge

全球數百萬工程師的信任

Iron Software的客戶商標
獲取您的無義務諮詢
填寫以下表格或發送電子郵件至sales@ironsoftware.com
您的詳細資訊始終保持機密。
全球數百萬工程師的信任
Iron Software的客戶商標
立即獲取您的30天試用金鑰。
無需信用卡或帳戶建立
PDF的Java Maven程式庫
using Maven 安裝

版本: 2026.6

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>ironpdf</artifactId>
   <version>2026.6.1</version>
</dependency>
https://central.sonatype.com/artifact/com.ironsoftware/ironpdf/2026.6.1
or
Java PDF JAR
下載 JAR

版本: 2026.6

手動安裝到您的專案中