TextTrimming.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 demonstrates how to display ellipsis
  12. '' if a string does Not fit in the allocated space.
  13. Public Class TextTrimming
  14. Function CreatePDF(ByVal stream As Stream) As Integer
  15. Dim doc = New GcPdfDocument()
  16. Dim page = doc.NewPage()
  17. Dim g = page.Graphics
  18. Const Inch = 72
  19.  
  20. Dim str = "This is a long line of text which does not fit in the allocated space."
  21. Dim wid = Inch * 4
  22. Dim dy = 0.3F
  23.  
  24. Dim rc = Util.AddNote(
  25. "TextLayout allows you to display ellipsis (or other character) " +
  26. "at the end of a text line that did not fit in the allocated space." + vbLf +
  27. "To use trimming, set TrimmingGranularity to Character or Word " +
  28. "(the default is None). Trimming will kick in if WrapMode is NoWrap, " +
  29. "and the text is too long. Wrapped text may also display trimming if " +
  30. "the layout width is too narrow to fit a single word." + vbLf +
  31. "Below are examples of text untrimmed, trimmed to character and to word. " +
  32. "The next line demonstrates a different trimming character (tilde in this case). " +
  33. "The line before last shows how to trim text (respecting TrimmingGranularity) " +
  34. "without adding any trimming character. " +
  35. "Finally the last line shows the use of DelimiterCharCode and DelimiterCharCount " +
  36. "properties to shorten file paths.",
  37. page)
  38. Dim top = rc.Bottom + 36
  39.  
  40. Dim ip = New PointF(rc.Left, top)
  41.  
  42. Dim tl = g.CreateTextLayout()
  43. tl.DefaultFormat.Font = StandardFonts.Times
  44. tl.DefaultFormat.FontSize = 12
  45. tl.MaxWidth = wid
  46. tl.WrapMode = WrapMode.NoWrap
  47.  
  48. '' TrimmingGranularity Is None by default
  49. tl.Append(str)
  50. tl.PerformLayout(True)
  51. g.DrawTextLayout(tl, ip)
  52. ip.Y += tl.ContentHeight + dy
  53.  
  54. '' Character trimming:
  55. tl.TrimmingGranularity = TrimmingGranularity.Character
  56. '' NOTE that the recalculateGlyphsBeforeLayout parameter to PerformLayout
  57. '' may be false after the first call, as the text/font has Not changed:
  58. tl.PerformLayout(False)
  59. g.DrawTextLayout(tl, ip)
  60. ip.Y += tl.ContentHeight + dy
  61.  
  62. '' Word trimming:
  63. tl.TrimmingGranularity = TrimmingGranularity.Word
  64. tl.PerformLayout(False)
  65. g.DrawTextLayout(tl, ip)
  66. ip.Y += tl.ContentHeight + dy
  67.  
  68. '' tl.EllipsisCharCode Is HorizontalEllipsis (0x2026) by default.
  69. '' Change it to a tilde:
  70. tl.EllipsisCharCode = &H7E
  71. tl.PerformLayout(False)
  72. g.DrawTextLayout(tl, ip)
  73. ip.Y += tl.ContentHeight + dy
  74.  
  75. '' Finally, we may set tl.EllipsisCharCode to 0 to trim text
  76. '' without rendering any trimming character:
  77. tl.EllipsisCharCode = 0
  78. tl.PerformLayout(False)
  79. g.DrawTextLayout(tl, ip)
  80. ip.Y += tl.ContentHeight + dy
  81.  
  82. '' Another interesting feature Is provided by the pair of
  83. '' DelimiterCharCode And DelimiterCharCount properties.
  84. '' These would typically be used to shorten long paths
  85. '' preserving a specified number of parts at the string's tail:
  86. tl.Clear()
  87. tl.EllipsisCharCode = &H2026
  88. tl.Append("c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe")
  89. tl.DelimiterCharCode = Asc("\"c)
  90. tl.DelimiterCharCount = 2
  91. tl.PerformLayout(True)
  92. g.DrawTextLayout(tl, ip)
  93. ip.Y += tl.ContentHeight + dy
  94.  
  95. g.DrawRectangle(New RectangleF(rc.Left, top, wid, ip.Y - top), Color.OrangeRed)
  96.  
  97. '' Done
  98. doc.Save(stream)
  99. Return doc.Pages.Count
  100. End Function
  101. End Class
  102.