MultiColumnText.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text

'' Creates a simple 3-column text layout.
'' For a slightly more complex but practically more useful way
'' to render text in columns, see BalancedColumns.
Public Class MultiColumnText
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim g = doc.NewPage().Graphics
        Dim tl = g.CreateTextLayout()
        tl.DefaultFormat.Font = StandardFonts.Times
        tl.DefaultFormat.FontSize = 12
        tl.TextAlignment = TextAlignment.Justified
        tl.FirstLineIndent = 72 / 2
        tl.ParagraphSpacing = 72 / 8
        '' Add some text (note that TextLayout interprets vbCr, vbLf and vbCrLf as paragraph delimiter):
        tl.Append(Util.LoremIpsum(20))
        '' Set up columns:
        Const colCount = 3
        '' 1/2" margins all around:
        Const margin = 72.0F / 2
        '' 1/4" gap between columns:
        Const colGap = margin / 4
        Dim colWidth = (doc.Pages.Last.Size.Width - margin * 2) / colCount - colGap * (colCount - 1)
        tl.MaxWidth = colWidth
        tl.MaxHeight = doc.Pages.Last.Size.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
            '' 'rest' will accept the text that did not fit:
            Dim rest As TextLayout = Nothing
            Dim splitResult = tl.Split(Nothing, 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
                doc.Pages.Add()
                g = doc.Pages.Last.Graphics
                col = 0
            End If
        End While
        ''
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class