Last active
August 25, 2023 10:21
-
-
Save aspose-words-gists/93b92a7e6f2f4bbfd9177dd7fcecbd8c to your computer and use it in GitHub Desktop.
Aspose.Words for .NET. Advanced table formatting C#.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
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 on the unique style identifier. | |
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); | |
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 + "WorkingWithTableStylesAndFormatting.BuildTableWithStyle.docx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document srcDoc = new Document(); | |
// Create a custom style for the source document. | |
Style srcStyle = srcDoc.Styles.Add(StyleType.Paragraph, "MyStyle"); | |
srcStyle.Font.Color = Color.Red; | |
// Import the source document's custom style into the destination document. | |
Document dstDoc = new Document(); | |
Style newStyle = dstDoc.Styles.AddCopy(srcStyle); | |
// The imported style has an appearance identical to its source style. | |
Assert.AreEqual("MyStyle", newStyle.Name); | |
Assert.AreEqual(Color.Red.ToArgb(), newStyle.Font.Color.ToArgb()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.StartTable(); | |
builder.InsertCell(); | |
builder.Write("Name"); | |
builder.InsertCell(); | |
builder.Write("Value"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.InsertCell(); | |
builder.EndTable(); | |
TableStyle tableStyle = (TableStyle) doc.Styles.Add(StyleType.Table, "MyTableStyle1"); | |
tableStyle.Borders.LineStyle = LineStyle.Double; | |
tableStyle.Borders.LineWidth = 1; | |
tableStyle.LeftPadding = 18; | |
tableStyle.RightPadding = 18; | |
tableStyle.TopPadding = 12; | |
tableStyle.BottomPadding = 12; | |
table.Style = tableStyle; | |
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.CreateTableStyle.docx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.StartTable(); | |
builder.InsertCell(); | |
builder.Write("Name"); | |
builder.InsertCell(); | |
builder.Write("Value"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.InsertCell(); | |
builder.EndTable(); | |
TableStyle tableStyle = (TableStyle) doc.Styles.Add(StyleType.Table, "MyTableStyle1"); | |
tableStyle.ConditionalStyles.FirstRow.Shading.BackgroundPatternColor = Color.GreenYellow; | |
tableStyle.ConditionalStyles.FirstRow.Shading.Texture = TextureIndex.TextureNone; | |
table.Style = tableStyle; | |
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.DefineConditionalFormatting.docx"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Tables.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; | |
// 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); | |
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment