ConditionalTableStyles.vb
C# VB
'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''Imports System.DrawingImports GrapeCity.Documents.Word
'' This sample demonstrates various conditional table formatting options.Public Class ConditionalTableStyles Function CreateDocx() As GcWordDocument Dim doc = New GcWordDocument()
'' Table dimensions: Dim rows = 20 Dim cols = 7
doc.Body.Paragraphs.Add( $"A {cols} columns by {rows} rows table, with conditional styles applied to most elements:")
'' Add an empty table: Dim t = doc.Body.Tables.Add(0, 0)
'' Add some rows and cells to it: Dim cells = New List(Of String)(cols) For col = 1 To cols cells.Add(String.Empty) Next For row = 0 To rows - 1 For col = 0 To cols - 1 cells(col) = $"Row {row + 1}, col {col + 1}" Next t.Rows.Add(cells.ToArray()) Next
'' Create a table style on which we will define conditional formatting: Dim ts1 = doc.Styles.Add("Table Style 1", StyleType.Table) '' And assign the style to the table: t.Style = ts1
'' Set up simple borders: For Each border In ts1.Table.Borders
border.LineStyle = LineStyle.Single border.LineWidth = 0.5F border.Color.RGB = Color.DarkGray Next '' 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 Or TableStyleOptions.LastRow Or TableStyleOptions.FirstColumn Or TableStyleOptions.LastColumn Or TableStyleOptions.RowBands Or 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: Dim 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(&HE6, &HFF, &HE6) '' Even columns' style: conds(TableStyleOverride.Band2Vertical).Shading.Texture = TexturePattern.Clear conds(TableStyleOverride.Band2Vertical).Shading.BackgroundPatternColor.RGB = Color.FromArgb(&HFF, &HFF, &HE6)
'' 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 End FunctionEnd Class