MultiColumnText.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. '' Creates a simple 3-column text layout.
  11. '' For a slightly more complex but practically more useful way
  12. '' to render text in columns, see BalancedColumns.
  13. Public Class MultiColumnText
  14. Function CreatePDF(ByVal stream As Stream) As Integer
  15. Dim doc = New GcPdfDocument()
  16. Dim g = doc.NewPage().Graphics
  17. Dim tl = g.CreateTextLayout()
  18. tl.DefaultFormat.Font = StandardFonts.Times
  19. tl.DefaultFormat.FontSize = 12
  20. tl.TextAlignment = TextAlignment.Justified
  21. tl.FirstLineIndent = 72 / 2
  22. tl.ParagraphSpacing = 72 / 8
  23. '' Add some text (note that TextLayout interprets vbCr, vbLf and vbCrLf as paragraph delimiter):
  24. tl.Append(Util.LoremIpsum(20))
  25. '' Set up columns:
  26. Const colCount = 3
  27. '' 1/2" margins all around:
  28. Const margin = 72.0F / 2
  29. '' 1/4" gap between columns:
  30. Const colGap = margin / 4
  31. Dim colWidth = (doc.Pages.Last.Size.Width - margin * 2) / colCount - colGap * (colCount - 1)
  32. tl.MaxWidth = colWidth
  33. tl.MaxHeight = doc.Pages.Last.Size.Height - margin * 2
  34. '' Calculate glyphs and perform layout for the whole text:
  35. tl.PerformLayout(True)
  36. '' In a loop, split and render the text in the current column:
  37. Dim col = 0
  38. While True
  39. '' 'rest' will accept the text that did not fit:
  40. Dim rest As TextLayout = Nothing
  41. Dim splitResult = tl.Split(Nothing, rest)
  42. g.DrawTextLayout(tl, New PointF(margin + col * (colWidth + colGap), margin))
  43. If splitResult <> SplitResult.Split Then
  44. Exit While
  45. End If
  46. tl = rest
  47. col += 1
  48. If col = colCount Then
  49. doc.Pages.Add()
  50. g = doc.Pages.Last.Graphics
  51. col = 0
  52. End If
  53. End While
  54. ''
  55. '' Done:
  56. doc.Save(stream)
  57. Return doc.Pages.Count
  58. End Function
  59. End Class
  60.