TextColumns.vb
''
'' This code is part of GrapeCity Documents for Imaging samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Numerics
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging

'' This sample shows how to draw multi-column text on a GcBitmap.
Public Class TextColumns
    Function GenerateImage(
            Optional pixelWidth As Integer = 1024,
            Optional pixelHeight As Integer = 1024,
            Optional opaque As Boolean = True,
            Optional dpiX As Single = 96,
            Optional dpiY As Single = 96) As GcBitmap
        Dim bmp = New GcBitmap(pixelWidth, pixelHeight, True, dpiX, dpiY)
        Using g = bmp.CreateGraphics(Color.White)
            g.Renderer.Multithreaded = True
            g.Renderer.SlowAntialiasing = True

            Dim tl = g.CreateTextLayout()
            tl.DefaultFormat.Font = Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"))

            tl.DefaultFormat.FontSize = 12
            tl.TextAlignment = TextAlignment.Justified
            tl.FirstLineIndent = 96 / 2
            tl.ParagraphSpacing = 96 / 8

            '' Add some text (note that TextLayout interprets "\r\n", "\r" And "\n" as paragraph delimiters)
            tl.Append(Util.LoremIpsum(20))
            '' Set up columns
            Const colCount = 3
            Const margin = 96 / 2 '' 1/2" margins all around
            Const colGap = margin / 2 '' 1/4" gap between columns
            Dim colWidth = (bmp.Width - margin * 2 - colGap * (colCount - 1)) / colCount
            tl.MaxWidth = colWidth
            tl.MaxHeight = bmp.Height - margin * 2
            '' Calculate glyphs And perform layout for the whole text:
            tl.PerformLayout(True)

            '' In a loop, split And render the text in the current column:
            Dim col = 0
            While (True)
                '' The TextLayout that will hold the rest of the text which did Not fit in the current layout:
                Dim tso = New TextSplitOptions(tl) With
                {
                    .MinLinesInLastParagraph = 2,
                    .MinLinesInFirstParagraph = 2
                }
                Dim rest As TextLayout = Nothing
                Dim splitResult = tl.Split(tso, rest)
                g.DrawTextLayout(tl, New PointF(margin + col * (colWidth + colGap), margin))
                If splitResult <> SplitResult.Split Then
                    Exit While
                End If
                tl = rest
                col += 1
                If col = colCount Then
                    Exit While
                End If
            End While
        End Using
        Return bmp
    End Function
End Class