使用 C# 为 Word 文档添加脚注尾注
word文档中经常会使用脚注和尾注来为文档添加说明。脚注一般位于当前页面的底部或文字下方,用于作为文档某处内容的注释。而尾注一般位于文档的末尾,列出引文的出处等。接下来主要描述如何使用C# 为Word文档添加脚注尾注。
//新建一个word文档对象并加载需要添加脚注尾注的word文档Document document = new Document();document.LoadFromFile("Test.docx", FileFormat.Docx2010);//获取第一个段落Paragraph paragraph = document.Sections[0].Paragraphs[0];//添加脚注Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);//在第一段里查找字符串“Spire.Doc for .NET” 并用来添加脚注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 == "Spire.Doc for .NET") { //为添加脚注的字符串设置加粗格式 textRange.CharacterFormat.Bold = true; //插入脚注 paragraph.ChildObjects.Insert(i + 1, footnote); break; } }}//添加脚注内容并设置字体格式TextRange text = footnote.TextBody.AddParagraph().AppendText("Spire.Doc脚注");text.CharacterFormat.FontName = "Arial Black";text.CharacterFormat.FontSize = 10;text.CharacterFormat.TextColor = Color.DarkGray;footnote.MarkerCharacterFormat.FontName = "Calibri";footnote.MarkerCharacterFormat.FontSize = 12;footnote.MarkerCharacterFormat.Bold = true;footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;//获取第三段落Paragraph paragraph2 = document.Sections[0].Paragraphs[2];//添加尾注并设置尾注和格式Footnote endnote = paragraph2.AppendFootnote(FootnoteType.Endnote);TextRange text2 = endnote.TextBody.AddParagraph().AppendText("Spire.Doc尾注");text2.CharacterFormat.FontName = "Arial Black";text2.CharacterFormat.FontSize = 10;text2.CharacterFormat.TextColor = Color.DarkGray;endnote.MarkerCharacterFormat.FontName = "Calibri";endnote.MarkerCharacterFormat.FontSize = 12;endnote.MarkerCharacterFormat.Bold = true;endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;//保存文档document.SaveToFile("添加脚注尾注.docx", FileFormat.Docx2010);

C# 删除 Word 中的脚注、尾注
下面将介绍通过使用Spire.Doc for .NET来删除Word中的脚注、尾注。
//实例化Document类的对象,并加载测试文档 Document document = new Document(); document.LoadFromFile("test.docx"); //获取第一节 Section section = document.Sections[0]; //遍历所有段落 foreach (Paragraph para in section.Paragraphs) { int index = -1; //遍历段落中的子对象,并删除脚注、尾注 for (int i = 0, cnt = para.ChildObjects.Count; i < cnt; i++) { ParagraphBase pBase = para.ChildObjects[i] as ParagraphBase; if (pBase is Footnote) { index = i; break; } } if (index > -1) para.ChildObjects.RemoveAt(index); } //保存文档 document.SaveToFile("result.docx", FileFormat.Docx);

推荐阅读:【想要快速完成文档格式转换吗pire系列组件格式转换完整攻略来啦!】
*购买Spire.Doc正版授权的朋友可以点击“咨询在线客服”哦~~
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!