We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Wrong table widths after upgrading to aspose-words-21.7.0

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

@bpoussin

We are investigating your issue and will get back to you soon.

@bpoussin

We have tested the scenario and managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-22716. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Thank you Tahir,

I tried several versions of Aspose Words and it seems this issue was introduced in aspose-words-16.12.0.
I checked the release notes and I assume it is related to the improvement:

  • Implemented combining adjacent tables on document loading

Luckily I found a workaround. It’s working fine if I insert an insignificant table between the 2 tables:

doc.getFirstSection().getBody().appendChild(table1);
doc.getFirstSection().getBody().appendChild(new Table(doc)); // Insignificant table required here
doc.getFirstSection().getBody().appendChild(table2);

Regards,
Brice

@bpoussin

It is nice to hear from you that you have found the workaround of this issue. We will inform you via this forum thread once there is an update available on it.