资讯中心

docx 库端注(Endnotes)实战指南:在 .docx 文档末尾添加学术引用与参考文献

📅 2026/10/18 3:03:36
docx 库端注(Endnotes)实战指南:在 .docx 文档末尾添加学术引用与参考文献
docx 库端注Endnotes实战指南在 .docx 文档末尾添加学术引用与参考文献【免费下载链接】docxEasily generate and modify .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.项目地址: https://gitcode.com/GitHub_Trending/do/docx导读本文围绕docx一个用 JS/TS 以声明式 API 生成与修改.docx文件的库的Endnotes端注功能展开端注用于把引文、注释统一收集到文档末尾是学术论文、研究报告与正式出版物的常见排版需求。读完本文你将掌握如何通过Document构造器的endnotes属性定义端注内容、用EndnoteReferenceRun在正文中插入上标引用标记并理解端注在 OOXML 底层w:endnotes/w:endnoteReference的生成原理从而写出可直接运行、可在 Word 中正确渲染的带端注文档。前置知识端注依赖对Section节的理解。请先阅读 Sections 使用指南 了解文档分节机制。端注是什么与脚注有什么区别端注Endnote与脚注Footnote都是为正文提供补充说明的注释机制区别在于呈现位置脚注Footnote出现在每一页的页脚区域紧贴引用所在页面适合简短说明。参见 Footnotes 使用指南。端注Endnote统一出现在整个文档的末尾把所有引文、参考文献集中成节适合论文、专著等长篇引文场景——大量脚注会挤占页面主体而端注让正文保持干净连贯。在docx库中两者的 API 形态非常接近都由「内容定义 正文引用标记」两部分组成但底层生成的 OOXML 部件不同脚注写入word/footnotes.xml端注写入word/endnotes.xml。快速上手完整的端注示例以下是官方文档对应的完整示例来源为仓库中的演示文件 demo/97-endnotes.ts它展示了端注最核心的用法import { Document, EndnoteReferenceRun, Paragraph, TextRun } from docx; const doc new Document({ endnotes: { 1: { children: [new Paragraph(This is the first endnote with some detailed explanation.)] }, 2: { children: [new Paragraph(Second endnote), new Paragraph(With multiple paragraphs for more complex content.)] }, 3: { children: [new Paragraph(Third endnote referencing important source material.)] }, 4: { children: [new Paragraph(Fourth endnote from a different section.)] }, }, sections: [ { children: [ new Paragraph({ children: [ new TextRun(This document demonstrates endnotes functionality. ), new TextRun(Here is some text with an endnote reference), new EndnoteReferenceRun(1), new TextRun(. This allows for detailed citations and references ), new EndnoteReferenceRun(2), new TextRun( without cluttering the main text flow.), ], }), new Paragraph({ children: [ new TextRun(Endnotes appear at the end of the document, ), new TextRun(unlike footnotes which appear at the bottom of each page), new EndnoteReferenceRun(3), new TextRun(. This makes them ideal for academic papers and formal documents.), ], }), ], }, { children: [ new Paragraph({ children: [ new TextRun(This is content from a different section ), new TextRun(with its own endnote reference), new EndnoteReferenceRun(4), new TextRun(. Endnotes from all sections appear together at the document end.), ], }), ], }, ], });配合打包输出Packer用法详见 Packers 使用指南import * as fs from fs; import { Packer } from docx; Packer.toBuffer(doc).then((buffer) { fs.writeFileSync(My Document.docx, buffer); });示例要点总结在Document构造器中通过endnotes对象一次性声明所有端注内容在正文任意段落的children中用EndnoteReferenceRun(编号)插入引用标记示例故意分成两个sections用来验证「来自不同节的端注最终都会汇聚到文档末尾统一展示」。Document 配置endnotes 属性详解端注定义位于Document构造器的endnotes属性其类型与语义如下PropertyTypeNotesDescriptionendnotesRecordstring, { children: Paragraph[] }Optional可选以引用编号为键的端注定义集合使用规则键是引用编号字符串形式文档中给出的键写作1、2、3等数字字面量但类型上它们是Record的字符串键与EndnoteReferenceRun(id)中的id一一对应。值是包含children数组的对象children由Paragraph对象组成即每条端注本质上是一组段落。一条端注可以包含多个段落如示例中的端注 2用多个Paragraph表达更复杂的注释内容。从源码看Document在构造时会把options.endnotes逐条注册进端注包装器src/file/file.tsif (options.endnotes) { for (const key in options.endnotes) { this.endnotesWrapper.View.createEndnote(parseFloat(key), options.endnotes[key].children); } }这段代码有两个值得注意的细节键通过parseFloat(key)解析为数值型 id说明键虽然声明为字符串但会被转成数字后写入w:id属性因此键必须是可解析的数字字符串如1、2不能是note-a之类的非数字文本。id 需要唯一且与引用标记匹配正文中的EndnoteReferenceRun(id)与endnotes中的键必须严格对应Word 才会把引用标记链接到对应的端注内容。EndnoteReferenceRun的用法规则接收单个id参数与endnotes对象的键匹配自动应用上标样式该 Run 自带EndnoteReference字符样式在 Word 中渲染为标准的端注上标编号引用对应的端注内容Word 根据w:id建立正文标记与端注正文之间的关联。源码级原理端注在 OOXML 中如何生成理解底层 XML 结构有助于排查渲染异常如编号不对、样式丢失等。端注相关的实现集中在 src/file/endnotes 目录下。1. 端注部件与内置特殊条目Endnotes类src/file/endnotes/endnotes.ts生成根元素w:endnotes并自动写入两个特殊端注id 为 -1 的 SEPARATOR分隔符端注渲染为正文与端注区域之间的分隔线对应w:typeseparatorid 为 0 的 CONTINUATION_SEPARATOR续接分隔符端注当端注跨页续排时使用对应w:typecontinuationSeparator。这两个条目复用了脚注模块中的SeperatorRun/ContinuationSeperatorRun位于 src/file/footnotes/footnote/run并显式设置了段落间距line: 240、lineRule: AUTO、after: 0以贴近 Word 默认排版。因此自定义端注的 id 应从 1 开始-1与0已被库内部占用。在Endnote类src/file/endnotes/endnote/endnote.ts中每条端注被序列化为w:endnote元素并带有w:id与可选的w:type属性见 endnote-attributes.ts。同时库会自动为端注的第一个段落在段首注入引用标记 RunEndnoteRefRun确保 Word 在端注正文前显示对应的编号——这也是为什么定义端注时不需要手动加编号段落。2. 正文中的引用标记EndnoteReferenceRunsrc/file/endnotes/endnote/run/reference-run.ts是一个特殊的Run设置字符样式EndnoteReferencew:rStyle内部生成w:endnoteReference元素属性w:id即为传入的编号。对应地端注正文区由EndnoteRefRunsrc/file/endnotes/endnote/run/endnote-ref-run.ts生成端注区域内的回显编号同样使用EndnoteReference样式。3. 测试用例验证的 XML 结构仓库中的单元测试 src/file/endnotes/endnote/run/reference-run.spec.ts 精确断言了生成的 XML 结构可作为排查时的“预期产物”参考w:r w:rPr w:rStyle w:valEndnoteReference/ /w:rPr w:endnoteReference w:id1/ /w:r即在正文段落中引用标记是一个带EndnoteReference样式、指向w:id1的w:endnoteReference元素。若在 Word 中打开文档后端注编号不是上标或点击无跳转可优先检查生成的document.xml中该 Run 的rStyle与id是否与此结构一致。4. 与包的关系Document在打包时会注册word/endnotes.xml及对应 relationship见 src/file/file.ts端注因此成为 OOXML 包的标准部件之一与footnotes.xml平级。这意味着使用Packer.toBuffer/Packer.toString输出后Word、WPS 等支持 OOXML 的处理器均能正常识别端注。多节文档中的端注行为端注的一个典型特点是全局收集不管引用标记出现在第几个 Section所有端注内容最终都集中显示在整个文档的末尾而不是各自 Section 的末尾。这正是示例构造两个sections的用意——第二个 Section 中插入的EndnoteReferenceRun(4)与第一个 Section 的端注 1、2、3 一起在文档末尾按 id 顺序统一呈现。这一点与脚注形成鲜明对比脚注按页就近显示受每节页眉页脚/页面设置影响较大端注则与节边界无关适合把「正文分节 文末统一参考文献」的排版组合。若你需要为不同节定制端注区域样式可从 Word 的「引用 → 脚注/尾注设置」中对端注的编号格式、起始编号、应用范围进行控制docx库本身在文档结构层面已保证多节端注的汇聚行为。常见问题与排查建议点击正文标记无法跳转到端注检查EndnoteReferenceRun(id)的id是否与endnotes对象的键完全一致并确认键可被parseFloat解析即纯数字字符串。库内部通过parseFloat(key)转换src/file/file.ts非数字键会得到NaN而无法正确关联。端注编号未显示为文档末尾连续编号确认自定义端注 id 从 1 开始且连续、唯一-1与0被库保留给分隔符与续接分隔符见 src/file/endnotes/endnotes.ts。端注区没有分隔线分隔线由库自动注入的 SEPARATOR 特殊端注渲染若被误删或覆盖请核对endnotes.xml中是否存在w:id-1且w:typeseparator的条目。端注内容想包含复杂排版端注的children是Paragraph[]意味着每个段落可像正文段落一样使用各类 Run、样式、编号列表等能力多段落内容直接追加多个Paragraph即可参考示例端注 2。延伸阅读Footnotes 使用指南按页展示的注释机制与端注互补Sections 使用指南理解多节文档结构端注的前置知识Packers 使用指南把Document打包为.docx文件或 Base64 字符串完整可运行示例demo/97-endnotes.ts端注核心实现src/file/endnotes含endnotes.ts、endnote/endnote.ts、endnote/run/reference-run.ts端注 XML 结构测试src/file/endnotes/endnote/run/reference-run.spec.ts。【免费下载链接】docxEasily generate and modify .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.项目地址: https://gitcode.com/GitHub_Trending/do/docx创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

看完文章,想为自己的企业也做一次专业网站诊断?

尧图顾问免费为您评估现有网站,并给出建站/改版建议与报价方案。

免费获取方案