PageLabels.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Pdf
  8. Imports GrapeCity.Documents.Text
  9. Imports GrapeCity.Documents.Drawing
  10.  
  11. '' This sample shows how to add page labels to a document.
  12. '' Page labels allow you to subdivide the document into sequences of
  13. '' logically related page ranges (e.g. preface, main body, postface).
  14. '' In this sample consisting of 'chapters', we add a separate
  15. '' page labeling range for each chapter.
  16. '' The code in this sample is similar to the Outlines sample.
  17. Public Class PageLabels
  18. Sub CreatePDF(ByVal stream As Stream)
  19. Dim doc = New GcPdfDocument()
  20. '' Text layout for main text (default DsPdf resolution is 72dpi):
  21. Dim tl = New TextLayout(72)
  22. tl.DefaultFormat.Font = StandardFonts.Times
  23. tl.DefaultFormat.FontSize = 12
  24. tl.FirstLineIndent = 72 / 2
  25. tl.MaxWidth = doc.PageSize.Width
  26. tl.MaxHeight = doc.PageSize.Height
  27. tl.MarginAll = tl.Resolution
  28. '' Text layout for chapter headers:
  29. Dim tlCaption = New TextLayout(72)
  30. tlCaption.DefaultFormat.Font = StandardFonts.TimesBold
  31. tlCaption.DefaultFormat.FontSize = tl.DefaultFormat.FontSize + 4
  32. tlCaption.DefaultFormat.Underline = True
  33. tlCaption.MaxWidth = tl.MaxWidth
  34. tlCaption.MarginAll = tlCaption.Resolution
  35. '' Split options to control splitting of text between pages:
  36. Dim tso = New TextSplitOptions(tl) With
  37. {
  38. .RestMarginTop = tl.Resolution,
  39. .MinLinesInFirstParagraph = 2,
  40. .MinLinesInLastParagraph = 2
  41. }
  42. '' Generate a number of "chapters", provide outline entry for each:
  43. Const NChapters = 20
  44. For i = 0 To NChapters - 1
  45. '' Chapter title - print as chapter header and add as outline node:
  46. Dim chapter = $"Chapter {i + 1}"
  47.  
  48. '' All it takes to add page lables is to add a PageLabelingRange
  49. '' associated with the index of the first page in the range,
  50. '' and the range prefix and numbering style:
  51. doc.PageLabelingRanges.Add(doc.Pages.Count, New PageLabelingRange($"{chapter}, p. ", NumberingStyle.DecimalArabic, 1))
  52.  
  53. doc.Pages.Add()
  54. tlCaption.Clear()
  55. tlCaption.Append(chapter)
  56. tlCaption.PerformLayout(True)
  57. '' Add outline node for the chapter:
  58. doc.Outlines.Add(New OutlineNode(chapter, New DestinationFitH(doc.Pages.Last, tlCaption.MarginTop)))
  59. '' Print the caption:
  60. doc.Pages.Last.Graphics.DrawTextLayout(tlCaption, PointF.Empty)
  61. '' Chapter text:
  62. tl.Clear()
  63. tl.FirstLineIsStartOfParagraph = True
  64. tl.LastLineIsEndOfParagraph = True
  65. tl.Append(Util.LoremIpsum(7))
  66. '' Account for chapter header in the main text layout:
  67. tl.MarginTop = tlCaption.ContentRectangle.Bottom + 12
  68. tl.PerformLayout(True)
  69. '' Print the chapter:
  70. While True
  71. '' 'rest' will accept the text that did not fit:
  72. Dim rest As TextLayout = Nothing
  73. Dim splitResult = tl.Split(tso, rest)
  74. doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty)
  75. If splitResult <> SplitResult.Split Then
  76. Exit While
  77. End If
  78. tl = rest
  79. Dim p = doc.Pages.Add()
  80. End While
  81. Next
  82. '' Done:
  83. doc.Save(stream)
  84. End Sub
  85. End Class
  86.