这篇文章主要为大家展示了“Java如何添加、读取、删除Word脚注/尾注”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java如何添加、读取、删除Word脚注/尾注”这篇文章吧。
使用工具:Free Spire.Doc for Java (免费版)
Jar文件获取及导入:
方法1:通过官网下载jar文件包,并解压。解压文件后,将lib文件夹中的Spire.Doc.jar文件导入Java程序。
方法2:通过maven仓库导入。
【示例1】添加脚注、尾注
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.Footnote;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class AddFootnoteEndnote {
public static void main(String[] args){
//加载测试文档
Document doc = new Document("test.doc");
//添加脚注1:给指定段落添加脚注
Paragraph para1 = doc.getSections().get(0).getParagraphs().get(2);//获取段落
Footnote footnote1 = para1.appendFootnote(FootnoteType.Footnote);//添加脚注
TextRange text1 = footnote1.getTextBody().addParagraph().appendText("详见附件内容");
text1.getCharacterFormat().setFontName("楷书");//格式化脚注标签及脚注内容
text1.getCharacterFormat().setFontSize(10);
text1.getCharacterFormat().setTextColor(new Color(255, 140, 0));
footnote1.getMarkerCharacterFormat().setFontName("楷书");
footnote1.getMarkerCharacterFormat().setFontSize(14);
footnote1.getMarkerCharacterFormat().setTextColor(new Color(0, 0, 139));
//添加脚注2:给指定文本添加脚注
TextSelection[] selections = doc.findAllString("消除缺陷", false, true);
for (TextSelection selection : selections) {
TextRange range = selection.getAsOneRange();
Paragraph para2 = range.getOwnerParagraph();
Footnote footnote2 = para2.appendFootnote(FootnoteType.Footnote);
int index = para2.getChildObjects().indexOf(range);
para2.getChildObjects().insert(index + 1, footnote2);
TextRange text2 = footnote2.getTextBody().addParagraph().appendText("请查看操作手册");
text2.getCharacterFormat().setFontName("Arial Black");
text2.getCharacterFormat().setFontSize(10);
text2.getCharacterFormat().setTextColor(new Color(153, 50, 204));
footnote2.getMarkerCharacterFormat().setFontName("Calibri");
footnote2.getMarkerCharacterFormat().setFontSize(14);
footnote2.getMarkerCharacterFormat().setTextColor(new Color(0, 0, 139));
//添加尾注:给指定段落添加尾注(给指定文本添加尾注可参考以上添加脚注的代码方法)
Paragraph para3 = doc.getSections().get(0).getParagraphs().get(15);
Footnote endnote= para3.appendFootnote(FootnoteType.Endnote);
TextRange text3 = endnote.getTextBody().addParagraph().appendText("引用自刘玲《操作手册》");
text3.getCharacterFormat().setFontName("Arial Black");
text3.getCharacterFormat().setFontSize(10);
text3.getCharacterFormat().setTextColor(new Color(135, 206, 204));
endnote.getMarkerCharacterFormat().setFontName("Calibri");
endnote.getMarkerCharacterFormat().setFontSize(14);
endnote.getMarkerCharacterFormat().setTextColor(new Color(0, 0, 139));
//保存文档
doc.saveToFile("result.docx",FileFormat.Docx_2010);
}
}
}
脚注添加效果:
尾注添加效果:
【示例2】读取Word脚注、尾注
以上文中生成的脚注、尾注为测试文档。
1. 读取Word脚注
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Footnote;
import com.spire.doc.fields.TextRange;
import java.util.List;
public class ExtractFootnoteAndEndnote {
public static void main(String[] args) {
//创建Document实例
Document doc = new Document();
doc.loadFromFile("result.docx");
//获取文档中的所有脚注
List<Footnote> footNotes = doc.getFootnotes();
//实例化String类型变量
String str = "";
//遍历脚注
for (Footnote footNote :footNotes) {
//遍历脚注中的段落
for (int j = 0; j < footNote.getTextBody().getParagraphs().getCount(); j++) {
Paragraph paragraph = footNote.getTextBody().getParagraphs().get(j);
//遍历段落中的对象
for(Object object : paragraph.getChildObjects()){
//读取文本
if (object instanceof TextRange) {
TextRange textRange = (TextRange) object;
str = str + textRange.getText();
}
}
}
}
//输出脚注文本
System.out.println(str);
}
}
脚注读取结果:
2. 读取Word尾注
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Footnote;
import com.spire.doc.fields.TextRange;
import java.util.List;
public class ExtractFootnoteAndEndnote {
public static void main(String[] args) {
//创建Document实例
Document doc = new Document();
doc.loadFromFile("result.docx");
//获取所有尾注
List<Footnote> endNotes = doc.getEndnotes();
//实例化String类型变量
String str = "";
//遍历尾注
for (Footnote endnote :endNotes) {
//遍历尾注中的段落
for (int j = 0; j < endnote.getTextBody().getParagraphs().getCount(); j++) {
Paragraph paragraph = endnote.getTextBody().getParagraphs().get(j);
//遍历段落中的对象
for(Object object : paragraph.getChildObjects()){
//读取文本
if (object instanceof TextRange) {
TextRange textRange = (TextRange) object;
str = str + textRange.getText();
}
}
}
}
//输出尾注文本
System.out.println(str);
}
}
尾注读取结果:
【示例3】删除Word脚注、尾注
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Footnote;
import java.util.List;
public class DeleteFootnoteAndEndnote {
public static void main(String[] args) {
//加载测试文档
Document doc = new Document();
doc.loadFromFile("result.docx");
//获取第一个section
Section section = doc.getSections().get(0);
//遍历所有段落中的子对象
for(int i =0; i<section.getParagraphs().getCount();i++){
Paragraph para = section.getParagraphs().get(i);
for(int j = 0; j<para.getChildObjects().getCount();j++){
DocumentObject object = para.getChildObjects().get(j);
//删除脚注尾注
if(object instanceof Footnote){
para.getChildObjects().remove(object);
}
}
}
//保存文档
doc.saveToFile("Removefootnote.docx", FileFormat.Docx);
doc.dispose();
}
}
以上是“Java如何添加、读取、删除Word脚注/尾注”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:http://blog.itpub.net/31499788/viewspace-2664814/