//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;
namespace DsWordWeb.Demos
{
// This sample demonstrates various conditional table formatting options.
public class ConditionalTableStyles
{
public GcWordDocument CreateDocx()
{
GcWordDocument doc = new GcWordDocument();
// Table dimensions:
var rows = 20;
var cols = 7;
doc.Body.Paragraphs.Add(
$"A {cols} columns by {rows} rows table, with conditional styles applied to most elements:");
// Add an empty table:
var t = doc.Body.Tables.Add(0, 0);
// Add some rows and cells to it:
var cells = new List<string>(cols);
for (int col = 0; col < cols; ++col)
cells.Add(string.Empty);
for (int row = 0; row < rows; ++row)
{
for (int col = 0; col < cols; ++col)
cells[col] = $"Row {row + 1}, col {col + 1}";
t.Rows.Add(cells.ToArray());
}
// Create a table style on which we will define conditional formatting:
var ts1 = doc.Styles.Add("Table Style 1", StyleType.Table);
// And assign the style to the table:
t.Style = ts1;
// Set up simple borders:
foreach (var border in ts1.Table.Borders)
{
border.LineStyle = LineStyle.Single;
border.LineWidth = 0.5f;
border.Color.RGB = Color.DarkGray;
}
// Add some padding:
ts1.Table.Padding.All = 2;
// To use conditional styles, we need to set corresponding flags on the table's style options.
// In this case, we set all of them:
t.Format.StyleOptions =
TableStyleOptions.FirstRow | TableStyleOptions.LastRow |
TableStyleOptions.FirstColumn | TableStyleOptions.LastColumn |
TableStyleOptions.RowBands | TableStyleOptions.ColumnBands;
// Set up the table for row and column bands:
ts1.Table.RowStripe = 1;
ts1.Table.ColumnStripe = 1;
// Shortcut to access table's conditionals:
var conds = ts1.Table.Conditionals;
// Odd rows' style:
conds[TableStyleOverride.Band1Horizontal].Font.Bold = true;
// Band*Vertical styles override Band*Horizontal styles if there is a conflict.
// Here, becase we set vertical band backgrounds, horizontal ones
// will not be seen in the document:
conds[TableStyleOverride.Band1Horizontal].Shading.Texture = TexturePattern.Clear; // overridden
conds[TableStyleOverride.Band1Horizontal].Shading.BackgroundPatternColor.RGB = Color.Magenta; // overridden
// Even rows' style:
conds[TableStyleOverride.Band2Horizontal].Font.Italic = true;
// Won't be seen in the document (see comment above):
conds[TableStyleOverride.Band2Horizontal].Shading.Texture = TexturePattern.Clear; // overridden
conds[TableStyleOverride.Band2Horizontal].Shading.BackgroundPatternColor.RGB = Color.Brown; // overridden
// Odd columns' style:
conds[TableStyleOverride.Band1Vertical].Shading.Texture = TexturePattern.Clear;
conds[TableStyleOverride.Band1Vertical].Shading.BackgroundPatternColor.RGB = Color.FromArgb(0xe6, 0xff, 0xe6);
// Even columns' style:
conds[TableStyleOverride.Band2Vertical].Shading.Texture = TexturePattern.Clear;
conds[TableStyleOverride.Band2Vertical].Shading.BackgroundPatternColor.RGB = Color.FromArgb(0xff, 0xff, 0xe6);
// First/last/corner styles will override horizontal and vertical bands if there is a conflict:
conds[TableStyleOverride.FirstColumn].Shading.Texture = TexturePattern.Clear;
conds[TableStyleOverride.FirstColumn].Shading.BackgroundPatternColor.RGB = Color.CadetBlue;
conds[TableStyleOverride.FirstRow].Shading.Texture = TexturePattern.Clear;
conds[TableStyleOverride.FirstRow].Shading.BackgroundPatternColor.RGB = Color.PaleVioletRed;
conds[TableStyleOverride.LastColumn].Shading.Texture = TexturePattern.Clear;
conds[TableStyleOverride.LastColumn].Shading.BackgroundPatternColor.RGB = Color.PapayaWhip;
conds[TableStyleOverride.LastRow].Shading.Texture = TexturePattern.Clear;
conds[TableStyleOverride.LastRow].Shading.BackgroundPatternColor.RGB = Color.PaleGoldenrod;
conds[TableStyleOverride.NorthWestCell].Shading.Texture = TexturePattern.Clear;
conds[TableStyleOverride.NorthWestCell].Shading.BackgroundPatternColor.RGB = Color.Red;
conds[TableStyleOverride.NorthEastCell].Shading.Texture = TexturePattern.Clear;
conds[TableStyleOverride.NorthEastCell].Shading.BackgroundPatternColor.RGB = Color.Green;
conds[TableStyleOverride.SouthWestCell].Shading.Texture = TexturePattern.Clear;
conds[TableStyleOverride.SouthWestCell].Shading.BackgroundPatternColor.RGB = Color.Blue;
conds[TableStyleOverride.SouthEastCell].Shading.Texture = TexturePattern.Clear;
conds[TableStyleOverride.SouthEastCell].Shading.BackgroundPatternColor.RGB = Color.Purple;
// Done:
return doc;
}
}
}