KeepWithNext.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
Imports GrapeCity.Documents.Drawing

'' This sample shows how to prevent a page break between a paragraph
'' and the next one when splitting a TextLayout.
'' Splitting of text in this sample is similar to that in PaginatedText,
'' see comments in PaginatedText for more info on text handling.
Public Class KeepWithNext
    Function CreatePDF(ByVal stream As Stream) As Integer
        Const NPAR = 40
        Dim doc = New GcPdfDocument()
        Dim tl = New TextLayout(72) With
            {
                .FirstLineIndent = 72 / 2,
                .MaxWidth = doc.PageSize.Width,
                .MaxHeight = doc.PageSize.Height,
                .MarginAll = 72
            }
        tl.DefaultFormat.Font = StandardFonts.Times
        tl.DefaultFormat.FontSize = 12
        '' Text format for paragraphs kept together with next one:
        Dim tf = New TextFormat(tl.DefaultFormat) With
            {
                .FontSize = tl.DefaultFormat.FontSize + 2,
                .FontBold = True
            }
        '' We add a number of random 'lorem ipsum' paragraphs to this document,
        '' adding a 'caption' before each paragraph which is kept together
        '' with the following 'lorem ipsum' paragraph:
        For i = 0 To NPAR - 1
            '' 'Caption' kept together with the next paragraph:
            tl.Append("Caption kept together with the next paragraph. No page break after this.", tf)
            '' AppendParagraphBreak adds a paragraph break but prevents a page break between the two paragraphs:
            tl.AppendParagraphBreak()
            '' Random paragraph after 'caption':
            tl.Append(Util.LoremIpsum(1))
        Next
        tl.PerformLayout(True)
        '' We force all paragraph lines to stay on the same page,
        '' this makes it more obvious that 'caption' and following paragraph
        '' are kept on the same page:
        Dim tso = New TextSplitOptions(tl) With
            {
                .KeepParagraphLinesTogether = True
            }
        '' 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