GraphicsInTable.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 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 graphics drawings in cells,
'' using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
Public Class GraphicsInTable
    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)
            DrawTable(g, pixelSize.Width, pixelSize.Height)
        End Using
        Return bmp
    End Function

    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.AnchorTopLeft(Nothing, 36, 36)

        Dim ta = New TableRenderer(g,
            rt, FixedTableSides.TopLeft,
            rowCount:=7,
            columnCount:=6,
            gridLineColor:=Color.Black,
            gridLineWidth:=1)

        ta.RowRects(0).SetHeight(50)
        ta.ColumnRects(0).SetWidth(110)

        Dim cs = New CellStyle() With
            {
                .TextAlignment = TextAlignment.Center,
                .ParagraphAlignment = ParagraphAlignment.Center,
                .TextFormat = New TextFormat() With
                    {
                        .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSansBold.ttf")),
                        .FontSize = 16,
                        .FontSizeInGraphicUnits = True
                    }
            }

        '' Background style for displaying "Shape" with a diagonal line:
        Dim csCornerTopRight = New CellStyle(cs) With
            {
                .Background = True,
                .LineWidth = 1F,
                .Borders = FrameBorders.MainDiagonal,
                .TextAlignment = TextAlignment.Trailing,
                .ParagraphAlignment = ParagraphAlignment.Near,
                .PaddingRight = 10,
                .PaddingTop = 5
            }

        '' Normal style for displaying "Color" at the bottom left corner of the same cell:
        Dim csCornerBottomLeft = New CellStyle(cs) With
            {
                .TextAlignment = TextAlignment.Leading,
                .ParagraphAlignment = ParagraphAlignment.Far,
                .PaddingLeft = 10,
                .PaddingBottom = 5
            }

        '' Add a background cell at the top left corner:
        ta.AddCell(csCornerTopRight, 0, 0, "Shape")

        ta.AddCell(cs, 0, 1, "Circle")
        ta.AddCell(cs, 0, 2, "Triangle")
        ta.AddCell(cs, 0, 3, "Rectangle")
        ta.AddCell(cs, 0, 4, "Oval")
        ta.AddCell(cs, 0, 5, "Square")

        '' Add a normal cell at the top left corner:
        ta.AddCell(csCornerBottomLeft, 0, 0, "Color")

        ta.AddCell(cs, 1, 0, "Red")
        ta.AddCell(cs, 2, 0, "Green")
        ta.AddCell(cs, 3, 0, "Blue")
        ta.AddCell(cs, 4, 0, "Cyan")
        ta.AddCell(cs, 5, 0, "Magenta")
        ta.AddCell(cs, 6, 0, "Yellow")

        ta.DefaultCellStyle = New CellStyle() With
            {
                .PaddingTop = 3,
                .PaddingLeftRight = 20,
                .PaddingBottom = 55,
                .FixedWidth = False,
                .TextAlignment = TextAlignment.Center,
                .TextFormat = New TextFormat() With
                    {
                        .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSans.ttf")),
                        .FontSizeInGraphicUnits = True,
                        .FontSize = 14
                    },
                .CreateTextLayout = Function(gr As GcGraphics, style As CellStyle, dat As Object)
                                         Dim tl = gr.CreateTextLayout()
                                         tl.Append(CType(dat, Figure).Title, style.TextFormat)
                                         Return tl
                                     End Function,
                .CustomDraw = Sub(gr As GcGraphics, tcell As TableCell)
                                  '' A Figure can draw itself:
                                  CType(tcell.Data, Figure).Draw(gr, tcell.Width, tcell.Height)
                              End Sub
            }

        ta.AddCell(1, 1, New Figure("Red Circle", Shape.Circle, Color.Red))
        ta.AddCell(1, 2, New Figure("Red Triangle", Shape.Triangle, Color.Red))
        ta.AddCell(1, 3, New Figure("Red Rectangle", Shape.Rectangle, Color.Red))
        ta.AddCell(1, 4, New Figure("Red Oval", Shape.Oval, Color.Red))
        ta.AddCell(1, 5, New Figure("Red Square", Shape.Square, Color.Red))

        ta.AddCell(2, 1, New Figure("Green Circle", Shape.Circle, Color.Green))
        ta.AddCell(2, 2, New Figure("Green Triangle", Shape.Triangle, Color.Green))
        ta.AddCell(2, 3, New Figure("Green Rectangle", Shape.Rectangle, Color.Green))
        ta.AddCell(2, 4, New Figure("Green Oval", Shape.Oval, Color.Green))
        ta.AddCell(2, 5, New Figure("Green Square", Shape.Square, Color.Green))

        ta.AddCell(3, 1, New Figure("Blue Circle", Shape.Circle, Color.Blue))
        ta.AddCell(3, 2, New Figure("Blue Triangle", Shape.Triangle, Color.Blue))
        ta.AddCell(3, 3, New Figure("Blue Rectangle", Shape.Rectangle, Color.Blue))
        ta.AddCell(3, 4, New Figure("Blue Oval", Shape.Oval, Color.Blue))
        ta.AddCell(3, 5, New Figure("Blue Square", Shape.Square, Color.Blue))

        ta.AddCell(4, 1, New Figure("Cyan Circle", Shape.Circle, Color.Cyan))
        ta.AddCell(4, 2, New Figure("Cyan Triangle", Shape.Triangle, Color.Cyan))
        ta.AddCell(4, 3, New Figure("Cyan Rectangle", Shape.Rectangle, Color.Cyan))
        ta.AddCell(4, 4, New Figure("Cyan Oval", Shape.Oval, Color.Cyan))
        ta.AddCell(4, 5, New Figure("Cyan Square", Shape.Square, Color.Cyan))

        ta.AddCell(5, 1, New Figure("Magenta Circle", Shape.Circle, Color.Magenta))
        ta.AddCell(5, 2, New Figure("Magenta Triangle", Shape.Triangle, Color.Magenta))
        ta.AddCell(5, 3, New Figure("Magenta Rectangle", Shape.Rectangle, Color.Magenta))
        ta.AddCell(5, 4, New Figure("Magenta Oval", Shape.Oval, Color.Magenta))
        ta.AddCell(5, 5, New Figure("Magenta Square", Shape.Square, Color.Magenta))

        ta.AddCell(6, 1, New Figure("Yellow Circle", Shape.Circle, Color.Yellow))
        ta.AddCell(6, 2, New Figure("Yellow Triangle", Shape.Triangle, Color.Yellow))
        ta.AddCell(6, 3, New Figure("Yellow Rectangle", Shape.Rectangle, Color.Yellow))
        ta.AddCell(6, 4, New Figure("Yellow Oval", Shape.Oval, Color.Yellow))
        ta.AddCell(6, 5, New Figure("Yellow Square", Shape.Square, Color.Yellow))

        ta.Render()
    End Sub

    Private Enum Shape
        Circle
        Triangle
        Rectangle
        Oval
        Square
    End Enum

    Private Class Figure
        Public Title As String
        Public Shape As Shape
        Public Color As Color

        Public Sub New(title As String, shape As Shape, color As Color)
            Me.Title = title
            Me.Shape = shape
            Me.Color = color
        End Sub

        Public Sub Draw(g As GcGraphics, w As Single, h As Single)
            Dim rc As RectangleF
            Dim pen = New GrapeCity.Documents.Drawing.Pen(Color.Black, 1)
            Select Case Me.Shape
                Case Shape.Circle
                    rc = New RectangleF(w / 2 - 20, h - 50, 40, 40)
                    g.FillEllipse(rc, Me.Color)
                    g.DrawEllipse(rc, pen)
                Case Shape.Triangle
                    Dim points = New PointF() {
                        New PointF(w / 2, h - 50),
                        New PointF(w / 2 + 25, h - 10),
                        New PointF(w / 2 - 25, h - 10)
                    }
                    g.FillPolygon(points, Me.Color)
                    g.DrawPolygon(points, pen)
                Case Shape.Rectangle
                    rc = New RectangleF(w / 2 - 35, h - 50, 70, 40)
                    g.FillRectangle(rc, Me.Color)
                    g.DrawRectangle(rc, pen)
                Case Shape.Oval
                    rc = New RectangleF(w / 2 - 35, h - 50, 70, 40)
                    g.FillEllipse(rc, Me.Color)
                    g.DrawEllipse(rc, pen)
                Case Shape.Square
                    rc = New RectangleF(w / 2 - 20, h - 50, 40, 40)
                    g.FillRectangle(rc, Me.Color)
                    g.DrawRectangle(rc, pen)
            End Select
        End Sub
    End Class
End Class