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

'' 演示如何将轮廓条目添加到文档中。
'' 另请参阅PaginatedText。
Public Class Outlines
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        '' 主要文本的文本布局(默认 GcDocs.Pdf 分辨率为 72dpi):
        Dim tl = New TextLayout(72)
        tl.DefaultFormat.Font = StandardFonts.Times
        tl.DefaultFormat.FontSize = 12
        tl.FirstLineIndent = 72 / 2
        tl.MaxWidth = doc.PageSize.Width
        tl.MaxHeight = doc.PageSize.Height
        tl.MarginAll = tl.Resolution
        '' 章节标题的文本布局:
        Dim tlCaption = New TextLayout(72)
        tlCaption.DefaultFormat.Font = StandardFonts.TimesBold
        tlCaption.DefaultFormat.FontSize = tl.DefaultFormat.FontSize + 4
        tlCaption.DefaultFormat.Underline = True
        tlCaption.MaxWidth = tl.MaxWidth
        tlCaption.MarginLeft = tlCaption.Resolution
        tlCaption.MarginTop = tlCaption.Resolution
        tlCaption.MarginRight = tlCaption.Resolution
        tlCaption.MarginBottom = tlCaption.Resolution
        '' 用于控制页面之间文本分割的分割选项:
        Dim tso = New TextSplitOptions(tl) With {
            .RestMarginTop = tl.Resolution,
            .MinLinesInFirstParagraph = 2,
            .MinLinesInLastParagraph = 2
        }
        '' 生成多个“章节”,为每个章节提供大纲条目:
        Const NChapters = 20
        For i = 0 To NChapters - 1

            doc.Pages.Add()
            '' 章节标题 - 打印为章节标题并添加为大纲节点:
            Dim chapter = $"Chapter {i + 1}"
            tlCaption.Clear()
            tlCaption.Append(chapter)
            tlCaption.PerformLayout(True)
            '' 为章节添加大纲节点:
            doc.Outlines.Add(New OutlineNode(chapter, New DestinationFitH(doc.Pages.Last, tlCaption.MarginTop)))
            '' 打印标题:
            doc.Pages.Last.Graphics.DrawTextLayout(tlCaption, PointF.Empty)
            '' 章节正文:
            tl.Clear()
            tl.FirstLineIsStartOfParagraph = True
            tl.LastLineIsEndOfParagraph = True
            tl.Append(Util.LoremIpsum(7))
            '' 在正文布局中考虑章节标题:
            tl.MarginTop = tlCaption.ContentRectangle.Bottom + 12
            tl.PerformLayout(True)
            '' 打印章节:
            While True
                '' 'rest' 将接受不适合的文本:
                Dim rest As TextLayout = Nothing
                Dim splitResult = tl.Split(tso, rest)
                doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty)
                If splitResult <> SplitResult.Split Then
                    Exit While
                End If
                tl = rest
                doc.Pages.Add()
            End While
        Next
        ''
        '' 完毕:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class