TableAutoFit Method

Resizes the table and cells according to the specified auto fit behavior.

Namespace:  Aspose.Words.Tables
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.7.0

Syntax

public void AutoFit(
	AutoFitBehavior behavior
)

Parameters

behavior
Type: Aspose.Words.TablesAutoFitBehavior
Specifies how to auto fit the table.

Remarks

This method mimics the commands available in the Auto Fit menu for a table in Microsoft Word. The commands available are "Auto Fit to Contents", "Auto Fit to Window" and "Fixed Column Width". In Microsoft Word these commands set relevant table properties and then update the table layout and Aspose.Words does the same for you.

Examples

Shows how to build a formatted table that contains 2 rows and 2 columns.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.StartTable();

// Insert a cell
builder.InsertCell();
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.Write("This is row 1 cell 1");

// Use fixed column widths
table.AutoFit(AutoFitBehavior.FixedColumnWidths);

// Insert a cell
builder.InsertCell();
builder.Write("This is row 1 cell 2");
builder.EndRow();

// Insert a cell
builder.InsertCell();

// Apply new row formatting
builder.RowFormat.Height = 100;
builder.RowFormat.HeightRule = HeightRule.Exactly;

builder.CellFormat.Orientation = TextOrientation.Upward;
builder.Write("This is row 2 cell 1");

// Insert a cell
builder.InsertCell();
builder.CellFormat.Orientation = TextOrientation.Downward;
builder.Write("This is row 2 cell 2");

builder.EndRow();
builder.EndTable();
Shows how to build a new table with a table style applied.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();

// We must insert at least one row first before setting any table formatting
builder.InsertCell();

// Set the table style used based of the unique style identifier
// Note that not all table styles are available when saving as .doc format
table.StyleIdentifier = StyleIdentifier.MediumShading1Accent1;

// Apply which features should be formatted by the style
table.StyleOptions =
    TableStyleOptions.FirstColumn | TableStyleOptions.RowBands | TableStyleOptions.FirstRow;
table.AutoFit(AutoFitBehavior.AutoFitToContents);

// Continue with building the table as normal
builder.Writeln("Item");
builder.CellFormat.RightPadding = 40;
builder.InsertCell();
builder.Writeln("Quantity (kg)");
builder.EndRow();

builder.InsertCell();
builder.Writeln("Apples");
builder.InsertCell();
builder.Writeln("20");
builder.EndRow();

builder.InsertCell();
builder.Writeln("Bananas");
builder.InsertCell();
builder.Writeln("40");
builder.EndRow();

builder.InsertCell();
builder.Writeln("Carrots");
builder.InsertCell();
builder.Writeln("50");
builder.EndRow();

doc.Save(ArtifactsDir + "DocumentBuilder.InsertTableWithStyle.docx");

See Also