PageLabels.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;

namespace DsPdfWeb.Demos.Basics
{
    // 此示例演示如何向文档添加页面标签。
    // 页面标签允许您将文档细分为以下序列
    // 逻辑相关的页面范围(例如前言、正文、后言)。
    // 在这个由“章节”组成的示例中,我们添加了一个单独的
    // 每章的页面标签范围。
    // 此示例​​中的代码与Outlines示例类似。
    public class PageLabels
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            // 主要文本的文本布局(默认 GcDocs.Pdf 分辨率为 72dpi):
            var 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;
            // 章节标题的文本布局:
            var 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;
            // 用于控制页面之间文本分割的分割选项:
            var to = new TextSplitOptions(tl)
            {
                RestMarginTop = tl.Resolution,
                MinLinesInFirstParagraph = 2,
                MinLinesInLastParagraph = 2
            };
            // 生成多个“章节”,为每个章节提供大纲条目:
            const int NChapters = 20;
            for (int i = 0; i < NChapters; ++i)
            {
                // 章节标题 - 打印为章节标题并添加为大纲节点:
                string chapter = $"Chapter {i + 1}";

                // 添加页面标签所需要做的就是添加 PageLabelingRange
                // 与范围内第一页的索引相关联,
                // 以及范围前缀和编号样式:
                doc.PageLabelingRanges.Add(doc.Pages.Count, new PageLabelingRange($"{chapter}, p. ", NumberingStyle.DecimalArabic, 1));

                doc.Pages.Add();
                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(Common.Util.LoremIpsum(7));
                // 在正文布局中考虑章节标题:
                tl.MarginTop = tlCaption.ContentRectangle.Bottom + 12;
                tl.PerformLayout(true);
                // 打印章节:
                while (true)
                {
                    // 'rest' 将接受不适合的文本:
                    var splitResult = tl.Split(to, out TextLayout rest);
                    doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty);
                    if (splitResult != SplitResult.Split)
                        break;
                    tl = rest;
                    var p = doc.Pages.Add();
                }
            }
            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}