TextAlign.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. '' Demonstrates text alignment options (horizontal alignment for LRT text):
  12. '' Left / Centered / Right / Justified.
  13. Public Class TextAlign
  14. Function CreatePDF(ByVal stream As Stream) As Integer
  15. '' Helper function to generate a paragraph of random text:
  16. Dim makePara As Func(Of String) =
  17. Function()
  18. Return Util.LoremIpsum(1, 3, 4, 15, 20)
  19. End Function
  20. ''
  21. Dim doc = New GcPdfDocument()
  22. Dim g = doc.NewPage().Graphics
  23. '' Using Graphics.CreateTextLayout() ensures that TextLayout's resolution
  24. '' is set to the same value as that of the graphics (which is 72 dpi by default):
  25. Dim tl = g.CreateTextLayout()
  26. tl.DefaultFormat.Font = StandardFonts.Times
  27. tl.DefaultFormat.FontSize = 12
  28. '' Set layout size (full page with 1" margins):
  29. tl.MaxWidth = doc.Pages.Last.Size.Width - 72 * 2
  30. tl.MaxHeight = doc.Pages.Last.Size.Height - 72 * 2
  31. '' Strong text format for 'headers':
  32. Dim tf = New TextFormat(tl.DefaultFormat) With {.Font = StandardFonts.TimesBold}
  33. '' Insertion point:
  34. Dim ip = New PointF(72, 72)
  35. '' TextAlignment controls how text is aligned horizontally within the layout rectangle.
  36. '' We render 5 paragraphs with different alignments.
  37. '' Leading:
  38. tl.TextAlignment = TextAlignment.Leading
  39. tl.Append("TextAlignment.Leading: ", tf)
  40. tl.Append(makePara())
  41. tl.PerformLayout(True)
  42. g.DrawTextLayout(tl, ip)
  43. '' Advance insertion point, adding one line's height between paragraphs:
  44. ip.Y += tl.ContentHeight + tl.Lines(0).Height
  45. '' Center:
  46. tl.Clear()
  47. tl.TextAlignment = TextAlignment.Center
  48. tl.Append("TextAlignment.Center: ", tf)
  49. tl.Append(makePara())
  50. tl.PerformLayout(True)
  51. g.DrawTextLayout(tl, ip)
  52. '' Advance insertion point, adding one line's height between paragraphs:
  53. ip.Y += tl.ContentHeight + tl.Lines(0).Height
  54. '' Trailing:
  55. tl.Clear()
  56. tl.TextAlignment = TextAlignment.Trailing
  57. tl.Append("TextAlignment.Trailing: ", tf)
  58. tl.Append(makePara())
  59. tl.PerformLayout(True)
  60. g.DrawTextLayout(tl, ip)
  61. '' Advance insertion point, adding one line's height between paragraphs:
  62. ip.Y += tl.ContentHeight + tl.Lines(0).Height
  63. '' Justified:
  64. tl.Clear()
  65. tl.TextAlignment = TextAlignment.Justified
  66. tl.Append("TextAlignment.Justified: ", tf)
  67. tl.Append(makePara())
  68. tl.PerformLayout(True)
  69. g.DrawTextLayout(tl, ip)
  70. '' Advance insertion point, adding one line's height between paragraphs:
  71. ip.Y += tl.ContentHeight + tl.Lines(0).Height
  72. '' Distributed:
  73. tl.Clear()
  74. tl.TextAlignment = TextAlignment.Distributed
  75. tl.Append("TextAlignment.Distributed: ", tf)
  76. tl.Append(makePara())
  77. tl.PerformLayout(True)
  78. g.DrawTextLayout(tl, ip)
  79. ''
  80. '' Done:
  81. doc.Save(stream)
  82. Return doc.Pages.Count
  83. End Function
  84. End Class
  85.