Hyphenation.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. '' This sample shows how to hyphenate text that contains soft hyphen characters (0x00AD).
  12. '' Breaks will be inserted at soft hyphen positions (if they are present in the text)
  13. '' if TextLayout.WrapMode Is set to WordWrap. Two properties are provided to control
  14. '' hyphenation:
  15. '' - TextLayout.SoftHyphenReplacementCharCode: specifies the character used As replacement
  16. '' for soft hyphen when breaking words across lines. By default this property Is 0x002D
  17. '' (the Unicode hyphen-minus character). Setting this property to 0 breaks words without
  18. '' showing any visible hyphen character. Setting it to -1 prevents breaking words at soft
  19. '' hyphens).
  20. '' - TextLayout.LinesBetweenConsecutiveHyphens: specifies the minimum number Of non-
  21. '' hyphenated lines between lines ending in a hyphen. By default this property Is zero.
  22. Public Class Hyphenation
  23. Function CreatePDF(ByVal stream As Stream) As Integer
  24. '' The online hypho-o tool
  25. '' was used to insert soft hyphens in the sample text from WordCharWrap:
  26. Dim str =
  27. "Lose noth­ing in your doc­u­ments! Grape­City Doc­u­ments for PDF " +
  28. "in­cludes text and para­graph format­ting, spe­cial char­ac­ters, " +
  29. "mul­tiple lan­guages, RTL sup­port, ver­tic­al and ro­tated text " +
  30. "on all sup­por­ted plat­forms."
  31. '' Replace HTML soft hyphens with Unicode ones:
  32. str = str.Replace("­", $"{ChrW(&HAD)}")
  33.  
  34. Dim doc = New GcPdfDocument()
  35. Dim page = doc.NewPage()
  36. Dim g = page.Graphics
  37.  
  38. Dim tl = g.CreateTextLayout()
  39. tl.Append(str)
  40. tl.DefaultFormat.Font = StandardFonts.Times
  41. tl.DefaultFormat.FontSize = 12
  42. tl.MaxWidth = 72 * 3
  43.  
  44. '' By default 0x002D (hyphen-minus) will be used as the hyphenation character
  45. '' when breaking a line at a soft hyphen (0x00AD):
  46. tl.PerformLayout(True)
  47.  
  48. Dim dy = tl.Lines(0).Height + 72 / 16
  49. Dim rc = New RectangleF(72, 72 + dy, tl.MaxWidth.Value, 72 * 1.4F)
  50.  
  51. g.DrawString("Default hyphenation:", tl.DefaultFormat, New PointF(rc.Left, rc.Top - dy))
  52. g.DrawTextLayout(tl, rc.Location)
  53. g.DrawRectangle(rc, Color.CornflowerBlue)
  54.  
  55. rc.Offset(0, 72 * 2)
  56. '' This will avoid hyphenating two consecutive lines:
  57. tl.LinesBetweenConsecutiveHyphens = 1
  58. '' Changing hyphenation options requires RecalculateGlyphs():
  59. tl.PerformLayout(True)
  60. g.DrawString("LinesBetweenConsecutiveHyphens: 1", tl.DefaultFormat, New PointF(rc.Left, rc.Top - dy))
  61. g.DrawTextLayout(tl, rc.Location)
  62. g.DrawRectangle(rc, Color.CornflowerBlue)
  63.  
  64. rc.Offset(0, 72 * 2)
  65. '' Reset previous setting:
  66. tl.LinesBetweenConsecutiveHyphens = 0
  67. '' Prevent hyphenating words at all:
  68. tl.SoftHyphenReplacementCharCode = -1
  69. '' Changing hyphenation options requires RecalculateGlyphs():
  70. tl.PerformLayout(True)
  71. g.DrawString("SoftHyphenReplacementCharCode: -1", tl.DefaultFormat, New PointF(rc.Left, rc.Top - dy))
  72. g.DrawTextLayout(tl, rc.Location)
  73. g.DrawRectangle(rc, Color.CornflowerBlue)
  74.  
  75. '' Done
  76. doc.Save(stream)
  77. Return doc.Pages.Count
  78. End Function
  79. End Class
  80.