Outlines.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
{
    // 演示如何将轮廓条目添加到文档中。
    // 另请参阅PaginatedText。
    public class Outlines
    {
        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.MarginLeft = tlCaption.MarginTop = tlCaption.MarginRight = tlCaption.MarginBottom = 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)
            {
                doc.Pages.Add();
                // 章节标题 - 打印为章节标题并添加为大纲节点:
                string 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(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;
                    doc.Pages.Add();
                }
            }
            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}