本文介绍如何使用spire.doc for java获取word文档中书签包含的文本。
import com.spire.doc.document;
import com.spire.doc.documents.bookmarksnavigator;
import com.spire.doc.documents.paragraph;
import com.spire.doc.documents.textbodypart;
import com.spire.doc.fields.textrange;
import java.io.filenotfoundexception;
import java.io.printwriter;
public class getbookmarktext {
public static void main(string[] args) throws filenotfoundexception {
//创建document对象
document doc = new document();
//加载word文档
doc.loadfromfile("c:\\users\\administrator\\desktop\\sample.docx");
//获取指定书签
bookmarksnavigator navigator = new bookmarksnavigator(doc);
navigator.movetobookmark("mybookmark");
//获取书签内容
textbodypart textbodypart = navigator.getbookmarkcontent();
//创建string变量
string text = "";
//遍历书签内容的项目
for (object item : textbodypart.getbodyitems()) {
//判断项目是否为段落
if (item instanceof paragraph) {
paragraph paragraph = (paragraph) item;
//遍历段落中的子对象
for (object childobj : paragraph.getchildobjects()) {
//判断子对象是否为textrange
if (childobj instanceof textrange) {
//获取textrange中的文本
textrange textrange = (textrange) childobj;
text = text textrange.gettext();
}
}
}
}
//将获取到的文本写入txt文件
printwriter printwriter = new printwriter("output/bookmarktext.txt");
printwriter.println(text);
printwriter.close();
}
}