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