//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem.IO;usingSystem.Drawing;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Text; namespaceDsPdfWeb.Demos.Basics{// Shows how to add outline entries to a document. // See also PaginatedText.publicclassOutlines {publicintCreatePDF(Stream stream) {var doc = newGcPdfDocument();// Text layout for main text (default DsPdf resolution is 72dpi):var tl = newTextLayout(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:var tlCaption = newTextLayout(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;// Split options to control splitting of text between pages:var to = newTextSplitOptions(tl) {RestMarginTop = tl.Resolution,MinLinesInFirstParagraph = 2,MinLinesInLastParagraph = 2 };// Generate a number of "chapters", provide outline entry for each:constintNChapters = 20;for (int i = 0; i < NChapters; ++i) { doc.Pages.Add();// Chapter title - print as chapter header and add as outline node:string chapter = $"Chapter {i + 1}"; tlCaption.Clear(); tlCaption.Append(chapter); tlCaption.PerformLayout(true);// Add outline node for the chapter: doc.Outlines.Add(newOutlineNode(chapter, newDestinationFitH(doc.Pages.Last, tlCaption.MarginTop)));// Print the caption: doc.Pages.Last.Graphics.DrawTextLayout(tlCaption, PointF.Empty);// Chapter text: tl.Clear(); tl.FirstLineIsStartOfParagraph = true; tl.LastLineIsEndOfParagraph = true; tl.Append(Common.Util.LoremIpsum(7));// Account for chapter header in the main text layout: tl.MarginTop = tlCaption.ContentRectangle.Bottom + 12; tl.PerformLayout(true);// Print the chapter:while (true) {// 'rest' will accept the text that did not fit:var splitResult = tl.Split(to, outTextLayout rest); doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty);if (splitResult != SplitResult.Split)break; tl = rest; doc.Pages.Add(); } }// Done: doc.Save(stream);return doc.Pages.Count; } }}