C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#添加读取Word脚注尾注

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

作者:E-iceblue

这篇文章主要为大家详细介绍了C#添加、读取Word脚注尾注的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#添加读取Word脚注尾注的具体代码,供大家参考,具体内容如下

脚注和尾注是对文本的补充说明。脚注一般位于页面的底部,可以作为文档某处内容的注释;尾注一般位于文档的末尾,列出引文 的出处等。在本示例中将介绍如何来添加或删除Word脚注。

工具使用: Free Spire. Doc for .NET(免费版)

第一步:dll引用

第二步:添加Word脚注、尾注

【C#】

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertFootnote_Doc
{
 class Program
 {
 static void Main(string[] args)
 {
  //新建一个word文档对象并加载需要添加脚注尾注的word文档
  Document document = new Document();
  document.LoadFromFile("sample.docx", FileFormat.Docx2010);
  //获取第3个段落
  Paragraph paragraph = document.Sections[0].Paragraphs[2];
  //添加脚注
  Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);
  //在第一段里查找指定字符串,并添加脚注
  DocumentObject obj = null;
  for (int i = 0; i < paragraph.ChildObjects.Count; i++)
  {
  obj = paragraph.ChildObjects[i];
  if (obj.DocumentObjectType == DocumentObjectType.TextRange)
  {
   TextRange textRange = obj as TextRange;
   if (textRange.Text == "中国——东盟自贸区框架")
   {
   //为添加脚注的字符串设置加粗格式
   textRange.CharacterFormat.Bold = true;
   //插入脚注
   paragraph.ChildObjects.Insert(i + 1, footnote);
   break;
   }
  }
  }
  //添加脚注内容被设置字体格式
  TextRange text = footnote.TextBody.AddParagraph().AppendText("123456789");
  text.CharacterFormat.FontName = "Arial Black";
  text.CharacterFormat.FontSize = 9;
  text.CharacterFormat.TextColor = Color.DarkGray;
  footnote.MarkerCharacterFormat.FontName = "Calibri";
  footnote.MarkerCharacterFormat.FontSize = 12;
  footnote.MarkerCharacterFormat.Bold = true;
  footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;
  //获取第5段落
  Paragraph paragraph2 = document.Sections[0].Paragraphs[4];
  //添加尾注并设置尾注和格式
  Footnote endnote = paragraph2.AppendFootnote(FootnoteType.Endnote);
  TextRange text2 = endnote.TextBody.AddParagraph().AppendText("太过分合同"
  +"“飞天狗还让他一人同一个人"
  +"染头发好人法回锅肉饭"
  +"分他争的新优势。");
  text2.CharacterFormat.FontName = "Arial Black";
  text2.CharacterFormat.FontSize = 9;
  text2.CharacterFormat.TextColor = Color.Black;
  endnote.MarkerCharacterFormat.FontName = "Calibri";
  endnote.MarkerCharacterFormat.FontSize = 12;
  endnote.MarkerCharacterFormat.Bold = false;
  endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;
  //保存并打开文档
  document.SaveToFile("添加脚注尾注.docx", FileFormat.Docx2010);
  System.Diagnostics.Process.Start("添加脚注尾注.docx");
 }
 }
}

第三步 :读取脚注/尾注

【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");

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

您可能感兴趣的文章:
阅读全文