TableAutoFit Method |
Namespace: Aspose.Words.Tables
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.
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.StartTable(); // Insert a cell builder.InsertCell(); // Use fixed column widths table.AutoFit(AutoFitBehavior.FixedColumnWidths); builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; builder.Write("This is row 1 cell 1"); // 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.Writeln("This is row 2 cell 1"); // Insert a cell builder.InsertCell(); builder.CellFormat.Orientation = TextOrientation.Downward; builder.Writeln("This is row 2 cell 2"); builder.EndRow(); builder.EndTable();
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");