文档星空最火知名网站>>Spire.Doc系列教程>>Word .NET库组件Spire.Doc系列教程(37):添加和删除脚注尾注
Word .NET库组件Spire.Doc系列教程(37):添加和删除脚注尾注
Spire.Doc for .NET是一个专业的Word .NET库,设计用于帮助开发人员高效地开发创建、阅读、编写、转换和打印任何来自.NET( C#, VB.NET, ASP.NET)平台的Word文档文件的功能。
本系列教程将为大家带来Spire.Doc for .NET在使用过程中的各类实际操作,word文档中经常会使用脚注和尾注来为文档添加说明。本文主要描述如何使用C# 为Word文档添加和删除脚注尾注。
使用 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);
推荐阅读:【想要快速完成文档格式转换吗?Spire系列组件格式转换完整攻略来啦!】
*购买Spire.Doc正版授权的朋友可以点击哦~~

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线