IRONSOFTWAREHOME
添加经典文本页眉和页脚
import {PdfDocument, AffixFonts} from "@ironsoftware/ironpdf";

(async () => {
    // Configure render options
    const options = {
        firstPageNumber: 1, // Use 2 if a cover page will be appended

        // Add a header to every page
        textHeader: {
            dividerLine: true,
            centerText: "{html-title}",
            font: AffixFonts.Helvetica, // Use the font name or font file path
            fontSize: 12,
        },

        // Add a footer
        textFooter: {
            dividerLine: true,
            leftText: "{date} {time}",
            rightText: "{page} of {total-pages}",
            font: AffixFonts.Arial, // Use the font name or font file path
            fontSize: 10,
        },
        // Mergeable fields are: {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}

        margin: {
            top: 25, // Create 25mm space for the header
            bottom: 25, // Create 25mm space for the footer
        },
    };

    // Define HTML content
    const htmlContent = "<h1>Hello IronPDF</h1>";

    // Render HTML content to a PDF with headers and footers
    const pdf = await PdfDocument.fromHtml(htmlContent, {renderOptions: options });

    // Save the PDF with headers and footers
    await pdf.saveAs("header_footer.pdf");
})();
npm i @ironsoftware/ironpdf
添加经典文本页眉和页脚

添加经典文本页眉和页脚

学习如何在渲染HTML内容时向PDF文档添加文本页眉和页脚。

要包含文本页眉和页脚,您需要在渲染选项对象中指定它们。 定义页眉内容时,使用分隔线、显示HTML标题的居中文本、字体设置和字体大小。同样,使用textFooter属性定义页脚。 您可以利用{pdf-title}等字段来个性化内容。

自定义边距以适当容纳页眉和页脚。

使用PdfDocument.fromHtml方法将您的HTML内容转换为PDF。 将renderOptions对象作为选项传递。

有关从HTML创建PDF的详细文档,请访问IronPDF文档。

生成的PDF文档,完整的页眉和页脚,保存在名为"header_footer.pdf"的文件中。

在 GitHub 上探索页眉和页脚示例代码
在 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 天试用密钥。
无需信用卡或创建账户
Node.js 模块下载用于 PDF
using npm 安装

版本: 2026.6

  1. 下载并安装 Node.js 12+。
  2. 在终端中执行上述命令。

$999 起