MultiColumnText.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text

'' 创建简单的 3 列文本布局。
'' 稍微复杂但实际上更有用的方法
'' 要呈现列中的文本,请参阅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
        '' 添加一些文本(请注意,TextLayout 将“\r\n”解释为段落分隔符):
        tl.Append(Util.LoremIpsum(20))
        '' 设置栏目:
        Const colCount = 3
        '' 周围 1/2" 边距:
        Const margin = 72.0F / 2
        '' 列间 1/4" 间隙:
        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
        '' 计算字形并对整个文本进行布局:
        tl.PerformLayout(True)
        '' 在循环中,拆分并渲染当前列中的文本:
        Dim col = 0
        While True
            '' 'rest' 将接受不适合的文本:
            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
        ''
        '' 完毕:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class