@TanPham We have completed analysis of the issue and concluded it is not a bug, but an expected behavior.
The issue occurs because an incomplete table definition constructed by your code is not interpreted as the you expect.
The behavior is intentional and it matches MS Word behavior if a similar table structure is read from the source document.
Your code constructs a table with 1 cell in row 1, 16 cells in row 2 and 2 cells in row 3. No cell grid spans or horizontal cell merges are specified. As the table is auto-fit, table cell widths are updated when
UpdatePageLayout() is called. A table grid is for a jagged table is calculated and saved in the document model.
Earlier Aspose.Words versions did not support table grid re-calculation for the problematic table. Since 22.2 version, the table is handled by the new logic which matches MS Word for the case when the same document model is read from the source docx. This logic works as designed.
UpdatePageLayout() call, docx writer detects that table grid with column width data is missing. As it needs the data to write
tblGrid element, the code assumes that all cells actually have the currently specified width and updates cell grid spans according to the current cell widths, which makes the cell in row 1 to span 16 columns and so on. This is the behavior expected by you.
However, in that scenario, table content metrics and container column width are ignored completely. The grid produced by the method (and saved to docx) does not match MS Word as MS Word actually squeezes most columns in order to fit into container. The generated output in this scenario has inconsistent data in
tblGrid element and it may cause layout differences when the document is opened in earlier Aspose.Words versions. The cell widths displayed in MS Word will not actually match the widths specified in your code.
For consistent results, your code should specify the table structure completely, including which cell spans which column.
This can be computed from the specified cell widths by calling
The method will reset the table layout to fixed however, so that the cell widths specified by your code are preserved.
In order to get the expected result, table layout and preferred widths should be set after the above method.
Adding the following lines after constructing the table will yield consistent results both with and without
UpdateTableLayout() or intermediate saving to docx:
Table table = builder.EndTable();
// Calculate cell grid spans from the widths specified in the builder.
// Make the table auto-fit, but preserve the specified preferred widths.
table.AllowAutoFit = true;
// Make the table fit to container if possible.
table.PreferredWidth = PreferredWidth.FromPercent(100.0);