'' 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 add page labels to a document.
'' Page labels allow to subdivide the document into sequences of
'' logically related page ranges (e.g. preface, main body, postface).
'' In this sample consisting of 'chapters', we add a separate
'' page labeling range for each chapter.
'' The code in this sample is similar to the Outlines sample.
Public Class PageLabels
    Sub CreatePDF(ByVal stream As Stream)
        Dim doc = New GcPdfDocument()
        '' Text layout for main text (default GcPdf resolution is 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
        '' Text layout for chapter headers:
        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.MarginAll = tlCaption.Resolution
        '' Split options to control splitting of text between pages:
        Dim tso = New TextSplitOptions(tl) With
            .RestMarginTop = tl.Resolution,
            .MinLinesInFirstParagraph = 2,
            .MinLinesInLastParagraph = 2
        '' Generate a number of "chapters", provide outline entry for each:
        Const NChapters = 20
        For i = 0 To NChapters - 1
            '' Chapter title - print as chapter header and add as outline node:
            Dim chapter = $"Chapter {i + 1}"

            '' All it takes to add page lables is to add a PageLabelingRange
            '' associated with the index of the first page in the range,
            '' and the range prefix and numbering style:
            doc.PageLabelingRanges.Add(doc.Pages.Count, New PageLabelingRange($"{chapter}, p. ", NumberingStyle.DecimalArabic, 1))

            '' Add outline node for the chapter:
            doc.Outlines.Add(New OutlineNode(chapter, New DestinationFitH(doc.Pages.Count - 1, tlCaption.MarginTop)))
            '' Print the caption:
            doc.Pages.Last.Graphics.DrawTextLayout(tlCaption, PointF.Empty)
            '' Chapter text:
            tl.FirstLineIsStartOfParagraph = True
            tl.LastLineIsEndOfParagraph = True
            '' Account for chapter header in the main text layout:
            tl.MarginTop = tlCaption.ContentRectangle.Bottom + 12
            '' Print the chapter:
            While True
                '' 'rest' will accept the text that did not fit:
                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
                Dim p = doc.Pages.Add()
            End While
        '' Done:
    End Sub
End Class