How to get coordinates of table/row/cell using Aspose Java API

I was working on get coordinates of word elements. For paragraphs and lines, I find layoutCollector and layoutEnumerator is quite powerful. I can get x,y coordinates via getRectangle() method. However, I find this method only works on span entities and I can’t get row/cell/table’s coordinates by calling this function. I go through some related questions, and I find that inserting bookmarks around the table/row/cell might be a solution. Can you provide some sample code in java to insert the bookmarks around the row or cell to get the X,Y coordinates? Or if there’re more easier ways to do that, please let me know.

@adayao

You can use LayoutEnumerator.MoveParent method (LayoutEntityType) as shown below and get the position of Row and Cell.

Document doc = new Document(MyDir + "input.docx");
LayoutCollector layoutCollector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

Table table = doc.getFirstSection().getBody().getTables().get(0);
enumerator.setCurrent(layoutCollector.getEntity(table.getFirstRow().getFirstCell().getFirstParagraph()));

System.out.println(enumerator.getRectangle());

enumerator.moveParent(LayoutEntityType.CELL);
System.out.println(enumerator.getRectangle());

enumerator.moveParent(LayoutEntityType.ROW);
System.out.println(enumerator.getRectangle());

We suggest you please check the code example shared here:

If you want to insert bookmark into table cell, you can use DocumentBuilder.MoveToCell method to move the cursor to the table cell and insert bookmark using DocumentBuilder.StartBookmark and DocumentBuilder.EndBookmark methods as suggested in following articles.

It’s helpful. Thank you.