Java and javascript get a bookmark location comparison for word documents
- 2020-03-30 03:25:27
- OfStack
1. Javascript: open the activex of Internet explorer and use the following page to see the order and position of bookmarks:
<html>
<head>
<script>
var word;
word = new ActiveXObject("Word.Application");
var range = word.Range;
word.Visible = true;
var path = "D:\xxx\xxx\xx.doc";
word.Documents.Open(path);
for(var i=1;i<=word.ActiveDocument.Bookmarks.count;i++){
document.write(word.ActiveDocument.Bookmarks(i).Name);
document.write(" ");
document.write(word.ActiveDocument.Bookmarks(i).Range.BookmarkID);
document.write("</br>");
}
</script>
</head>
<body>
</body>
</html>
Java: open with poi, this is a.doc file, so use the old poi API, if docx, the principle is the same.
FileInputStream in = new FileInputStream("D:\xxx\xxx\xx.doc");
HWPFDocument doc = new HWPFDocument(in);
Bookmarks bookmarks = doc.getBookmarks();
for(int i=0,j=bookmarks.getBookmarksCount();i<j;i++){
Bookmark bookmark = bookmarks.getBookmark(i);
System.out.println(bookmark.getName());
System.out.println(i);
System.out.println(bookmark.getStart());
}