本文介绍如何使用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();
}
}