Outlines.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. '' Shows how to add ouline entries to a document.
  12. '' See also PaginatedText.
  13. Public Class Outlines
  14. Function CreatePDF(ByVal stream As Stream) As Integer
  15. Dim doc = New GcPdfDocument()
  16. '' Text layout for main text (default DsPdf resolution is 72dpi):
  17. Dim tl = New TextLayout(72)
  18. tl.DefaultFormat.Font = StandardFonts.Times
  19. tl.DefaultFormat.FontSize = 12
  20. tl.FirstLineIndent = 72 / 2
  21. tl.MaxWidth = doc.PageSize.Width
  22. tl.MaxHeight = doc.PageSize.Height
  23. tl.MarginAll = tl.Resolution
  24. '' Text layout for chapter headers:
  25. Dim tlCaption = New TextLayout(72)
  26. tlCaption.DefaultFormat.Font = StandardFonts.TimesBold
  27. tlCaption.DefaultFormat.FontSize = tl.DefaultFormat.FontSize + 4
  28. tlCaption.DefaultFormat.Underline = True
  29. tlCaption.MaxWidth = tl.MaxWidth
  30. tlCaption.MarginLeft = tlCaption.Resolution
  31. tlCaption.MarginTop = tlCaption.Resolution
  32. tlCaption.MarginRight = tlCaption.Resolution
  33. tlCaption.MarginBottom = tlCaption.Resolution
  34. '' Split options to control splitting of text between pages:
  35. Dim tso = New TextSplitOptions(tl) With {
  36. .RestMarginTop = tl.Resolution,
  37. .MinLinesInFirstParagraph = 2,
  38. .MinLinesInLastParagraph = 2
  39. }
  40. '' Generate a number of "chapters", provide outline entry for each:
  41. Const NChapters = 20
  42. For i = 0 To NChapters - 1
  43.  
  44. doc.Pages.Add()
  45. '' Chapter title - print as chapter header and add as outline node:
  46. Dim chapter = $"Chapter {i + 1}"
  47. tlCaption.Clear()
  48. tlCaption.Append(chapter)
  49. tlCaption.PerformLayout(True)
  50. '' Add outline node for the chapter:
  51. doc.Outlines.Add(New OutlineNode(chapter, New DestinationFitH(doc.Pages.Last, tlCaption.MarginTop)))
  52. '' Print the caption:
  53. doc.Pages.Last.Graphics.DrawTextLayout(tlCaption, PointF.Empty)
  54. '' Chapter text:
  55. tl.Clear()
  56. tl.FirstLineIsStartOfParagraph = True
  57. tl.LastLineIsEndOfParagraph = True
  58. tl.Append(Util.LoremIpsum(7))
  59. '' Account for chapter header in the main text layout:
  60. tl.MarginTop = tlCaption.ContentRectangle.Bottom + 12
  61. tl.PerformLayout(True)
  62. '' Print the chapter:
  63. While True
  64. '' 'rest' will accept the text that did not fit:
  65. Dim rest As TextLayout = Nothing
  66. Dim splitResult = tl.Split(tso, rest)
  67. doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty)
  68. If splitResult <> SplitResult.Split Then
  69. Exit While
  70. End If
  71. tl = rest
  72. doc.Pages.Add()
  73. End While
  74. Next
  75. ''
  76. '' Done:
  77. doc.Save(stream)
  78. Return doc.Pages.Count
  79. End Function
  80. End Class
  81.