PPTX

页眉与页脚

为幻灯片页脚添加日期、幻灯片编号和自定义文本

使用 HeaderFooter 在幻灯片中添加一致的元数据 — 日期、幻灯片编号和自定义文本。

演示文稿级别的页眉/页脚

通过 Presentation 为所有幻灯片应用页眉和页脚:

import { Presentation, Slide, Shape, HeaderFooter, Paragraph, TextRun } from "@office-open/pptx";

const pres = new Presentation({
    slides: [
        new Slide({
            children: [
                new Shape({
                    x: 1,
                    y: 1,
                    width: 8,
                    height: 5,
                    text: "Content slide",
                }),
            ],
        }),
        new Slide({
            children: [
                new Shape({
                    x: 1,
                    y: 1,
                    width: 8,
                    height: 5,
                    text: "Another slide",
                }),
            ],
        }),
    ],
    headerFooter: new HeaderFooter({
        dateTime: "2025-01-15",
        footer: "Confidential",
        slideNumber: true,
    }),
});

HeaderFooter 选项

属性类型说明
dateTimestring显示的日期/时间文本
footerstring自定义页脚文本
slideNumberboolean显示幻灯片编号
headerstring页眉文本(用于备注/讲义)

单张幻灯片覆盖

在个别幻灯片上覆盖演示文稿级别的页眉/页脚:

new Slide({
    children: [new Shape({ x: 1, y: 1, width: 8, height: 5, text: "Special slide" })],
    headerFooter: new HeaderFooter({
        footer: "Draft — Not for Distribution",
        slideNumber: false, // 在此幻灯片上隐藏编号
    }),
});

单张幻灯片的页眉/页脚

仅对特定幻灯片应用页眉/页脚:

new Slide({
    children: [],
    headerFooter: new HeaderFooter({
        dateTime: "January 2025",
        footer: "Internal Use Only",
        slideNumber: true,
    }),
});

完整示例

import {
    Presentation,
    Slide,
    Shape,
    Paragraph,
    TextRun,
    HeaderFooter,
    Packer,
} from "@office-open/pptx";
import fs from "node:fs";

const pres = new Presentation({
    title: "Quarterly Report",
    creator: "Finance Team",
    headerFooter: new HeaderFooter({
        dateTime: "Q4 2025",
        footer: "Confidential",
        slideNumber: true,
    }),
    slides: [
        new Slide({
            children: [
                new Shape({
                    x: 1,
                    y: 1,
                    width: 8,
                    height: 2,
                    paragraphs: [
                        new Paragraph({
                            alignment: "center",
                            children: [
                                new TextRun({ text: "Q4 2025 Report", fontSize: 36, bold: true }),
                            ],
                        }),
                    ],
                }),
            ],
        }),
        new Slide({
            children: [new Shape({ x: 1, y: 1, width: 8, height: 4, text: "Slide content" })],
        }),
    ],
});

const buffer = await Packer.toBuffer(pres);
fs.writeFileSync("report.pptx", buffer);
Copyright © 2026