word文档中经常会使用脚注和尾注来为文档添加说明。脚注一般位于当前页面的底部或文字下方,用于作为文档某处内容的注释。而尾注一般位于文档的末尾,列出引文的出处等。该文章主要描述如何使用c# 为word文档添加脚注尾注。
c#
//新建一个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);
vb.net
'新建一个word文档对象并加载需要添加脚注尾注的word文档
dim document as document = new document
document.loadfromfile("test.docx", fileformat.docx2010)
'获取第一个段落
dim paragraph as paragraph = document.sections(0).paragraphs(0)
'添加脚注
dim footnote as footnote = paragraph.appendfootnote(footnotetype.footnote)
'在第一段里查找字符串“spire.doc for .net” 并用来添加脚注
dim obj as documentobject = nothing
dim i as integer = 0
do while (i < paragraph.childobjects.count)
obj = paragraph.childobjects(i)
if (obj.documentobjecttype = documentobjecttype.textrange) then
dim textrange as textrange = ctype(obj,textrange)
if (textrange.text = "spire.doc for .net") then
'为添加脚注的字符串设置加粗格式
textrange.characterformat.bold = true
'插入脚注
paragraph.childobjects.insert((i 1), footnote)
exit for
end if
end if
i = (i 1)
loop
'添加脚注内容并设置字体格式
dim text as textrange = 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
'获取第三段落
dim paragraph2 as paragraph = document.sections(0).paragraphs(2)
'添加尾注并设置尾注和格式
dim endnote as footnote = paragraph2.appendfootnote(footnotetype.endnote)
dim text2 as textrange = 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)
word脚注尾注效果图: