I have a nice little problem with docx4j. I have a document which I build from a template (replace MailMerge fields and adding text to it) In this document I want to insert two tables. So far, everything works fine, no problem here. I just want to indent my tables, to put a tab in front of it.
I work with a class which represents the docx4j document and work with helper methods like these:
public void buildTable(Map<String, String> data, boolean indent) {
Tbl table = factory.createTbl();
Tr tableRow = null;
for (String key : data.keySet()) {
tableRow = factory.createTr();
addTableCell(tableRow, key);
addTableCell(tableRow, data.get(key));
table.getContent().add(tableRow);
}
if (indent) {
R run = factory.createR();
P para = factory.createP();
R.Tab tab = factory.createRTab();
run.getContent().add(tab);
para.getContent().add(run);
table.setParent(para);
documentPart.addObject(para);
documentPart.addObject(table);
} else {
documentPart.addObject(table);
}
}
I have tried different things, the tab is inserted. But either my table isn't rendered or my it is appended below the tabs.
I tried to add the table to the run object -> No table
to the paragraph object -> No table
setting the paragraph as parent for the table and then add the table -> table below the tabbed paragraph
So... obviously I'm doing something wrong. Do you have a solution for this problem? I saw that the text object has a property like text.setSpace("preserve"); to preserve the space for the text, is there something similar for the table object?
Thanks in advance.
Matthias