How to Apply Different AutoFit Settings to a Table

When creating a table using a visual agent such as Microsoft Word, you will often find yourself using one of the AutoFit options to automatically size the table to the desired width. For instance you can use the AutoFit to Window option to fit the table to the width of the page and the AutoFit to Contents option to allow each cell to grow or shrink to accommodate its contents.

By default Aspose.Words inserts a new table using “AutoFit to Window”. The table will size to the available width on the page. To change the sizing behavior on such a table or an existing table you can call Table.autoFit(int) method. This method accepts an AutoFitBehavior enumeration which defines what type of auto fitting is applied to the table.

As in Microsoft Word, an autofit method is actually a shortcut which applies different properties to the table all at once. These properties are actually what give the table the observed behavior. We will discuss these properties for each autofit option. We will use the following table and apply the different auto fit settings as a demonstration:

apply-different-autofit-settings-to-a-table-aspose-words-java

AutoFitting a Table to Window

The following code example autofits a table to fit the page width.

The result after this operation is a table which is lengthened to fit the width of the page is shown below.

autofit-table-aspose-words-java

AutoFitting a Table to Contents

The following code example autofits a table in the document to its contents.

The result of the above code causes the table to automatically resize each column to its contents.

resize-column-autofit-settings-aspose-words-java

When a table is an auto fitted to contents the following steps are actually undertaken behind the scenes:

  1. The Table.AllowAutoFit property is enabled to automatically resize each cell to accommodate its contents.
  2. The table-wide preferred width under Table.PreferredWidth is removed.
  3. The CellFormat.PreferredWidth is removed for every cell in the table.
  4. The column widths are recalculated for the current content in the table. The end result is a table whose column widths and the entire table width change automatically to best accommodate the content as the user edits text in MS Word.

Disabling AutoFitting on a Table and Use Fixed Column Widths

The following code example disables autofitting and enables fixed widths for the specified table.

The result of disabling autofit and using fixed widths for the column sizes is shown below.

disable-autofit-settings-to-a-table-aspose-words-java

When a table has auto fit disabled and fixed column widths used instead the following steps are taken:

  1. The Table.AllowAutoFit property is disabled so columns do not grow or shrink to their contents.
  2. The table-wide preferred width is removed from the Table.PreferredWidth.
  3. The CellFormat.PreferredWidth is removed from all cells in the table. The end result is a table whose column widths are defined using the CellFormat.getWidth() property and whose columns do not automatically resize when the user enter texts or the page size is modified.