最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#添加、讀取Word腳注尾注的方法

 更新時間:2026年05月09日 09:13:51   作者:E-iceblue  
這篇文章主要為大家詳細(xì)介紹了C#添加、讀取Word腳注尾注的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#添加讀取Word腳注尾注的具體代碼,供大家參考,具體內(nèi)容如下

腳注和尾注是對文本的補(bǔ)充說明。腳注一般位于頁面的底部,可以作為文檔某處內(nèi)容的注釋;尾注一般位于文檔的末尾,列出引文 的出處等。在本示例中將介紹如何來添加或刪除Word腳注。

工具使用: Free Spire. Doc for .NET(免費(fèi)版)

第一步: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 == "中國——東盟自貿(mào)區(qū)框架")
   {
   //為添加腳注的字符串設(shè)置加粗格式
   textRange.CharacterFormat.Bold = true;
   //插入腳注
   paragraph.ChildObjects.Insert(i + 1, footnote);
   break;
   }
  }
  }
  //添加腳注內(nèi)容被設(shè)置字體格式
  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];
  //添加尾注并設(shè)置尾注和格式
  Footnote endnote = paragraph2.AppendFootnote(FootnoteType.Endnote);
  TextRange text2 = endnote.TextBody.AddParagraph().AppendText("太過分合同"
  +"“飛天狗還讓他一人同一個人"
  +"染頭發(fā)好人法回鍋肉飯"
  +"分他爭的新優(yōu)勢。");
  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#】

//創(chuàng)建Document類對象,加載需要測試的文檔
  Document document = new Document();
  document.LoadFromFile("添加腳注尾注.docx");
  //獲取文檔第一個section
  Section section = document.Sections[0];

  //實(shí)例化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);
    }
   }
   }
  }
  }
//將讀取內(nèi)容寫入文本并保存
File.WriteAllText("FootNotes.txt", sb.ToString());
//打開文檔
System.Diagnostics.Process.Start("FootNotes.txt");

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

平罗县| 龙游县| 万源市| 海伦市| 南木林县| 叶城县| 宝应县| 镇巴县| 山西省| 汕头市| 册亨县| 安宁市| 庆安县| 廉江市| 元谋县| 茂名市| 富蕴县| 同德县| 新巴尔虎右旗| 翼城县| 沭阳县| 柳江县| 仪征市| 昌乐县| 资兴市| 徐水县| 邢台县| 海宁市| 黄浦区| 泾川县| 基隆市| 无极县| 正镶白旗| 资阳市| 桐梓县| 襄城县| 富平县| 安仁县| 临夏市| 南漳县| 台州市|