好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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( "2002年11月4日,朱镕基总理和东盟10国领导人共同签署了《中国-东盟全面经济合作框架协议》,这标志着中国与东盟的经贸合作进入了一个新的历史阶段。" );

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

读取结果:

脚注读取结果:

尾注读取结果:

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

原文链接:http://HdhCmsTestcnblogs测试数据/Yesi/p/8952949.html

dy("nrwz");

查看更多关于C#添加、读取Word脚注尾注的方法的详细内容...

  阅读:45次