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(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("-----------------------------------------------");   
    
}

}

@chrisbeecham2,

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) {
            builder.moveTo(para);

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

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

            counter++;
        }
    }
}

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

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

Yes this works thanks