I want to set the column width in a table and fix this column width, so that the text automatically breaks.
My document contains several tables. And I want to set the column-width / fixing for every table.
How can I FIX the table width to a certain value and the column widths of this table?
For example:
Table1 (width =14cm fixed)
Column1=5cm (fixed); Column2=4cm (fixed); Column3=3cm (fixed); Column4=2cm (fixed)
Table2 (width=20cm fixed)
Column1=2cm (not fixed); Column2=4cm (not fixed); Column3=6cm (not fixed); Column4=8cm (not fixed)
etc.
I tried:
- Code: Select all
TblPr tableProps = new TblPr();
CTTblLayoutType tblLayoutType = new CTTblLayoutType();
STTblLayoutType stTblLayoutType = STTblLayoutType.FIXED;
tblLayoutType.setType(stTblLayoutType);
tableProps.setTblLayout(tblLayoutType);
table.setTblPr(tableProps);
It fixes as I want, but then I can't change the column width. I tried
- Code: Select all
private static void addTableCellWithWidth(Tr row, String content, int width){
Tc tableCell = factory.createTc();
tableCell.getContent().add(
wordMLPackageTemplate.getMainDocumentPart().createParagraphOfText(
content));
if (width > 0) {
setCellWidth(tableCell, width);
}
row.getContent().add(tableCell);
}
- Code: Select all
private static void setCellWidth(Tc tableCell, int width) {
TcPr tableCellProperties = new TcPr();
TblWidth tableWidth = new TblWidth();
tableWidth.setW(BigInteger.valueOf(width));
tableCellProperties.setTcW(tableWidth);
tableCell.setTcPr(tableCellProperties);
}
but the column width doesn't change.