Using Words with variable length tables

I have been tasked with doing a proof of concept to see if Aspose Words for Java is right for our application. We are starting with templates created using Word documents with bookmarks. Part of the proof of concept is to show support for a table with a variable number of rows. What would be the best approach for solving this? I see that the dom of the document is accessible. Can I get the parent node of a bookmark? Any suggestions welcome.

@riwright Sure you can get parent node of the bookmark, but you should note that bookmark in MS Word document consist of two nodes - start and end, which can have different parent nodes. Please see the following code:

Document doc = new Document("C:\\Temp\\in.docx");

Bookmark bk = doc.getRange().getBookmarks().get("my_bookmark");
        
// get parent of the bookmark start.
CompositeNode bkStartParent = bk.getBookmarkStart().getParentNode();
// get parent of the bookmark end.
CompositeNode bkEndParent = bk.getBookmarkEnd().getParentNode();
// Get bookmark start ancestor of the specific type.
Cell cell = (Cell)bk.getBookmarkStart().getAncestor(NodeType.CELL);

Also, for building tables there is easier approach then cloning rows and filling them manually. You can consider using Mail Merge with Regions or LINQ Reporting Engine to achieve this.

Thanks, this will help me get started.

1 Like