ConditionalTableStyles.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Word;
  11.  
  12. namespace DsWordWeb.Demos
  13. {
  14. // This sample demonstrates various conditional table formatting options.
  15. public class ConditionalTableStyles
  16. {
  17. public GcWordDocument CreateDocx()
  18. {
  19. GcWordDocument doc = new GcWordDocument();
  20.  
  21. // Table dimensions:
  22. var rows = 20;
  23. var cols = 7;
  24.  
  25. doc.Body.Paragraphs.Add(
  26. $"A {cols} columns by {rows} rows table, with conditional styles applied to most elements:");
  27.  
  28. // Add an empty table:
  29. var t = doc.Body.Tables.Add(0, 0);
  30.  
  31. // Add some rows and cells to it:
  32. var cells = new List<string>(cols);
  33. for (int col = 0; col < cols; ++col)
  34. cells.Add(string.Empty);
  35. for (int row = 0; row < rows; ++row)
  36. {
  37. for (int col = 0; col < cols; ++col)
  38. cells[col] = $"Row {row + 1}, col {col + 1}";
  39. t.Rows.Add(cells.ToArray());
  40. }
  41.  
  42. // Create a table style on which we will define conditional formatting:
  43. var ts1 = doc.Styles.Add("Table Style 1", StyleType.Table);
  44. // And assign the style to the table:
  45. t.Style = ts1;
  46.  
  47. // Set up simple borders:
  48. foreach (var border in ts1.Table.Borders)
  49. {
  50. border.LineStyle = LineStyle.Single;
  51. border.LineWidth = 0.5f;
  52. border.Color.RGB = Color.DarkGray;
  53. }
  54. // Add some padding:
  55. ts1.Table.Padding.All = 2;
  56.  
  57. // To use conditional styles, we need to set corresponding flags on the table's style options.
  58. // In this case, we set all of them:
  59. t.Format.StyleOptions =
  60. TableStyleOptions.FirstRow | TableStyleOptions.LastRow |
  61. TableStyleOptions.FirstColumn | TableStyleOptions.LastColumn |
  62. TableStyleOptions.RowBands | TableStyleOptions.ColumnBands;
  63.  
  64. // Set up the table for row and column bands:
  65. ts1.Table.RowStripe = 1;
  66. ts1.Table.ColumnStripe = 1;
  67.  
  68. // Shortcut to access table's conditionals:
  69. var conds = ts1.Table.Conditionals;
  70.  
  71. // Odd rows' style:
  72. conds[TableStyleOverride.Band1Horizontal].Font.Bold = true;
  73. // Band*Vertical styles override Band*Horizontal styles if there is a conflict.
  74. // Here, becase we set vertical band backgrounds, horizontal ones
  75. // will not be seen in the document:
  76. conds[TableStyleOverride.Band1Horizontal].Shading.Texture = TexturePattern.Clear; // overridden
  77. conds[TableStyleOverride.Band1Horizontal].Shading.BackgroundPatternColor.RGB = Color.Magenta; // overridden
  78. // Even rows' style:
  79. conds[TableStyleOverride.Band2Horizontal].Font.Italic = true;
  80. // Won't be seen in the document (see comment above):
  81. conds[TableStyleOverride.Band2Horizontal].Shading.Texture = TexturePattern.Clear; // overridden
  82. conds[TableStyleOverride.Band2Horizontal].Shading.BackgroundPatternColor.RGB = Color.Brown; // overridden
  83.  
  84. // Odd columns' style:
  85. conds[TableStyleOverride.Band1Vertical].Shading.Texture = TexturePattern.Clear;
  86. conds[TableStyleOverride.Band1Vertical].Shading.BackgroundPatternColor.RGB = Color.FromArgb(0xe6, 0xff, 0xe6);
  87. // Even columns' style:
  88. conds[TableStyleOverride.Band2Vertical].Shading.Texture = TexturePattern.Clear;
  89. conds[TableStyleOverride.Band2Vertical].Shading.BackgroundPatternColor.RGB = Color.FromArgb(0xff, 0xff, 0xe6);
  90.  
  91. // First/last/corner styles will override horizontal and vertical bands if there is a conflict:
  92. conds[TableStyleOverride.FirstColumn].Shading.Texture = TexturePattern.Clear;
  93. conds[TableStyleOverride.FirstColumn].Shading.BackgroundPatternColor.RGB = Color.CadetBlue;
  94.  
  95. conds[TableStyleOverride.FirstRow].Shading.Texture = TexturePattern.Clear;
  96. conds[TableStyleOverride.FirstRow].Shading.BackgroundPatternColor.RGB = Color.PaleVioletRed;
  97.  
  98. conds[TableStyleOverride.LastColumn].Shading.Texture = TexturePattern.Clear;
  99. conds[TableStyleOverride.LastColumn].Shading.BackgroundPatternColor.RGB = Color.PapayaWhip;
  100.  
  101. conds[TableStyleOverride.LastRow].Shading.Texture = TexturePattern.Clear;
  102. conds[TableStyleOverride.LastRow].Shading.BackgroundPatternColor.RGB = Color.PaleGoldenrod;
  103.  
  104. conds[TableStyleOverride.NorthWestCell].Shading.Texture = TexturePattern.Clear;
  105. conds[TableStyleOverride.NorthWestCell].Shading.BackgroundPatternColor.RGB = Color.Red;
  106.  
  107. conds[TableStyleOverride.NorthEastCell].Shading.Texture = TexturePattern.Clear;
  108. conds[TableStyleOverride.NorthEastCell].Shading.BackgroundPatternColor.RGB = Color.Green;
  109.  
  110. conds[TableStyleOverride.SouthWestCell].Shading.Texture = TexturePattern.Clear;
  111. conds[TableStyleOverride.SouthWestCell].Shading.BackgroundPatternColor.RGB = Color.Blue;
  112.  
  113. conds[TableStyleOverride.SouthEastCell].Shading.Texture = TexturePattern.Clear;
  114. conds[TableStyleOverride.SouthEastCell].Shading.BackgroundPatternColor.RGB = Color.Purple;
  115.  
  116. // Done:
  117. return doc;
  118. }
  119. }
  120. }
  121.