学习C#调用OpenXml操作word文档的基本用法(16:学习文档脚注类)

OpenXml中使用FootnotesPart类保存Word文档内所有脚注的定义和文本。采用解压缩软件打开Word文档,其内的word文件夹中有独立的footnotes.xml文件,用于保存文档中使用的脚注信息,如下图所示。

  FootnotesPart类的Footnotes属性保存脚注集合,其命名空间及类型为DocumentFormat. OpenXml.Wordprocessing.Footnotes,通过调用Footnotes.Elements函数获取DocumentFormat. OpenXml.Wordprocessing.Footnote类型集合,Footnote类保存具体的脚注信息,继承自FootnoteEndnoteType类。
  FootnoteEndnoteType类的主要属性包括Id和Type,前者为脚注唯一ID,用于在文档正文中引用,而后者为脚注类型,包括ContinuationNotice(连续通知)、ContinuationSeparator(连续分隔符)、Normal(普通脚注或尾注)、Separator(分隔符)等,同时可通过Elements函数或GetFirstChild等函数获取脚注内容,可能的内容类型如下所示:

csharp 复制代码
DocumentFormat.OpenXml.Wordprocessing.AltChunk <w:altChunk>
DocumentFormat.OpenXml.Wordprocessing.BookmarkStart <w:bookmarkStart>
DocumentFormat.OpenXml.Wordprocessing.ContentPart <w:contentPart>
DocumentFormat.OpenXml.Wordprocessing.CustomXmlBlock <w:customXml>
DocumentFormat.OpenXml.Wordprocessing.CustomXmlInsRangeEnd <w:customXmlInsRangeEnd>
DocumentFormat.OpenXml.Wordprocessing.CustomXmlDelRangeEnd <w:customXmlDelRangeEnd>
DocumentFormat.OpenXml.Wordprocessing.CustomXmlMoveFromRangeEnd <w:customXmlMoveFromRangeEnd>
DocumentFormat.OpenXml.Wordprocessing.CustomXmlMoveToRangeEnd <w:customXmlMoveToRangeEnd>
DocumentFormat.OpenXml.Office2010.Word.CustomXmlConflictInsertionRangeEnd <w14:customXmlConflictInsRangeEnd>
DocumentFormat.OpenXml.Office2010.Word.CustomXmlConflictDeletionRangeEnd <w14:customXmlConflictDelRangeEnd>
DocumentFormat.OpenXml.Wordprocessing.BookmarkEnd <w:bookmarkEnd>
DocumentFormat.OpenXml.Wordprocessing.CommentRangeStart <w:commentRangeStart>
DocumentFormat.OpenXml.Wordprocessing.CommentRangeEnd <w:commentRangeEnd>
DocumentFormat.OpenXml.Wordprocessing.MoveFromRangeEnd <w:moveFromRangeEnd>
DocumentFormat.OpenXml.Wordprocessing.MoveToRangeEnd <w:moveToRangeEnd>
DocumentFormat.OpenXml.Wordprocessing.MoveFromRangeStart <w:moveFromRangeStart>
DocumentFormat.OpenXml.Wordprocessing.MoveToRangeStart <w:moveToRangeStart>
DocumentFormat.OpenXml.Wordprocessing.Paragraph <w:p>
DocumentFormat.OpenXml.Wordprocessing.PermEnd <w:permEnd>
DocumentFormat.OpenXml.Wordprocessing.PermStart <w:permStart>
DocumentFormat.OpenXml.Wordprocessing.ProofError <w:proofErr>
DocumentFormat.OpenXml.Wordprocessing.InsertedRun <w:ins>
DocumentFormat.OpenXml.Wordprocessing.DeletedRun <w:del>
DocumentFormat.OpenXml.Wordprocessing.MoveFromRun <w:moveFrom>
DocumentFormat.OpenXml.Wordprocessing.MoveToRun <w:moveTo>
DocumentFormat.OpenXml.Office2010.Word.RunConflictInsertion <w14:conflictIns>
DocumentFormat.OpenXml.Office2010.Word.RunConflictDeletion <w14:conflictDel>
DocumentFormat.OpenXml.Wordprocessing.SdtBlock <w:sdt>
DocumentFormat.OpenXml.Wordprocessing.Table <w:tbl>
DocumentFormat.OpenXml.Wordprocessing.CustomXmlInsRangeStart <w:customXmlInsRangeStart>
DocumentFormat.OpenXml.Wordprocessing.CustomXmlDelRangeStart <w:customXmlDelRangeStart>
DocumentFormat.OpenXml.Wordprocessing.CustomXmlMoveFromRangeStart<w:customXmlMoveFromRangeStart>
DocumentFormat.OpenXml.Wordprocessing.CustomXmlMoveToRangeStart<w:customXmlMoveToRangeStart>
DocumentFormat.OpenXml.Office2010.Word.CustomXmlConflictInsertionRangeStart<w14:customXmlConflictInsRangeStart>
DocumentFormat.OpenXml.Office2010.Word.CustomXmlConflictDeletionRangeStart

下面的代码示例用于获取word文档的脚注及内容:

csharp 复制代码
using (WordprocessingDocument document = WordprocessingDocument.Open(txtFilePath.Text, false))
{
    FootnotesPart fPart = document.MainDocumentPart.FootnotesPart;
    if (fPart != null && fPart.Footnotes != null)
    {
        Footnotes footnotes = fPart.Footnotes;

        // 遍历所有脚注
        foreach (Footnote fn in footnotes.Elements<Footnote>())
        {
            // 获取脚注ID
            Console.WriteLine($"脚注ID: {fn.Id?.Value}");

            // 获取脚注类型
            FootnoteEndnoteValues? footnoteType = fn.Type?.Value;

            // 提取脚注的段落文本内容
            DocumentFormat.OpenXml.Wordprocessing.Paragraph para = fn.GetFirstChild<DocumentFormat.OpenXml.Wordprocessing.Paragraph>();
            if (para != null)
            {
                Console.WriteLine($"内容: {para.InnerText}");
            }
        }
    }
}

Word文档中的尾注类EndnotesPart的用法与脚注类FootnotesPart极其相似,EndnotesPart类的Endnotes属性保存尾注集合,其命名空间及类型为DocumentFormat.OpenXml.Wordprocessing. Endnotes,通过调用Endnotes.Elements函数获取DocumentFormat.OpenXml.Wordprocessing. Endnote类型集合,Endnote类保存具体的尾注信息,继承自FootnoteEndnoteType类。

参考文献

1\]https://github.com/dotnet/Open-XML-SDK \[2\]https://learn.microsoft.com/zh-cn/office/open-xml/open-xml-sdk \[3\]https://learn.microsoft.com/zh-cn/dotnet/api/documentformat.openxml.wordprocessing.style?view=openxml-3.0.1 \[4\]https://blog.csdn.net/i042416/article/details/126228816 \[5\]https://zhuanlan.zhihu.com/p/40256815

相关推荐
缺点内向19 小时前
C#: 告别繁琐!轻松移除Word文档中的文本与图片水印
c#·自动化·word·.net
徐小夕@趣谈前端21 小时前
拒绝重复造轮子?我们偏偏花365天,用Vue3写了款AI协同的Word编辑器
人工智能·编辑器·word
kingwebo'sZone1 天前
C#使用Aspose.Words把 word转成图片
前端·c#·word
科技D人生1 天前
Vue.js 学习总结(20)—— Vue-Office 实战:word、pdf、excel、ppt 多种文档的在线预览
vue.js·word·vue-pdf·stylesheet·docx-preview·vue-office
weixin_416660072 天前
技术分析:豆包生成带公式文案导出Word乱码的底层机理
人工智能·word·豆包
骆驼爱记录2 天前
Word样式库不显示的8种修复方法
word·wps·新人首发
苍煜2 天前
超简单 poi-tl 学习博客:从0到1掌握Word生成(无需模板+模板填充)
学习·word
请为小H留灯2 天前
Word论文 封面、目录、页码设置步骤!(2026详细版教程)
毕业设计·word·论文格式
星尘库3 天前
在word中怎么把段落回车替换成空 删除空行
word
weixin_416660073 天前
AI 导出 Word 不正规?10 类文档样式模板(可直接套用,含字体/字号/行距/缩进)
人工智能·word·论文·排版·数学公式