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 屬性定義頁尾。 您可以利用 {html-title} 及 {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
使用npm安裝

版本: 2026.6

  1. 下載並安裝Node.js 12+。
  2. 在終端機中執行上述命令。

授權從$999起