背景與前景
from ironpdf import *

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Render a PDF from a URL
pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")

# Add a PDF as background
pdf.AddBackgroundPdf("MyBackground.pdf")

# Add a PDF as foreground overlay to the first page
pdf.AddForegroundOverlayPdfToPage(0, "MyForeground.pdf", 0)

# Save the merged PDF
pdf.SaveAs("Complete.pdf")
pip install ironpdf
背景與前景

背景與前景

IronPDF for Python能通過將一個PDF作為另一個內容的背景或前景覆蓋,來分層PDF文件——對於應用信頭、浮水印或設計範本於生成文件中特別有用。

開始使用

加載或渲染一個基礎PdfDocument,然後調用AddBackgroundPdf或AddForegroundOverlayPdfToPage以將另一個PDF文件作為圖層應用。 使用SaveAs儲存結果。

理解程式碼

  • AddBackgroundPdf(path):將指定的PDF文件作為背景應用到當前文件的每一頁。 背景出現在現有頁面內容的後面。
  • AddForegroundOverlayPdfToPage(targetPageIndex, path, sourcePageIndex):將指定PDF中的單頁覆蓋到當前文件的特定頁面。 覆蓋出現在現有內容的上面。
    • targetPageIndex:在當前文件中應用覆蓋的零基頁索引。
    • sourcePageIndex:在覆蓋PDF中用作來源的零基頁索引。
  • SaveAs("Complete.pdf"):將分層的文件保存到磁碟。

使用案例

  • 信頭:將公司的信頭PDF作為背景應用到生成報告的每一頁。
  • 印章範本:將簽名區或批准印章覆蓋到特定頁面。
  • 設計層:通過保留佈局範本PDF並與動態生成的內容合併來分離內容與設計。

準備好開始了嗎?

版本:2026.11剛剛發布