This technical tip explains how developers can build a table with applying Table Style & expand styles to rows & cells of the table. A table style defines a set of formatting that can be easily applied to a table. Formatting such as borders, shading, alignment and font can be set in a table style and applied to many tables for a consistent appearance. Aspose.Words supports applying a table style to a table and also reading properties of any table style. Table styles are preserved during loading and saving in the following ways:
- Table styles in DOCX and WordML formats are preserved when loading and saving to these formats.
- Table styles are preserved when loading and saving in the DOC format (but not to any other format).
- When exporting to other formats, rendering or printing, table styles are expanded to direct formatting on the table so all formatting is preserved.
In Aspose.Words you can apply a table style by using any of the Table.Style, Table.StyleIdentifier and Table.StyleName properties.
Code shows how to build a new table with a table style applied
[C#]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
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);
// 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.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.Writeln("Apples");
builder.InsertCell();
builder.Writeln("20");
builder.EndRow();
builder.InsertCell();
builder.Writeln("Bananas");
builder.InsertCell();
builder.Writeln("40");
builder.EndRow();
builder.Writeln("Bananas");
builder.InsertCell();
builder.Writeln("40");
builder.EndRow();
builder.InsertCell();
builder.Writeln("Carrots");
builder.InsertCell();
builder.Writeln("50");
builder.EndRow();
builder.Writeln("Carrots");
builder.InsertCell();
builder.Writeln("50");
builder.EndRow();
doc.Save(MyDir + "DocumentBuilder.SetTableStyle Out.docx");
[VB.NET]
Dim doc As New Document()
Dim builder As New DocumentBuilder(doc)
Dim builder As New DocumentBuilder(doc)
Dim table As 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 Or TableStyleOptions.RowBands Or TableStyleOptions.FirstRow
table.AutoFit(AutoFitBehavior.AutoFitToContents)
' 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 Or TableStyleOptions.RowBands Or 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.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.Writeln("Apples")
builder.InsertCell()
builder.Writeln("20")
builder.EndRow()
builder.InsertCell()
builder.Writeln("Bananas")
builder.InsertCell()
builder.Writeln("40")
builder.EndRow()
builder.Writeln("Bananas")
builder.InsertCell()
builder.Writeln("40")
builder.EndRow()
builder.InsertCell()
builder.Writeln("Carrots")
builder.InsertCell()
builder.Writeln("50")
builder.EndRow()
builder.Writeln("Carrots")
builder.InsertCell()
builder.Writeln("50")
builder.EndRow()
doc.Save(MyDir & "DocumentBuilder.SetTableStyle Out.docx")
Code for how to expand the formatting from styles onto the rows and cells of the table as direct formatting
[C#]
Document doc = new Document(MyDir + "Table.TableStyle.docx");
// Get the first cell of the first table in the document.
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
Cell firstCell = table.FirstRow.FirstCell;
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
Cell firstCell = table.FirstRow.FirstCell;
// First print the color of the cell shading. This should be empty as the current shading
// is stored in the table style.
Color cellShadingBefore = firstCell.CellFormat.Shading.BackgroundPatternColor;
Console.WriteLine("Cell shading before style expansion: " + cellShadingBefore.ToString());
// is stored in the table style.
Color cellShadingBefore = firstCell.CellFormat.Shading.BackgroundPatternColor;
Console.WriteLine("Cell shading before style expansion: " + cellShadingBefore.ToString());
// Expand table style formatting to direct formatting.
doc.ExpandTableStylesToDirectFormatting();
doc.ExpandTableStylesToDirectFormatting();
// Now print the cell shading after expanding table styles. A blue background pattern color
// should have been applied from the table style.
Color cellShadingAfter = firstCell.CellFormat.Shading.BackgroundPatternColor;
Console.WriteLine("Cell shading after style expansion: " + cellShadingAfter.ToString());
// should have been applied from the table style.
Color cellShadingAfter = firstCell.CellFormat.Shading.BackgroundPatternColor;
Console.WriteLine("Cell shading after style expansion: " + cellShadingAfter.ToString());
[VB.NET]
Dim doc As New Document(MyDir & "Table.TableStyle.docx")
' Get the first cell of the first table in the document.
Dim table As Table = CType(doc.GetChild(NodeType.Table, 0, True), Table)
Dim firstCell As Cell = table.FirstRow.FirstCell
Dim table As Table = CType(doc.GetChild(NodeType.Table, 0, True), Table)
Dim firstCell As Cell = table.FirstRow.FirstCell
' First print the color of the cell shading. This should be empty as the current shading
' is stored in the table style.
Dim cellShadingBefore As Color = firstCell.CellFormat.Shading.BackgroundPatternColor
Console.WriteLine("Cell shading before style expansion: " & cellShadingBefore.ToString())
' is stored in the table style.
Dim cellShadingBefore As Color = firstCell.CellFormat.Shading.BackgroundPatternColor
Console.WriteLine("Cell shading before style expansion: " & cellShadingBefore.ToString())
' Expand table style formatting to direct formatting.
doc.ExpandTableStylesToDirectFormatting()
doc.ExpandTableStylesToDirectFormatting()
' Now print the cell shading after expanding table styles. A blue background pattern color
' should have been applied from the table style.
Dim cellShadingAfter As Color = firstCell.CellFormat.Shading.BackgroundPatternColor
Console.WriteLine("Cell shading after style expansion: " & cellShadingAfter.ToString())
' should have been applied from the table style.
Dim cellShadingAfter As Color = firstCell.CellFormat.Shading.BackgroundPatternColor
More about Aspose.Words for .NET
Aspose.Words is a word processing component that enables Java & .NET applications to read, write and modify Word documents without using Microsoft Word. Other useful features include document creation, content and formatting manipulation, mail merge abilities, reporting features, TOC updated/rebuilt, Embedded OOXML, Footnotes rendering and support of DOCX, DOC, WordprocessingML, HTML, XHTML, TXT and PDF formats (requires Aspose.Pdf). It supports both 32-bit and 64-bit operating systems. You can even use Aspose.Words to build applications with Mono.
- Visit Homepage of C# .NET Component
No comments:
Post a Comment