脚注和尾注是对文本的补充说明。脚注一般位于页面的底部,可以作为文档某处内容的注释;尾注一般位于文档的末尾,列出引文 的出处等。在本示例中将介绍如何来添加或读取Word脚注尾注。
工具使用: Free Spire. Doc for .NET(免费版)
第一步 :dll引用第二步: 添加Word脚注、尾注【C#】
using Spire.Doc; Spire.Doc.documents; Spire.Doc.FIElds; 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 + 1break; } } } 添加脚注内容被设置字体格式 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 = ; footnote.MarkerCharacterFormat.Textcolor = color.DarkGreen; 获取第5段落 Paragraph paragraph2 = document.Sections[4添加尾注并设置尾注和格式 Footnote endnote = paragraph2.AppendFootnote(FootnoteType.Endnote); TextRange text2 = endnote.Textbody.AddParagraph().AppendText(党的十七大报告明确指出:" +“坚持对外开放的基本国策,把‘引进来’和‘走出去’更好地结合起来,扩大开放领域,优化开放结构,提高开放质量,完善内外联动,互利共赢、安全高效的开放型经济体系,形成经济全球化条件下参与国际经济合作和竞争的新优势。); text2.CharacterFormat.Fontname = ; text2.CharacterFormat.FontSize = ; text2.CharacterFormat.Textcolor = color.Black; endnote.MarkerCharacterFormat.Fontname = ; endnote.MarkerCharacterFormat.FontSize = ; endnote.MarkerCharacterFormat.Bold = false; endnote.MarkerCharacterFormat.Textcolor = color.DarkGreen; 保存并打开文档 document.Savetofile(添加脚注尾注.docx); } }}
测试结果:
第三步 :读取脚注/尾注【C#】
创建document类对象,加载需要测试的文档 document document = ); 获取文档第一个section Section section = document.Sections[0实例化StringBuilder类 StringBuilder sb = StringBuilder(); 遍历文档中所有段落 foreach (Paragraph paragraph in section.Paragraphs) { 0,cnt = paragraph.Childobjects.Count; i < cnt; i++) { ParagraphBase pBase = paragraph.Childobjects[i] ParagraphBase; if (pBase is Footnote) { 若需要读取尾注,将此处FootnoteType.Footnote改成 FootnoteType.Endnote即可 if ((pBase as Footnote).FootnoteType == FootnoteType.Footnote) { foreach (Paragraph footPara in (pBase Footnote).Textbody.Paragraphs) { sb.Append(footPara.Text); } } } } } 将读取内容写入文本并保存 file.WriteallText(FootNotes.txt打开文档 System.Diagnostics.Process.Start(");
读取结果:
脚注读取结果:
尾注读取结果:
总结
以上是内存溢出为你收集整理的C# 添加、读取Word脚注尾注全部内容,希望文章能够帮你解决C# 添加、读取Word脚注尾注所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)