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

Free Support Forum - aspose.com

Table's width issue when AutoFit is FIXED_COLUMN_WIDTHS

Hi,
I have noticed a problem working with tables.

Failure Effect:
The auto fit function for tables does not behave in the same way in different cases.
If there is a table in a table and the auto fit method with the FIXED_COLUMN_WIDTHS behavior is called for this table, the column widths are not displayed correctly (does not behave as expected).
If the same method is called for a table (not nested in other tables), the column widths are displayed correctly.

Steps to Reproduce:
Using Aspose.Words for Java 19.11 (also tested with 18.7).
Using the following code:

String tableString = "<table style=\"width: 100%\">"
   + "<tbody>"
   + "<tr>"
   + "<td style=\"width: 50%\">Test</td>"
   + "<td style=\"width: 50%\"> </td>"
   + "</tr>"
   + "</tbody>"
   + "</table>";

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml(tableString);

Table outerTable = (Table)doc.getChild(NodeType.TABLE, 0, true);
outerTable.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);

builder.moveTo(outerTable.getFirstRow().getLastCell().getFirstParagraph());
builder.insertHtml(tableString);

Table innerTable = (Table)outerTable.getChild(NodeType.TABLE, 0, true);
innerTable.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);

doc.save("result.docx");

The outer table is displayed correctly. The table width is equal to 100% and both column widths are equal to 50%. Even with fixed column widths, the widths are displayed correctly.
The inner table is not displayed correctly after call auto fit with fixed column widths behavior. After the call the table width is not equal to 100%. The table is displayed too small.

Is there an explanation for this?

Regards
Ingo Gutzeit

@gutzeit

You are using FIXED_COLUMN_WIDTHS as AutoFitBehavior. When this option is used, Aspose.Words disables the AutoFit option and removes the preferred with from the table. The widths of the cells remain as they are specified by their Width properties.

In this case, please do not set the width of table’s cell as percentage. You can use the width of cell as shown below. Moreover, the second cell has width 150pt. So, the width of inner table should be according its parent’s cell width.

String tableString = “<table style=“width: 300pt”>”
+ “<tbody>”
+ “<tr>”
+ “<td style=“width: 150pt”>Test</td>”
+ “<td style=“width: 150pt”> </td>”
+ “</tr>”
+ “</tbody>”
+ “</table>”;

We suggest you please read the following article about the behavior of AutoFit method.
How to Apply Different AutoFit Settings to a Table

Hello @tahir.manzoor

I’m sorry, but I don’t quite understand it yet.
The same table is inserted in both cases.
When the table is added the first time (the outerTable), the widths are set as follows (before set to fixed column widths):

preferred width of a cell = 50%
width of a cell = 233.65 [20th of a point] = 4673 [points]

After set fixed column width the widths are set as follows:

preferred width of a cell = 4673
width of a cell = 233.65 [20th of a point] = 4673 [points]

When adding the table, the width seems to have already been set correctly (4673 points). When the auto fit property was set to fixed colum width, the preferred width was then removed and the width was taken as preferred width.
That’s the expected behavior for me.

When the table is added the second time into the first table (the innerTable), the widths are set as follows (before set to fixed column widths):

preferred width of a cell = 50%
width of a cell = 0.0 [20th of a point]

After set fixed column width the widths are set as follows:

preferred width of a cell = 360
width of a cell = 18.0 [20th of a point] = 360 [points]

Why is the width 0.0 when inserting the table? The behavior is not understandable for me at this point. Is that correct?
The behavior after setting the auto fit property to fixed column width is again understandable. The preferred width was removed and the width was taken as preferred width. Because cells cannot be 0 pixels wide, the minimum width is set.

For me there seems to be an error while inserting a table into another table. The width should not be 0.

Regards
Ingo Gutzeit

@gutzeit

Please note that Aspose.Words mimics the behavior of MS Word. If you open the same HTML in MS Word and set AutoFit to ‘fixed column width’, you should get the same width of inner table.

In your case, the width of cells of inner table is incorrect in the output document that is generated by Aspose.Words. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-19685. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-19685) have been fixed in this Aspose.Words for .NET 20.7 update and this Aspose.Words for Java 20.7 update.