OddEvenRows.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Drawing
Imports System.IO
Imports System.Numerics
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging
Imports GrapeCity.Documents.Layout
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' This example shows how to draw a table with differently shaded odd and even rows,
'' using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
Public Class OddEvenRows
    Public Function GenerateImage(
        ByVal pixelSize As Size,
        ByVal dpi As Single,
        ByVal opaque As Boolean,
        Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
        Using g = bmp.CreateGraphics(Color.White)
            g.Transform = Matrix3x2.CreateTranslation(CSng((-pixelSize.Width) \ 2), 0) * Matrix3x2.CreateScale(2)
            DrawTable(g, pixelSize.Width, pixelSize.Height)
            g.Transform = Matrix3x2.Identity
        End Using
        Return bmp
    End Function

    '' Data source for the table:
    Private Shared ReadOnly _teams As Team() = New Team() {
        New Team("England"),
        New Team("France"),
        New Team("Ireland"),
        New Team("Italy"),
        New Team("Scotland"),
        New Team("Wales")
    }

    Private Shared Sub DrawTable(g As GcGraphics, pageWidth As Single, pageHeight As Single)
        Dim host = New LayoutHost()
        Dim view = host.CreateView(pageWidth, pageHeight)

        Dim rt = view.CreateRect()
        rt.AnchorTopRight(Nothing, 36, 36)

        Dim ta = New TableRenderer(g,
            rt, FixedTableSides.TopRight,
            rowCount:=_teams.Length + 2,
            columnCount:=11,
            gridLineColor:=Color.FromArgb(173, 223, 252),
            gridLineWidth:=1,
            rowMinHeight:=10,
            columnMinWidth:=10)

        Dim fmt = New TextFormat() With
            {
                .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Regular.ttf")),
                .FontSize = 18,
                .FontSizeInGraphicUnits = True
            }
        Dim cs = New CellStyle() With
            {
                .TextFormat = fmt,
                .FixedWidth = False,
                .PaddingAll = 4
            }
        Dim csHeaderH = New CellStyle(cs) With
            {
                .TextFormat = New TextFormat(fmt) With
                    {
                        .ForeColor = Color.White,
                        .FontBold = True
                    },
                .FillColor = Color.FromArgb(17, 93, 140),
                .TextAlignment = TextAlignment.Center,
                .ParagraphAlignment = ParagraphAlignment.Center
            }
        Dim csHeaderV = New CellStyle(csHeaderH) With
            {
                .RotationAngle = 270,
                .TextAlignment = TextAlignment.Leading,
                .PaddingLeft = 3
            }
        Dim csNumber = New CellStyle(cs) With
            {
                .TextAlignment = TextAlignment.Center
            }
        Dim csNation = New CellStyle(cs) With
            {
                .TextFormat = New TextFormat(fmt) With
                    {
                        .ForeColor = Color.FromArgb(50, 123, 197)
                    }
            }

        '' add cells to the header
        ta.AddCell(csHeaderV, 0, 0, 2, 1, "Position")
        ta.AddCell(csHeaderH, 0, 1, 2, 1, "Nation")
        ta.AddCell(csHeaderH, 0, 2, 1, 4, "Games")
        ta.AddCell(csHeaderV, 1, 2, "Played")
        ta.AddCell(csHeaderV, 1, 3, "Won")
        ta.AddCell(csHeaderV, 1, 4, "Drawn")
        ta.AddCell(csHeaderV, 1, 5, "Lost")
        ta.AddCell(csHeaderH, 0, 6, 1, 4, "Points")
        ta.AddCell(csHeaderV, 1, 6, "For")
        ta.AddCell(csHeaderV, 1, 7, "Against")
        ta.AddCell(csHeaderV, 1, 8, "Difference")
        ta.AddCell(csHeaderV, 1, 9, "Tries")
        ta.AddCell(csHeaderH, 0, 10, 2, 1, "Table" & vbLf & "points")

        '' add the data cells
        For i = 0 To _teams.Length - 1
            Dim rowIndex = i + 2
            Dim team = _teams(i)
            ta.AddCell(csNumber, rowIndex, 0, $"{i + 1}")
            ta.AddCell(csNation, rowIndex, 1, team.Nation)
            ta.AddCell(csNumber, rowIndex, 2, $"{team.Played}")
            ta.AddCell(csNumber, rowIndex, 3, $"{team.Won}")
            ta.AddCell(csNumber, rowIndex, 4, $"{team.Drawn}")
            ta.AddCell(csNumber, rowIndex, 5, $"{team.Lost}")
            ta.AddCell(csNumber, rowIndex, 6, $"{team.For}")
            ta.AddCell(csNumber, rowIndex, 7, $"{team.Against}")
            ta.AddCell(csNumber, rowIndex, 8, $"{team.Diff}")
            ta.AddCell(csNumber, rowIndex, 9, $"{team.Tries}")
            ta.AddCell(csNumber, rowIndex, 10, $"{team.TablePoints}")
        Next

        '' change background for odd rows
        ta.DefaultCellStyle = New CellStyle() With
            {
                .Background = True,
                .FillColor = Color.FromArgb(238, 238, 238)
            }
        For i = 0 To _teams.Length - 1 Step 2
            ta.AddCell(i + 2, 0, 1, 11)
        Next

        ta.Render()
    End Sub

    '' A class to store example data:
    Private Class Team
        Private Shared ReadOnly _rnd As Random = Util.NewRandom()

        Public Nation As String
        Public Played, Won, Drawn, Lost As Integer
        Public [For], Against, Diff, Tries As Integer
        Public TablePoints As Integer

        Friend Sub New(nation As String)
            Me.Nation = nation
            Played = _rnd.Next(0, 50)
            Won = _rnd.Next(0, 50)
            Drawn = _rnd.Next(0, 50)
            Lost = _rnd.Next(0, 50)
            [For] = _rnd.Next(0, 50)
            Against = _rnd.Next(0, 50)
            Diff = _rnd.Next(0, 50)
            Tries = _rnd.Next(0, 50)
            TablePoints = _rnd.Next(0, 150)
        End Sub
    End Class
End Class