Currently, I am creating a Word document that has a table template in it. I use the ".deepClone()" function to make copies of the table and replace the template with different data to create several tables, the problem I’m having is the spacing between tables, they are either generated on top of each other or too far away when I use a paragraph with setBeforeSpacing() and setAfterSpacing() between the tables.
I tried generating temporary paragraphs with bookmarks beforehand and then putting the tables on them, but the tables are overlapping and I can’t find a way to get the previous table height to calculate how many empty paragraphs (spacing) I need to insert in order for the tables to not overlap.
My code is something like this:
- Code: Select all
TextRange range = txtTemplateTable.getAsOneRange();
//Get table template
Table templateTable = (Table) range.getOwner().getOwner().getOwner().getOwner();
generateParagraphsForEachObject(objects);
for (Object o : objects) {
Table table = templateTable.deepClone();
Data data = getData(o);
writeDataToDocument(table, data);
// find the generated paragraph
TextSelection txt = doc.findString("GenerateTableId"+o.getId(), true, true);
Paragraph para = txt.getAsOneRange(true).getOwnerParagraph();
Section section = (Section) para.getOwner().getOwner();
//Insert Bookmark in paragraph
para.appendBookmarkStart("Bookmark"+o.getId());
para.appendBookmarkEnd("Bookmark"+o.getId());
BookmarksNavigator bn = new BookmarksNavigator(doc);
bn.moveToBookmark("Bookmark"+o.getId(), true, true);
// insert new generated table
bn.insertTable(table);
// delete the pargraph
int ix = section.getParagraphs().indexOf(para);
section.getParagraphs().removeAt(ix);
}
// remove template table
Section section = (Section) tableOriginal.getOwner().getOwner();
section.getTables().remove(tableOriginal);