TableTextAlign.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 different
'' alignments of texts and paragraphs in table cells,
'' using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
Public Class TableTextAlign
    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, 30, 20)

        Dim ta = New TableRenderer(g,
            rt, FixedTableSides.TopLeft,
            rowCount:=4,
            columnCount:=3,
            gridLineColor:=Color.Black,
            gridLineWidth:=1,
            rowMinHeight:=28)

        Dim columns = ta.ColumnRects
        columns(0).SetWidth(150)
        columns(1).SetWidth(200)
        columns(2).SetWidth(200)

        Dim fmtNorm = New TextFormat() With
            {
                .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSerif.ttf")),
                .FontSize = 25,
                .FontSizeInGraphicUnits = True,
                .FontFeatures = New FontFeature() {New FontFeature(FeatureTag.dlig)}
            }
        Dim fmtOrange = New TextFormat(fmtNorm) With
            {
                .ForeColor = Color.Orange
            }

        Dim cs = New CellStyle() With
            {
                .PaddingLeftRight = 15,
                .PaddingBottom = 3,
                .TextAlignment = TextAlignment.Center,
                .TextFormat = fmtNorm,
                .CreateTextLayout = Function(gr As GcGraphics, style As CellStyle, dat As Object)
                                        Dim tl = gr.CreateTextLayout()
                                        tl.TextExtensionStrategy = TextExtensionStrategy.Excel
                                        tl.Append(CStr(dat), style.TextFormat)
                                        Return tl
                                    End Function
            }

        '' Setting the default cell style allows us to call the AddCell
        '' method without passing the explicit CellStyle object.
        ta.DefaultCellStyle = cs

        ta.AddCell(0, 0, "Column 1")
        ta.AddCell(0, 1, "Column 2")
        ta.AddCell(0, 2, "Column 3")

        ta.AddCell(New CellStyle(cs) With {.ParagraphAlignment = ParagraphAlignment.Far, .FillColor = Color.LemonChiffon},
            1, 0, "One-liner.")
        ta.AddCell(New CellStyle(cs) With {.ParagraphAlignment = ParagraphAlignment.Center},
            1, 1, "Multi-line and centered text.")
        ta.AddCell(New CellStyle(cs) With {.TextAlignment = TextAlignment.Distributed},
            1, 2, "A multi-line piece of text that is distributed within the table cell.")

        ta.AddCell(2, 0, "Apple")
        ta.AddCell(2, 1, "Banana")
        ta.AddCell(New CellStyle(cs) With {.TextFormat = fmtOrange}, 2, 2, "Orange")
        ta.AddCell(3, 0, "Apple")
        ta.AddCell(3, 1, "Banana")
        ta.AddCell(3, 2, "Orange")

        ta.Render()
    End Sub
End Class