I use a .odt template to create multiple pdf-documents with a Java application.
I want to create a table in this document using the properties of an existing table in the template. So in the template there's an empty table and in code I 'copy' this table to create the table filled with data and remove the empty table. The next time I use the template I use the filled table to create a new one and delete the previously filled table and so one, and so one. The template is added to this post as an attachment.
When I remove the table from the template there is still an empty line in the template. So the next time I use the template there's a new empty line added and so one leading to empty pages in my pdf-documents.
This is the code where I create the table:
Code: Select all
public void createTable(String srcTableName, List<List<String>> data) throws Exception {
// Get source table and styles
XTextTable srcTable = getTable(srcTableName);
XCell srcHeaderCell = srcTable.getCellByName("A1");
XCell srcContentCell = srcTable.getCellByName("A2");
// Create target table
XMultiServiceFactory mxDocFactory = UnoRuntime.queryInterface(
XMultiServiceFactory.class, textDoc);
XTextTable targetTable = UnoRuntime.queryInterface(XTextTable.class,
mxDocFactory.createInstance("com.sun.star.text.TextTable"));
targetTable.initialize(data.size(), data.get(0).size());
// get properties
XPropertySet srcProps = UnoRuntime.queryInterface(XPropertySet.class, srcTable);
XPropertySet targetProps = UnoRuntime.queryInterface(XPropertySet.class, targetTable);
// Move cursor to table
cursor.gotoRange(getAnchor(srcTableName), false);
// Insert table
textDoc.getText().insertTextContent(cursor, targetTable, false);
// Insert table data
int rowId = 1;
char columnId;
for (List<String> row : data) {
columnId = 'A';
for (String value : row) {
String cellName = columnId + "" + rowId;
insertIntoCell(cellName, value, targetTable);
columnId++;
// Copy properties
XCell cellTarget = targetTable.getCellByName(cellName);
copyProps(rowId == 1 ? srcHeaderCell : srcContentCell, cellTarget);
copyProps(UnoRuntime.queryInterface(XText.class, rowId == 1 ? srcHeaderCell : srcContentCell),
UnoRuntime.queryInterface(XText.class, cellTarget));
}
rowId++;
}
// Copy table properties
copyProps(srcTable, targetTable);
// Optimize table columns widths
optimizeTableColums(targetTable);
targetProps.setPropertyValue("Width", srcProps.getPropertyValue("Width"));
srcTable.dispose();
XNamed xNamedTable = UnoRuntime.queryInterface(XNamed.class, targetTable);
xNamedTable.setName(srcTableName);
}