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