Trying to add bookmark on every para and get location

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(
at aspose_test.AddBookmarkAndLocate.main(

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();
    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;
        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);   
    BookmarkCollection bookmarksInSectWithColumnBreak = doc.getRange().getBookmarks();
    System.out.println("count=" + bookmarksInSectWithColumnBreak.getCount());
    for (Bookmark bookmark: bookmarksInSectWithColumnBreak)
    }"user.dir") + "/rubicon_bookmarktest2.docx");

public static void outputCoords(Document doc, Bookmark bookmark) throws Exception
    Object renderObject = collector.getEntity(bookmark.getBookmarkStart());

    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));



Unfortunately, your question isn’t clear enough therefore we request you to please elaborate your inquiry further by providing complete details of your use-case. Do you want to insert Bookmarks at the very beginnings of Paragraphs and then calculate (x, y) coordinates of those Bookmarks? These details will help us to understand your scenario, and we will be in a better position to address your concerns accordingly.

Please also check if the following Java code is acceptable for you?

Document doc = new Document("C:\\temp\\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

int counter = 0;
for (Section section : doc.getSections()) {
    for (Paragraph para : (Iterable<Paragraph>) section.getBody().getChildNodes(NodeType.PARAGRAPH, true)) {
        if (para.getChildNodes().getCount() > 2) {

            BookmarkStart start = builder.startBookmark("bm_" + counter);
            BookmarkEnd end = builder.endBookmark("bm_" + counter);

            para.insertBefore(start, para.getFirstChild());
            para.insertAfter(end, start);


LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

for (Bookmark bookmark : doc.getRange().getBookmarks()) {
    System.out.println("(" + enumerator.getRectangle().getX() + ", " + enumerator.getRectangle().getY() + " )");

Yes this works thanks