Outlines.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Drawing;
  11.  
  12. namespace DsPdfWeb.Demos.Basics
  13. {
  14. // Shows how to add ouline entries to a document.
  15. // See also PaginatedText.
  16. public class Outlines
  17. {
  18. public int CreatePDF(Stream stream)
  19. {
  20. var doc = new GcPdfDocument();
  21. // Text layout for main text (default DsPdf resolution is 72dpi):
  22. var tl = new TextLayout(72);
  23. tl.DefaultFormat.Font = StandardFonts.Times;
  24. tl.DefaultFormat.FontSize = 12;
  25. tl.FirstLineIndent = 72 / 2;
  26. tl.MaxWidth = doc.PageSize.Width;
  27. tl.MaxHeight = doc.PageSize.Height;
  28. tl.MarginAll = tl.Resolution;
  29. // Text layout for chapter headers:
  30. var tlCaption = new TextLayout(72);
  31. tlCaption.DefaultFormat.Font = StandardFonts.TimesBold;
  32. tlCaption.DefaultFormat.FontSize = tl.DefaultFormat.FontSize + 4;
  33. tlCaption.DefaultFormat.Underline = true;
  34. tlCaption.MaxWidth = tl.MaxWidth;
  35. tlCaption.MarginLeft = tlCaption.MarginTop = tlCaption.MarginRight = tlCaption.MarginBottom = tlCaption.Resolution;
  36. // Split options to control splitting of text between pages:
  37. var to = new TextSplitOptions(tl)
  38. {
  39. RestMarginTop = tl.Resolution,
  40. MinLinesInFirstParagraph = 2,
  41. MinLinesInLastParagraph = 2
  42. };
  43. // Generate a number of "chapters", provide outline entry for each:
  44. const int NChapters = 20;
  45. for (int i = 0; i < NChapters; ++i)
  46. {
  47. doc.Pages.Add();
  48. // Chapter title - print as chapter header and add as outline node:
  49. string chapter = $"Chapter {i + 1}";
  50. tlCaption.Clear();
  51. tlCaption.Append(chapter);
  52. tlCaption.PerformLayout(true);
  53. // Add outline node for the chapter:
  54. doc.Outlines.Add(new OutlineNode(chapter, new DestinationFitH(doc.Pages.Last, tlCaption.MarginTop)));
  55. // Print the caption:
  56. doc.Pages.Last.Graphics.DrawTextLayout(tlCaption, PointF.Empty);
  57. // Chapter text:
  58. tl.Clear();
  59. tl.FirstLineIsStartOfParagraph = true;
  60. tl.LastLineIsEndOfParagraph = true;
  61. tl.Append(Common.Util.LoremIpsum(7));
  62. // Account for chapter header in the main text layout:
  63. tl.MarginTop = tlCaption.ContentRectangle.Bottom + 12;
  64. tl.PerformLayout(true);
  65. // Print the chapter:
  66. while (true)
  67. {
  68. // 'rest' will accept the text that did not fit:
  69. var splitResult = tl.Split(to, out TextLayout rest);
  70. doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty);
  71. if (splitResult != SplitResult.Split)
  72. break;
  73. tl = rest;
  74. doc.Pages.Add();
  75. }
  76. }
  77. // Done:
  78. doc.Save(stream);
  79. return doc.Pages.Count;
  80. }
  81. }
  82. }
  83.