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

Free Support Forum - aspose.com

How to work with Table preferred width and Allow AutoFit using Java

Hey folks,

this is a follow-up from How to Apply Different AutoFit Settings to a Table using Java

Setting a table’s preferred width to 100% results in a “autofit to window” effect close to Microsoft Word’s one. The problem is that this setting is ignored (the table width remains unchanged) if table.setAllowAutoFit( false) is also used.

Why is this happening and how can it be fixed?


Hi Dragos,

Thanks for your query. When autofit to window is applied to a table the following operations are actually being performed behind the scenes:

  1. The Table.AllowAutoFit property is enabled to automatically resize columns to the available content.
  2. A Table.PreferredWidth value of 100% is applied.
  3. The CellFormat.PreferredWidth is removed from all cells in the table. Note this is a little bit different to how Microsoft Word performs this step. In Microsoft Word the preferred width of each cell is set to suitable values based off their current size and content. Aspose.Words does not update preferred width so instead they are just cleared.
  4. The column widths are recalculated for the current content of the table.
    The end result is a table that occupies all available width. The widths of the columns in the table change automatically as the user edits text in MS Word.
Please read following documentation links for your kind reference.


Hey Tahir,

thanks for the explanation but it doesn’t seem to be related to my question. My problem is with regards to the setAllowAutoFit method and how it behaves when used in pair with the setPreferredWidth method :

Scenario 1 - works, the table occupies the full page width
a. table.setPreferredWidth( 100);

b. table.setAllowAutoFit( true);

Scenario 2 - doesn’t work, the table width does not increase

a. table.setPreferredWidth( 100);

b. table.setAllowAutoFit( false);

Note that on step (b) I’m using the table’s setAllowAutoFit method not the autoFit method you seem to be referring to.


Hi Dragos,

Thanks for your inquiry.

I managed to reproduce the issue on my side. I have linked your request to the appropriate issue. We will inform you as soon as it’s resolved.

In the mean time just like the other bugs you can work around this issue by calling Document.UpdateTableLayout().


Thanks Adam. updateTableLayout is not an option , see my other threads and the replies of your colleagues that explicitly ask that updateTableLayout is not used as it causes a number of different issues.



Thank you for additional information. We will keep you informed regarding status of the issue and immediately let you know once it is resolved.

Best regards,

Hi Dragos,

I’m a developer working on the issue attached to this thread.

I confirm that this is a problem with our table layout algorithm. Setting preferred table width to 100% should produce a table that spans the whole page width.

Unfortunately, I cannot promise you a quick fix. Table layout is a very complex issue. We will certainly fix it eventually, but I’m afraid it will not happen in the nearest releases.

If you need to get a table spanning the whole page and with equal column widths, you could try this piece of code:

Table table1 = builder.StartTable();

// added by DM
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(50d);

builder.Writeln("AllowAutoFit True + PreferredWidth 50%");

// changed by DM
// table1.AllowAutoFit = false;
table1.AllowAutoFit = true;

table1.PreferredWidth = PreferredWidth.FromPercent(100);

It is basically your sample, but I set AllowAutoFit to true and set column widths in percents explicitly.

Hope this helps.

Thanks && Best Regards,