We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to find coordinates of table rows using Java

Hi all,

I am traversing a table and I need to find start and end coordinates of all the rows separately for each row.Flow of my code is I am traversing over each row and creating a bookmark start and end at that row position. Then I am initialising layoutCollector Object and calling getEntity method for fetch each bookmark object. But once I get first entity from layour collector, it makes other bookmarks as null.

Please find the code snippet . I am using Aspose 18.7 with Java 9.

LayoutEnumerator layoutEnumerator = new LayoutEnumerator(document);
DocumentBuilder documentBuilder = new DocumentBuilder(document);

    for (Table table : (Iterable<Table>) tables) {
        Long time = System.currentTimeMillis();
        logger.debug("[START] Prasing per table");
        Map<Integer, Map<Integer, StringBuilder>> tableMatrix = new HashMap<Integer, Map<Integer, StringBuilder>>();

        int rowIndex = 0;
        int rowBookMarkIndex = 0;
        Map<Integer, ParaData> bookMarkMap = new HashMap<>();
        layoutCollector = new LayoutCollector(document);
        for (Row row : table.getRows()) {


            ParaData paraData = new ParaData();
            documentBuilder.moveTo(row.getFirstCell().getFirstParagraph());
            BookmarkStart start = documentBuilder.startBookmark("Bookmark start" + rowBookMarkIndex); //will be at the start of para of first cell
            //documentBuilder.moveTo(row.getLastCell().getLastParagraph());
            BookmarkEnd end = documentBuilder.endBookmark("Bookmark end" + rowBookMarkIndex); //will be at the end of para of last cell
            paraData.setStart(start);
            paraData.setEnd(end);

            bookMarkMap.put(rowBookMarkIndex, paraData);
            rowBookMarkIndex++;
        }


        layoutCollector = new LayoutCollector(document);
        System.out.println("Layout Collector");
        for (Map.Entry<Integer, ParaData> entry : bookMarkMap.entrySet()) {
            //layoutCollector = new LayoutCollector(document);
            Object bstart = layoutCollector.getEntity(entry.getValue().getStart());
            Object bend = layoutCollector.getEntity(entry.getValue().getEnd());
            if(bstart==null || bend==null){ // after first iteration, I am getting null here
                System.out.println("null value");
            }
            entry.getValue().setBstart(bstart);
            entry.getValue().setBend(bend);
        }

@yogesh300890

We suggest you please try the latest version of Aspose.Words for Java 20.4. Hope this helps you.

If you still face problem, please attach your input Word document here for testing. We will investigate the issue on our side and provide you more information.

I tried that as well by taking trial version but still it didnt work.Untitled 3.zip (4.3 KB)

@yogesh300890

In your code, you are creating the bookmark incorrectly. Please use the same string value in DocumentBuilder.StartBookmark and DocumentBuilder.EndBookmark methods as shown below.

for (Table table : (Iterable<Table>) doc.getFirstSection().getBody().getTables()) {
    for (Row row : table.getRows()) {
        documentBuilder.moveTo(row.getFirstCell().getFirstParagraph());
        BookmarkStart start = documentBuilder.startBookmark("Bookmark" + rowBookMarkIndex);
        documentBuilder.moveTo(row.getLastCell().getLastParagraph());
        BookmarkEnd end = documentBuilder.endBookmark("Bookmark" + rowBookMarkIndex);
        rowBookMarkIndex++;
    }
}