TextTrimming.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Drawing;
  11.  
  12. namespace DsPdfWeb.Demos.Basics
  13. {
  14. // This sample demonstrates how to display ellipsis
  15. // if a string does not fit in the allocated space.
  16. public class TextTrimming
  17. {
  18. public int CreatePDF(Stream stream)
  19. {
  20. var doc = new GcPdfDocument();
  21. var page = doc.NewPage();
  22. var g = page.Graphics;
  23. const float In = 72;
  24.  
  25. var str = "This is a long line of text which does not fit in the allocated space.";
  26. var wid = In * 4;
  27. var dy = 0.3f;
  28.  
  29. var rc = Common.Util.AddNote(
  30. "TextLayout allows you to display ellipsis (or other character) " +
  31. "at the end of a text line that did not fit in the allocated space.\n" +
  32. "To use trimming, set TrimmingGranularity to Character or Word " +
  33. "(the default is None). Trimming will kick in if WrapMode is NoWrap, " +
  34. "and the text is too long. Wrapped text may also display trimming if " +
  35. "the layout width is too narrow to fit a single word.\n" +
  36. "Below are examples of text untrimmed, trimmed to character and to word. " +
  37. "The next line demonstrates a different trimming character (tilde in this case). " +
  38. "The line before last shows how to trim text (respecting TrimmingGranularity) " +
  39. "without adding any trimming character. " +
  40. "Finally the last line shows the use of DelimiterCharCode and DelimiterCharCount " +
  41. "properties to shorten file paths.",
  42. page);
  43. var top = rc.Bottom + 36;
  44.  
  45. var ip = new PointF(rc.Left, top);
  46.  
  47. var tl = g.CreateTextLayout();
  48. tl.DefaultFormat.Font = StandardFonts.Times;
  49. tl.DefaultFormat.FontSize = 12;
  50. tl.MaxWidth = wid;
  51. tl.WrapMode = WrapMode.NoWrap;
  52.  
  53. // TrimmingGranularity is None by default:
  54. tl.Append(str);
  55. tl.PerformLayout(true);
  56. g.DrawTextLayout(tl, ip);
  57. ip.Y += tl.ContentHeight + dy;
  58.  
  59. // Character trimming:
  60. tl.TrimmingGranularity = TrimmingGranularity.Character;
  61. // NOTE that the recalculateGlyphsBeforeLayout parameter to PerformLayout
  62. // may be false after the first call, as the text/font has not changed:
  63. tl.PerformLayout(false);
  64. g.DrawTextLayout(tl, ip);
  65. ip.Y += tl.ContentHeight + dy;
  66.  
  67. // Word trimming:
  68. tl.TrimmingGranularity = TrimmingGranularity.Word;
  69. tl.PerformLayout(false);
  70. g.DrawTextLayout(tl, ip);
  71. ip.Y += tl.ContentHeight + dy;
  72.  
  73. // tl.EllipsisCharCode is HorizontalEllipsis (0x2026) by default.
  74. // Change it to a tilde:
  75. tl.EllipsisCharCode = 0x007E;
  76. tl.PerformLayout(false);
  77. g.DrawTextLayout(tl, ip);
  78. ip.Y += tl.ContentHeight + dy;
  79.  
  80. // We can also set tl.EllipsisCharCode to 0 to trim text
  81. // without rendering any trimming character:
  82. tl.EllipsisCharCode = 0;
  83. tl.PerformLayout(false);
  84. g.DrawTextLayout(tl, ip);
  85. ip.Y += tl.ContentHeight + dy;
  86.  
  87. // Another interesting feature is provided by the pair of
  88. // DelimiterCharCode and DelimiterCharCount properties.
  89. // These would typically be used to shorten long paths
  90. // preserving a specified number of parts at the string's tail:
  91. tl.Clear();
  92. tl.EllipsisCharCode = 0x2026;
  93. tl.Append(@"c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe");
  94. tl.DelimiterCharCode = '\\';
  95. tl.DelimiterCharCount = 2;
  96. tl.PerformLayout(true);
  97. g.DrawTextLayout(tl, ip);
  98. ip.Y += tl.ContentHeight + dy;
  99.  
  100. g.DrawRectangle(new RectangleF(rc.Left, top, wid, ip.Y - top), Color.OrangeRed);
  101.  
  102. // Done:
  103. doc.Save(stream);
  104. return doc.Pages.Count;
  105. }
  106. }
  107. }
  108.