Hello,
After upgrading from aspose-words-16.8.0 to aspose-words-21.7.0, the following code snippet produces tables with unexpected widths.
public class Test {
public static void main(String[] args) throws Exception {
Document doc = new Document();
Table table1 = buildTable1(doc);
Table table2 = buildTable2(doc);
doc.getFirstSection().getBody().appendChild(table1);
doc.getFirstSection().getBody().appendChild(table2);
doc.save("D:\\test.docx", SaveFormat.DOCX);
}
private static Table buildTable1(Document doc) throws Exception {
Paragraph p1 = new Paragraph(doc);
p1.getRuns().add(new Run(doc, "a"));
Paragraph p2 = new Paragraph(doc);
p2.getRuns().add(new Run(doc, "b"));
Table table = new Table(doc);
Row row = new Row(doc);
Cell cell1 = new Cell(doc);
cell1.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(240));
cell1.appendChild(p1);
Cell cell2 = new Cell(doc);
cell2.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(240));
cell2.appendChild(p2);
row.appendChild(cell1);
row.appendChild(cell2);
table.appendChild(row);
table.setPreferredWidth(PreferredWidth.fromPoints(480));
table.setAllowAutoFit(false);
return table;
}
private static Table buildTable2(Document doc) throws Exception {
Paragraph p = new Paragraph(doc);
p.getRuns().add(new Run(doc, "Hello World"));
Table table = new Table(doc);
Row row = new Row(doc);
Cell cell = new Cell(doc);
cell.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(480));
cell.appendChild(p);
row.appendChild(cell);
table.appendChild(row);
table.setPreferredWidth(PreferredWidth.fromPoints(480));
table.setAllowAutoFit(false);
return table;
}
}
-
What I expect (aspose-words-16.8.0):
image.png (830 Bytes) -
What I get after upgrade (aspose-words-21.7.0):
image.png (850 Bytes)
I tried to replace Cell.getCellFormat().setPreferredWidth(PreferredWidth)
by Cell.getCellFormat().setWidth(double)
and the layout is correct but I’m afraid it involves other regressions and the documentation says it’s better to specify a preferred width on a cell.
Could you please tell me if the way to specify table widths has changed recently or if this is a regression to be fixed in a future release?
Thank you for your help.
Best regards,
Brice