C#添加、读取Word脚注尾注的方法

2019-12-30 19:39:34于海丽

测试结果:

C#,Word,脚注,尾注

第三步 :读取脚注/尾注

【C#】


//创建Document类对象,加载需要测试的文档
  Document document = new Document();
  document.LoadFromFile("添加脚注尾注.docx");
  //获取文档第一个section
  Section section = document.Sections[0];

  //实例化StringBuilder类 
  StringBuilder sb = new StringBuilder();

  //遍历文档中所有段落
  foreach (Paragraph paragraph in section.Paragraphs)
  {
  for (int i = 0, cnt = paragraph.ChildObjects.Count; i < cnt; i++)
  {
   ParagraphBase pBase = paragraph.ChildObjects[i] as ParagraphBase;
   if (pBase is Footnote)
   {
   //若需要读取尾注,将此处FootnoteType.Footnote改成 FootnoteType.Endnote即可
   if ((pBase as Footnote).FootnoteType == FootnoteType.Footnote)
   {
    foreach (Paragraph footPara in (pBase as Footnote).TextBody.Paragraphs)
    {
    sb.Append(footPara.Text);
    }
   }
   }
  }
  }
//将读取内容写入文本并保存
File.WriteAllText("FootNotes.txt", sb.ToString());
//打开文档
System.Diagnostics.Process.Start("FootNotes.txt");

读取结果:

脚注读取结果:

C#,Word,脚注,尾注

尾注读取结果:

C#,Word,脚注,尾注

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。


注:相关教程知识阅读请移步到c#教程频道。