我通过下面的代码获取到了书签集合,但是书签中没有位置等属性
com.aspose.words.Document document = new com.aspose.words.Document("C:\\Users\\Administrator\\Downloads\\docx.docx");
BookmarkCollection bookmarks = document.getRange().getBookmarks();
我通过下面的代码获取到了书签集合,但是书签中没有位置等属性
com.aspose.words.Document document = new com.aspose.words.Document("C:\\Users\\Administrator\\Downloads\\docx.docx");
BookmarkCollection bookmarks = document.getRange().getBookmarks();
@humanhuman, 您可以使用 LayoutCollector 获取放置书签的页码,getNextSibling 和 getPreviousSibling 属性获取前后节点。 例如看下面的代码:
Document doc = new Document("in.docx");
LayoutCollector collector = new LayoutCollector(doc);
for (Bookmark bk : doc.getRange().getBookmarks())
{
System.out.println("Bookmark: " + bk.getName());
System.out.println("Page: " + collector.getStartPageIndex(bk.getBookmarkStart()));
Node previousSibling = bk.getBookmarkStart().getPreviousSibling();
String previousSiblingText = previousSibling != null ? previousSibling.toString(SaveFormat.TEXT).trim() : "";
System.out.println("Preceding text: '" + previousSiblingText + "'");
Node nextSibling = bk.getBookmarkEnd().getNextSibling();
String nextSiblingText = nextSibling != null ? nextSibling.toString(SaveFormat.TEXT).trim() : "";
System.out.println("Following text: '" + nextSiblingText + "'");
System.out.println("---------------------------");
}
in.docx (15.0 KB)
请参阅 Aspose.Words Object Model 了解更多信息:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
您可以使用 DocumentExplorer 来探索文档中的节点: