TextColumns.vb
  1. ''
  2. '' This code is part of Document Solutions for Imaging demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Numerics
  8. Imports GrapeCity.Documents.Drawing
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Imaging
  11. Imports GCTEXT = GrapeCity.Documents.Text
  12. Imports GCDRAW = GrapeCity.Documents.Drawing
  13.  
  14. '' This sample shows how to draw multi-column text on a GcBitmap.
  15. Public Class TextColumns
  16. Function GenerateImage(
  17. ByVal pixelSize As Size,
  18. ByVal dpi As Single,
  19. ByVal opaque As Boolean,
  20. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  21.  
  22. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  23. Using g = bmp.CreateGraphics(Color.White)
  24. g.Renderer.Multithreaded = True
  25. g.Renderer.SlowAntialiasing = True
  26.  
  27. Dim tl = g.CreateTextLayout()
  28. tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"))
  29.  
  30. tl.DefaultFormat.FontSize = 12
  31. tl.TextAlignment = TextAlignment.Justified
  32. tl.FirstLineIndent = 96 / 2
  33. tl.ParagraphSpacing = 96 / 8
  34.  
  35. '' Add some text (note that TextLayout interprets "\r\n", "\r" And "\n" as paragraph delimiters)
  36. tl.Append(Util.LoremIpsum(20))
  37. '' Set up columns
  38. Const colCount = 3
  39. Const margin = 96 / 2 '' 1/2" margins all around
  40. Const colGap = margin / 2 '' 1/4" gap between columns
  41. Dim colWidth = (bmp.Width - margin * 2 - colGap * (colCount - 1)) / colCount
  42. tl.MaxWidth = colWidth
  43. tl.MaxHeight = bmp.Height - margin * 2
  44. '' Calculate glyphs And perform layout for the whole text:
  45. tl.PerformLayout(True)
  46.  
  47. '' In a loop, split And render the text in the current column:
  48. Dim col = 0
  49. While (True)
  50. '' The TextLayout that will hold the rest of the text which did Not fit in the current layout:
  51. Dim tso = New TextSplitOptions(tl) With
  52. {
  53. .MinLinesInLastParagraph = 2,
  54. .MinLinesInFirstParagraph = 2
  55. }
  56. Dim rest As TextLayout = Nothing
  57. Dim splitResult = tl.Split(tso, rest)
  58. g.DrawTextLayout(tl, New PointF(margin + col * (colWidth + colGap), margin))
  59. If splitResult <> SplitResult.Split Then
  60. Exit While
  61. End If
  62. tl = rest
  63. col += 1
  64. If col = colCount Then
  65. Exit While
  66. End If
  67. End While
  68. End Using
  69. Return bmp
  70. End Function
  71. End Class
  72.