TextColumns.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS 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
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' This sample shows how to draw multi-column text on a GcBitmap.
Public Class TextColumns
    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, True, dpi, dpi)
        Using g = bmp.CreateGraphics(Color.White)
            g.Renderer.Multithreaded = True
            g.Renderer.SlowAntialiasing = True

            Dim tl = g.CreateTextLayout()
            tl.DefaultFormat.Font = GCTEXT.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