PageHeaders.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.  
  10. '' Demonstrates a simple way to generate left/centered/right aligned page headers and footers.
  11. Public Class PageHeaders
  12.  
  13. '' The document being generated:
  14. Private _doc As GcPdfDocument
  15.  
  16. '' Utility method to draw a part of a page header or footer.
  17. '' Parameters:
  18. '' - text: The part's text.
  19. '' - tf: The text format to use.
  20. '' - pageIdx: The page index.
  21. '' - header: True if this is a header, false if a footer.
  22. '' - horzAlign: Horizontal alignment (left/center/right).
  23. Private Sub RenderHeader(ByVal text As String, ByVal tf As TextFormat, ByVal pageIdx As Integer, ByVal header As Boolean, ByVal horzAlign As TextAlignment)
  24. Dim page = _doc.Pages(pageIdx)
  25. Dim tl = New TextLayout(72) With {.Resolution = page.Graphics.Resolution}
  26. tl.MaxWidth = page.Size.Width
  27. tl.MaxHeight = page.Size.Height
  28. '' 1" margins, adjust as needed:
  29. tl.MarginLeft = 72
  30. tl.MarginRight = 72
  31. '' 1/3" spacing above top/below bottom header, adjust as needed:
  32. tl.MarginTop = 72 / 3
  33. tl.MarginBottom = tl.MarginTop
  34. '' Vertical alignment:
  35. tl.ParagraphAlignment = If(header, ParagraphAlignment.Near, ParagraphAlignment.Far)
  36. '' Horizontal alignment:
  37. tl.TextAlignment = horzAlign
  38. tl.Append(text, tf)
  39. '' NOTE: if some part of a header or footer is static, we could cache the corresponding TextLayout
  40. '' object and save some cycles by just drawing that cached TextLayout on each page w/out anything else:
  41. tl.PerformLayout(True)
  42. '' Draw the header at (0,0) (header located by margins and alignment):
  43. page.Graphics.DrawTextLayout(tl, PointF.Empty)
  44. End Sub
  45.  
  46. '' The main program.
  47. Function CreatePDF(ByVal stream As Stream) As Integer
  48. _doc = New GcPdfDocument()
  49. Dim page = _doc.NewPage()
  50. '' Add a note about flipping landscape:
  51. Dim noteRect = Util.AddNote(
  52. "We flip page orientation in this sample only to show that these page headers can adapt to the changing page size.",
  53. page)
  54. ''
  55. '' Prepare a TextLayout with some long text and print it (see PaginatedText for details):
  56. Dim tl = page.Graphics.CreateTextLayout()
  57. tl.DefaultFormat.Font = StandardFonts.Times
  58. tl.DefaultFormat.FontSize = 12
  59. tl.MaxWidth = _doc.PageSize.Width
  60. tl.MaxHeight = _doc.PageSize.Height
  61. tl.MarginAll = tl.Resolution
  62. tl.MarginTop = noteRect.Bottom + 18
  63. '' Add sample text:
  64. tl.Append(Util.LoremIpsum(20))
  65. '' Calculate glyphs and perform layout (see also PerformLayout call in the loop below):
  66. tl.PerformLayout(True)
  67. '' In a loop, split and render the text:
  68. While True
  69. Dim rest As TextLayout = Nothing
  70. Dim splitResult = tl.Split(Nothing, rest)
  71. page.Graphics.DrawTextLayout(tl, PointF.Empty)
  72. If splitResult <> SplitResult.Split Then
  73. Exit While
  74. End If
  75. tl = rest
  76. tl.MarginTop = tl.Resolution
  77. page = _doc.Pages.Add()
  78. '' For sample sake, toggle page orientation:
  79. page.Landscape = Not _doc.Pages(_doc.Pages.Count - 2).Landscape
  80. '' Update layout size to reflect the new page orientation:
  81. tl.MaxWidth = page.Size.Width
  82. tl.MaxHeight = page.Size.Height
  83. '' Because we changed layout size, we must perform layout again -
  84. '' but can do it without recalculating glyphs:
  85. tl.PerformLayout(False)
  86. End While
  87. '' Render the headers in a separate loop (so that we can provide 'Page X of Y' header):
  88. Dim tf = New TextFormat() With {.Font = StandardFonts.Helvetica, .FontSize = 10, .ForeColor = Color.Gray}
  89. Dim now = Util.TimeNow().ToString("u")
  90. For pageIdx = 0 To _doc.Pages.Count - 1
  91. RenderHeader(now, tf, pageIdx, True, TextAlignment.Leading)
  92. RenderHeader("Easy Page Headers Sample", tf, pageIdx, True, TextAlignment.Center)
  93. RenderHeader($"Page {pageIdx + 1} of {_doc.Pages.Count}", tf, pageIdx, True, TextAlignment.Trailing)
  94. RenderHeader("Page footer - left", tf, pageIdx, False, TextAlignment.Leading)
  95. RenderHeader("DsPdf", tf, pageIdx, False, TextAlignment.Center)
  96. RenderHeader("Page footer - right", tf, pageIdx, False, TextAlignment.Trailing)
  97. Next
  98. ''
  99. '' Done:
  100. _doc.Save(stream)
  101. Return _doc.Pages.Count
  102. End Function
  103. End Class
  104.