Word中的脚注和尾注都是对文本的补充说明。脚注一般是附在书页最左下端的注文,用以解释、说明特定内容;而尾注则是位于文档末尾,用于列出引文的出处。脚注和尾注都可以是针对某些文字或者段落来添加。本文中,将通过java演示如何添加脚注及尾注到Word文档以及读取、删除Word中已有的脚注尾注。
使用工具:
Free Spire.Doc for Java (免费版)
Jar文件获取及导入:
方法
1
:通过官网
下载jar文件包,并解压。解压文件后,将lib文件夹中的Spire.Doc.jar文件导入Java程序。
方法
2
:通过maven仓库
导入。
【示例1】添加脚注、尾注
importcom.spire.doc.*;
importcom.spire.doc.documents.Paragraph;
importcom.spire.doc.documents.TextSelection;
importcom.spire.doc.fields.Footnote;
importcom.spire.doc.fields.TextRange;
importjava.awt.*;
publicclassAddFootnoteEndnote{
publicstaticvoidmain(String[]args){
//加载测试文档
Documentdoc=newDocument("test.doc");
//添加脚注1:给指定段落添加脚注
Paragraphpara1=doc.getSections().get(0).getParagraphs().get(2);//获取段落
Footnotefootnote1=para1.appendFootnote(FootnoteType.Footnote);//添加脚注
TextRangetext1=footnote1.getTextBody().addParagraph().appendText("详见附件内容");
text1.getCharacterFormat().setFontName("楷书");//格式化脚注标签及脚注内容
text1.getCharacterFormat().setFontSize(10);
text1.getCharacterFormat().setTextColor(newColor(255,140,0));
footnote1.getMarkerCharacterFormat().setFontName("楷书");
footnote1.getMarkerCharacterFormat().setFontSize(14);
footnote1.getMarkerCharacterFormat().setTextColor(newColor(0,0,139));
//添加脚注2:给指定文本添加脚注
TextSelection[]selections=doc.findAllString("消除缺陷",false,true);
for(TextSelectionselection:selections){
TextRangerange=selection.getAsOneRange();
Paragraphpara2=range.getOwnerParagraph();
Footnotefootnote2=para2.appendFootnote(FootnoteType.Footnote);
intindex=para2.getChildObjects().indexOf(range);
para2.getChildObjects().insert(index+1,footnote2);
TextRangetext2=footnote2.getTextBody().addParagraph().appendText("请查看操作手册");
text2.getCharacterFormat().setFontName("ArialBlack");
text2.getCharacterFormat().setFontSize(10);
text2.getCharacterFormat().setTextColor(newColor(153,50,204));
footnote2.getMarkerCharacterFormat().setFontName("Calibri");
footnote2.getMarkerCharacterFormat().setFontSize(14);
footnote2.getMarkerCharacterFormat().setTextColor(newColor(0,0,139));
//添加尾注:给指定段落添加尾注(给指定文本添加尾注可参考以上添加脚注的代码方法)
Paragraphpara3=doc.getSections().get(0).getParagraphs().get(15);
Footnoteendnote=para3.appendFootnote(FootnoteType.Endnote);
TextRangetext3=endnote.getTextBody().addParagraph().appendText("引用自刘玲《操作手册》");
text3.getCharacterFormat().setFontName("ArialBlack");
text3.getCharacterFormat().setFontSize(10);
text3.getCharacterFormat().setTextColor(newColor(135,206,204));
endnote.getMarkerCharacterFormat().setFontName("Calibri");
endnote.getMarkerCharacterFormat().setFontSize(14);
endnote.getMarkerCharacterFormat().setTextColor(newColor(0,0,139));
//保存文档
doc.saveToFile("result.docx",FileFormat.Docx_);
}
}
}
脚注添加效果:
尾注添加效果:
【示例2】读取Word脚注、尾注
以上文中生成的脚注、尾注为测试文档。
1. 读取Word脚注
importcom.spire.doc.*;
importcom.spire.doc.documents.Paragraph;
importcom.spire.doc.fields.Footnote;
importcom.spire.doc.fields.TextRange;
importjava.util.List;
publicclassExtractFootnoteAndEndnote{
publicstaticvoidmain(String[]args){
//创建Document实例
Documentdoc=newDocument();
doc.loadFromFile("result.docx");
//获取文档中的所有脚注
ListfootNotes=doc.getFootnotes();
//实例化String类型变量
Stringstr="";
//遍历脚注
for(FootnotefootNote:footNotes){
//遍历脚注中的段落
for(intj=0;j
Paragraphparagraph=footNote.getTextBody().getParagraphs().get(j);
//遍历段落中的对象
for(Objectobject:paragraph.getChildObjects()){
//读取文本
if(objectinstanceofTextRange){
TextRangetextRange=(TextRange)object;
str=str+textRange.getText();
}
}
}
}
//输出脚注文本
System.out.println(str);
}
}
脚注读取结果:
2. 读取Word尾注
importcom.spire.doc.*;
importcom.spire.doc.documents.Paragraph;
importcom.spire.doc.fields.Footnote;
importcom.spire.doc.fields.TextRange;
importjava.util.List;
publicclassExtractFootnoteAndEndnote{
publicstaticvoidmain(String[]args){
//创建Document实例
Documentdoc=newDocument();
doc.loadFromFile("result.docx");
//获取所有尾注
ListendNotes=doc.getEndnotes();
//实例化String类型变量
Stringstr="";
//遍历尾注
for(Footnoteendnote:endNotes){
//遍历尾注中的段落
for(intj=0;j
Paragraphparagraph=endnote.getTextBody().getParagraphs().get(j);
//遍历段落中的对象
for(Objectobject:paragraph.getChildObjects()){
//读取文本
if(objectinstanceofTextRange){
TextRangetextRange=(TextRange)object;
str=str+textRange.getText();
}
}
}
}
//输出尾注文本
System.out.println(str);
}
}
尾注读取结果:
【示例3】删除Word脚注、尾注
importcom.spire.doc.*;
importcom.spire.doc.documents.Paragraph;
importcom.spire.doc.fields.Footnote;
importjava.util.List;
publicclassDeleteFootnoteAndEndnote{
publicstaticvoidmain(String[]args){
//加载测试文档
Documentdoc=newDocument();
doc.loadFromFile("result.docx");
//获取第一个section
Sectionsection=doc.getSections().get(0);
//遍历所有段落中的子对象
for(inti=0;i
Paragraphpara=section.getParagraphs().get(i);
for(intj=0;j
DocumentObjectobject=para.getChildObjects().get(j);
//删除脚注尾注
if(objectinstanceofFootnote){
para.getChildObjects().remove(object);
}
}
}
//保存文档
doc.saveToFile("Removefootnote.docx",FileFormat.Docx);
doc.dispose();
}
}
运行程序后,生成的文档将不再有脚注、尾注。
(本文完)