I am trying to add a bookmark on every para and then output its co-ordinates, the following code doesnt correctly insert the bookmarks and I get an exception on the line layoutEnumerator.setCurrent(renderObject);
Exception in thread “main” java.lang.NullPointerException: value
at com.aspose.words.LayoutEnumerator.setCurrent(Unknown Source)
at aspose_test.AddBookmarkAndLocate.outputCoords(AddBookmarkAndLocate.java:69)
at aspose_test.AddBookmarkAndLocate.main(AddBookmarkAndLocate.java:58)
The code I have is the following:
public class AddBookmarkAndLocate {
public static LayoutCollector collector;
public static LayoutEnumerator layoutEnumerator;
public static int BOOKMARK_COUNT = 0;
public static void main(String[] args) throws Exception {
License license = new License();
license.setLicense(new java.io.FileInputStream("C:\\jdk\\jdk-11\\lib\\Aspose.Words.Product.Family.lic"));
final Document doc = new Document(System.getProperty("user.dir") + "/rubicon_asset_management.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
collector = new LayoutCollector(doc);
layoutEnumerator = new LayoutEnumerator(doc);
//get all the paras
NodeCollection ndCol = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Object nd: ndCol)
{
Paragraph para = (Paragraph) nd;
builder.moveTo(para);
BookmarkStart start = builder.startBookmark("bm_" + BOOKMARK_COUNT);
BookmarkEnd end = builder.endBookmark("bm_" + BOOKMARK_COUNT);
if (para.getChildNodes().getCount() > 2) {
para.insertBefore(start, para.getFirstChild());
para.insertAfter(start, end);
}
BOOKMARK_COUNT++;
}
BookmarkCollection bookmarksInSectWithColumnBreak = doc.getRange().getBookmarks();
System.out.println("count=" + bookmarksInSectWithColumnBreak.getCount());
for (Bookmark bookmark: bookmarksInSectWithColumnBreak)
{
outputCoords(doc,bookmark);
}
doc.save(System.getProperty("user.dir") + "/rubicon_bookmarktest2.docx");
}
public static void outputCoords(Document doc, Bookmark bookmark) throws Exception
{
Object renderObject = collector.getEntity(bookmark.getBookmarkStart());
layoutEnumerator.setCurrent(renderObject);
System.out.println("-----------------------------------------------");
System.out.println("" + bookmark.getName());
System.out.println("" + bookmark.getText());
System.out.println("X : " + layoutEnumerator.getRectangle().getX());
System.out.println("Y : " + layoutEnumerator.getRectangle().getY());
//System.out.println("Page start = " + collector.getStartPageIndex(bookmark));
//System.out.println("Page end = " + collector.getEndPageIndex(para));
System.out.println("-----------------------------------------------");
}
}