PageLabels.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. // This sample shows how to add page labels to a document.
  15. // Page labels allow you to subdivide the document into sequences of
  16. // logically related page ranges (e.g. preface, main body, postface).
  17. // In this sample consisting of 'chapters', we add a separate
  18. // page labeling range for each chapter.
  19. // The code in this sample is similar to the Outlines sample.
  20. public class PageLabels
  21. {
  22. public int CreatePDF(Stream stream)
  23. {
  24. var doc = new GcPdfDocument();
  25. // Text layout for main text (default DsPdf resolution is 72dpi):
  26. var tl = new TextLayout(72);
  27. tl.DefaultFormat.Font = StandardFonts.Times;
  28. tl.DefaultFormat.FontSize = 12;
  29. tl.FirstLineIndent = 72 / 2;
  30. tl.MaxWidth = doc.PageSize.Width;
  31. tl.MaxHeight = doc.PageSize.Height;
  32. tl.MarginAll = tl.Resolution;
  33. // Text layout for chapter headers:
  34. var tlCaption = new TextLayout(72);
  35. tlCaption.DefaultFormat.Font = StandardFonts.TimesBold;
  36. tlCaption.DefaultFormat.FontSize = tl.DefaultFormat.FontSize + 4;
  37. tlCaption.DefaultFormat.Underline = true;
  38. tlCaption.MaxWidth = tl.MaxWidth;
  39. tlCaption.MarginAll = tlCaption.Resolution;
  40. // Split options to control splitting of text between pages:
  41. var to = new TextSplitOptions(tl)
  42. {
  43. RestMarginTop = tl.Resolution,
  44. MinLinesInFirstParagraph = 2,
  45. MinLinesInLastParagraph = 2
  46. };
  47. // Generate a number of "chapters", provide outline entry for each:
  48. const int NChapters = 20;
  49. for (int i = 0; i < NChapters; ++i)
  50. {
  51. // Chapter title - print as chapter header and add as outline node:
  52. string chapter = $"Chapter {i + 1}";
  53.  
  54. // All it takes to add page lables is to add a PageLabelingRange
  55. // associated with the index of the first page in the range,
  56. // and the range prefix and numbering style:
  57. doc.PageLabelingRanges.Add(doc.Pages.Count, new PageLabelingRange($"{chapter}, p. ", NumberingStyle.DecimalArabic, 1));
  58.  
  59. doc.Pages.Add();
  60. tlCaption.Clear();
  61. tlCaption.Append(chapter);
  62. tlCaption.PerformLayout(true);
  63. // Add outline node for the chapter:
  64. doc.Outlines.Add(new OutlineNode(chapter, new DestinationFitH(doc.Pages.Last, tlCaption.MarginTop)));
  65. // Print the caption:
  66. doc.Pages.Last.Graphics.DrawTextLayout(tlCaption, PointF.Empty);
  67. // Chapter text:
  68. tl.Clear();
  69. tl.FirstLineIsStartOfParagraph = true;
  70. tl.LastLineIsEndOfParagraph = true;
  71. tl.Append(Common.Util.LoremIpsum(7));
  72. // Account for chapter header in the main text layout:
  73. tl.MarginTop = tlCaption.ContentRectangle.Bottom + 12;
  74. tl.PerformLayout(true);
  75. // Print the chapter:
  76. while (true)
  77. {
  78. // 'rest' will accept the text that did not fit:
  79. var splitResult = tl.Split(to, out TextLayout rest);
  80. doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty);
  81. if (splitResult != SplitResult.Split)
  82. break;
  83. tl = rest;
  84. var p = doc.Pages.Add();
  85. }
  86. }
  87. // Done:
  88. doc.Save(stream);
  89. return doc.Pages.Count;
  90. }
  91. }
  92. }
  93.