新增經典文字頁首與頁尾
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.11剛剛發布