IRONSOFTWAREHOME
新增 HTML 頁首與頁尾
using IronPdf;
using System;

// Instantiate Renderer
var renderer = new IronPdf.ChromePdfRenderer();


// Build a footer using html to style the text
// mergeable fields are:
// {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    MaxHeight = 15, //millimeters
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
    DrawDividerLine = true
};

// Use sufficient MarginBottom to ensure that the HtmlFooter does not overlap with the main PDF page content.
renderer.RenderingOptions.MarginBottom = 25; //mm


// Build a header using an image asset
// Note the use of BaseUrl to set a relative path to the assets
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    MaxHeight = 20, //millimeters
    HtmlFragment = "<img src='logo.png'>",
    BaseUrl = new Uri(@"C:\assets\images\").AbsoluteUri
};

// Use sufficient MarginTop to ensure that the HtmlHeader does not overlap with the main PDF page content.
renderer.RenderingOptions.MarginTop = 25; //mm
Imports IronPdf
Imports System

' Instantiate Renderer
Dim renderer = New IronPdf.ChromePdfRenderer()

' Build a footer using html to style the text
' mergeable fields are:
' {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter() With {
    .MaxHeight = 15, 'millimeters
    .HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
    .DrawDividerLine = True
}

' Use sufficient MarginBottom to ensure that the HtmlFooter does not overlap with the main PDF page content.
renderer.RenderingOptions.MarginBottom = 25 'mm

' Build a header using an image asset
' Note the use of BaseUrl to set a relative path to the assets
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter() With {
    .MaxHeight = 20,  'millimeters
    .HtmlFragment = "<img src='logo.png'>",
    .BaseUrl = New Uri("C:\assets\images\").AbsoluteUri
}

' Use sufficient MarginTop to ensure that the HtmlHeader does not overlap with the main PDF page content.
renderer.RenderingOptions.MarginTop = 25 'mm
NuGet Download
PM > Install-Package IronPdf
新增 HTML 頁首與頁尾

新增 HTML 頁首與頁尾

HTML 頁首與頁尾提供了一種靈活的方法,可為您的 PDF 文件建立動態的頁首與頁尾。 透過此方法新增頁首與頁尾,開發人員可完全掌控其樣式呈現,因為這些元素會以獨立的 HTML 文件形式渲染,並能包含專屬的資源檔案與樣式表。

使用 IronPDF 在 PDF 中新增自訂 HTML 頁首與頁尾的步驟

首先,您需要建立 ChromePdfRenderer 類別的實例,該類別負責將 HTML 內容渲染為像素級精準的 PDF 文件。

接著,請使用 HtmlHeaderFooter 類別定義頁尾,其中需指定 MaxHeight、頁尾的 HTML 內容(本例中包含頁碼),以及用於解決圖片的基礎 URL。 頁尾的樣式設定為居中顯示頁面資訊。

為避免頁尾與 PDF 主內容重疊,請使用 MarginBottom 屬性設定底部邊距。 同樣地,請使用 HtmlHeaderFooter 類別建立包含圖片(例如標誌)的標題。 我們已在包含您圖片資源的目錄中設置了 BaseUrl,以確保渲染時能呈現適當的圖片解析度。

最後,請使用 MarginTop 屬性設定頂部邊距,以避免標題與內容重疊。 此範例展示了使用 IronPDF 在 PDF 文件中實作自訂 HTML 頁首與頁尾是多麼簡單。

學習如何使用 IronPDF 為 PDF 文件新增 HTML 頁首與頁尾
在 GitHub 上查看

準備開始了嗎?

Nuget Downloads 20,878,335版本:2026.9剛剛發布

Key in blue circle

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

Your trial license will be sent to your email address

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

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

受到全球數百萬工程師的信任

Iron Software的客戶標誌
獲取您的無義務諮詢
填寫以下表格或電子郵件sales@ironsoftware.com
您的詳細資訊將始終保密
受到全球數百萬工程師的信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立