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

'' This sample shows how to render a long text spanning multiple pages.
Public Class PaginatedText
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        ''
        '' Use TextLayout to render text:
        Dim tl = New TextLayout()
        '' If not specifying formats for individual runs, we MUST provide
        '' font and font size on TextLayout.DefaultFormat:
        tl.DefaultFormat.Font = StandardFonts.Times
        tl.DefaultFormat.FontSize = 12
        '' First line offset 1/2":
        tl.FirstLineIndent = 72 / 2
        ''
        '' All other formatting properties are left at their default values.
        '' In particular, TextLayout's default resolution is 72 dpi - 
        '' the same as GcPdf's, and WordWrap is true.
        ''
        '' Set TextLayout's area to the whole page:
        tl.MaxWidth = doc.PageSize.Width
        tl.MaxHeight = doc.PageSize.Height
        '' ...and have it manage the page margins (1" all around):
        tl.MarginAll = tl.Resolution
        ''
        '' Append the text (20 paragraphs so they would not fit on a single page)
        '' (note that TextLayout interprets vbCrLf as paragraph delimiter):
        tl.Append(Util.LoremIpsum(20))
        ''
        '' When all text has been added, we must calculate the glyphs needed to render the text,
        '' and perform the layout. This can be done by a single call to PerformLayout, passing true to
        '' recalculate glyphs first (even though the text won't all fit in the specified max size,
        '' we only need to call PerformLayout once):
        tl.PerformLayout(True)
        '' Use split options to provide widow/orphan control:
        Dim tso = New TextSplitOptions(tl)
        tso.MinLinesInFirstParagraph = 2
        tso.MinLinesInLastParagraph = 2
        '' In a loop, split and render the text:
        While (True)
            '' 'rest' will accept the text that did not fit:
            Dim rest As TextLayout = Nothing
            Dim splitResult = tl.Split(tso, rest)
            doc.Pages.Add().Graphics.DrawTextLayout(tl, PointF.Empty)
            If splitResult <> SplitResult.Split Then
                Exit While
            End If
            tl = rest
        End While
        ''
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class