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 21,105,021版本:2026.9刚刚发布

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 天试用密钥。
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 “IronPDF”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

或在此处下载 Windows 安装程序。

  1. 下载并解压 IronPDF 到您的解决方案目录中的 ~/Libs 之类的位置
  2. 在 Visual Studio 解决方案资源管理器中,右键点击引用。选择浏览,“IronPDF.dll”

$999 起