DrawSlantedText.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. using GCTEXT = GrapeCity.Documents.Text;
  12. using GCDRAW = GrapeCity.Documents.Drawing;
  13.  
  14. namespace DsPdfWeb.Demos.Basics
  15. {
  16. // This demo illustrates the results of using the GcGraphics.DrawSlantedText() method
  17. // with different combinations of arguments.
  18. // The DrawSlantedText() method draws text slanted within a specified rectangle
  19. // similar to how rotated text is drawn in MS Excel cells with borders.
  20. // See also the DrawRotatedText demo.
  21. public class DrawSlantedText
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. var doc = new GcPdfDocument();
  26. var page = doc.Pages.Add();
  27. var g = page.Graphics;
  28. // Set up some values to manage layout:
  29. var gap = g.Resolution;
  30. var w = page.Size.Width / 3;
  31. var h = w;
  32.  
  33. // Various text alignments with SlantedTextAlignment.BelowRotatedInside:
  34. Draw(g, q0(), angle: -70, false, SlantedTextAlignment.BelowRotatedInside, TextAlignment.Leading);
  35. Draw(g, q1(), angle: -70, false, SlantedTextAlignment.BelowRotatedInside, TextAlignment.Trailing);
  36. Draw(g, q2(), angle: -70, false, SlantedTextAlignment.BelowRotatedInside, TextAlignment.Center);
  37. Draw(g, q3(), angle: -70, false, SlantedTextAlignment.BelowRotatedInside, TextAlignment.Distributed);
  38. // Various text alignments with SlantedTextAlignment.BelowRotatedOutside:
  39. page = doc.Pages.Add();
  40. g = page.Graphics;
  41. Draw(g, q0(), angle: -70, false, SlantedTextAlignment.BelowRotatedOutside, TextAlignment.Leading);
  42. Draw(g, q1(), angle: -70, false, SlantedTextAlignment.BelowRotatedOutside, TextAlignment.Trailing);
  43. Draw(g, q2(), angle: -70, false, SlantedTextAlignment.BelowRotatedOutside, TextAlignment.Center);
  44. Draw(g, q3(), angle: -70, false, SlantedTextAlignment.BelowRotatedOutside, TextAlignment.Distributed);
  45. // Various text alignments with SlantedTextAlignment.AboveRotatedInside:
  46. page = doc.Pages.Add();
  47. g = page.Graphics;
  48. Draw(g, q0(), angle: -70, false, SlantedTextAlignment.AboveRotatedInside, TextAlignment.Leading);
  49. Draw(g, q1(), angle: -70, false, SlantedTextAlignment.AboveRotatedInside, TextAlignment.Trailing);
  50. Draw(g, q2(), angle: -70, false, SlantedTextAlignment.AboveRotatedInside, TextAlignment.Center);
  51. Draw(g, q3(), angle: -70, false, SlantedTextAlignment.AboveRotatedInside, TextAlignment.Distributed);
  52. // Various text alignments with SlantedTextAlignment.AboveRotatedOutside:
  53. page = doc.Pages.Add();
  54. g = page.Graphics;
  55. Draw(g, q0(), angle: -70, false, SlantedTextAlignment.AboveRotatedOutside, TextAlignment.Leading);
  56. Draw(g, q1(), angle: -70, false, SlantedTextAlignment.AboveRotatedOutside, TextAlignment.Trailing);
  57. Draw(g, q2(), angle: -70, false, SlantedTextAlignment.AboveRotatedOutside, TextAlignment.Center);
  58. Draw(g, q3(), angle: -70, false, SlantedTextAlignment.AboveRotatedOutside, TextAlignment.Distributed);
  59. // Various text alignments with SlantedTextAlignment.CenterInsideOutside:
  60. page = doc.Pages.Add();
  61. g = page.Graphics;
  62. Draw(g, q0(), angle: -70, false, SlantedTextAlignment.CenterInsideOutside, TextAlignment.Leading);
  63. Draw(g, q1(), angle: -70, false, SlantedTextAlignment.CenterInsideOutside, TextAlignment.Trailing);
  64. Draw(g, q2(), angle: -70, false, SlantedTextAlignment.CenterInsideOutside, TextAlignment.Center);
  65. Draw(g, q3(), angle: -70, false, SlantedTextAlignment.CenterInsideOutside, TextAlignment.Distributed);
  66. // Various text alignments with SlantedTextAlignment.CenterOutsideInside:
  67. page = doc.Pages.Add();
  68. g = page.Graphics;
  69. Draw(g, q0(), angle: -70, false, SlantedTextAlignment.CenterOutsideInside, TextAlignment.Leading);
  70. Draw(g, q1(), angle: -70, false, SlantedTextAlignment.CenterOutsideInside, TextAlignment.Trailing);
  71. Draw(g, q2(), angle: -70, false, SlantedTextAlignment.CenterOutsideInside, TextAlignment.Center);
  72. Draw(g, q3(), angle: -70, false, SlantedTextAlignment.CenterOutsideInside, TextAlignment.Distributed);
  73. // Examples with positive rotation angle using SlantedTextAlignment.BelowRotatedInside:
  74. page = doc.Pages.Add();
  75. g = page.Graphics;
  76. Draw(g, q0(), angle: 70, false, SlantedTextAlignment.BelowRotatedInside, TextAlignment.Leading);
  77. Draw(g, q1(), angle: 70, false, SlantedTextAlignment.BelowRotatedInside, TextAlignment.Trailing);
  78. Draw(g, q2(), angle: 70, false, SlantedTextAlignment.BelowRotatedInside, TextAlignment.Center);
  79. Draw(g, q3(), angle: 70, false, SlantedTextAlignment.BelowRotatedInside, TextAlignment.Distributed);
  80. // Examples with positive rotation angle using SlantedTextAlignment.AboveRotatedInside:
  81. page = doc.Pages.Add();
  82. g = page.Graphics;
  83. Draw(g, q0(), angle: 70, false, SlantedTextAlignment.AboveRotatedInside, TextAlignment.Leading);
  84. Draw(g, q1(), angle: 70, false, SlantedTextAlignment.AboveRotatedInside, TextAlignment.Trailing);
  85. Draw(g, q2(), angle: 70, false, SlantedTextAlignment.AboveRotatedInside, TextAlignment.Center);
  86. Draw(g, q3(), angle: 70, false, SlantedTextAlignment.AboveRotatedInside, TextAlignment.Distributed);
  87.  
  88. // Below are examples of vertically stacked text.
  89.  
  90. // Vertically stacked text rotated to a negative angle using SlantedTextAlignment.BelowRotatedInside:
  91. page = doc.Pages.Add();
  92. g = page.Graphics;
  93. Draw(g, q0(), angle: -20, true, SlantedTextAlignment.BelowRotatedInside, TextAlignment.Leading);
  94. Draw(g, q1(), angle: -20, true, SlantedTextAlignment.BelowRotatedInside, TextAlignment.Trailing);
  95. Draw(g, q2(), angle: -20, true, SlantedTextAlignment.BelowRotatedInside, TextAlignment.Center);
  96. Draw(g, q3(), angle: -20, true, SlantedTextAlignment.BelowRotatedInside, TextAlignment.Distributed);
  97. // Vertically stacked text rotated to a negative angle using SlantedTextAlignment.BelowRotatedOutside:
  98. page = doc.Pages.Add();
  99. g = page.Graphics;
  100. Draw(g, q0(), angle: -20, true, SlantedTextAlignment.BelowRotatedOutside, TextAlignment.Leading);
  101. Draw(g, q1(), angle: -20, true, SlantedTextAlignment.BelowRotatedOutside, TextAlignment.Trailing);
  102. Draw(g, q2(), angle: -20, true, SlantedTextAlignment.BelowRotatedOutside, TextAlignment.Center);
  103. Draw(g, q3(), angle: -20, true, SlantedTextAlignment.BelowRotatedOutside, TextAlignment.Distributed);
  104. // Vertically stacked text rotated to a positive angle using SlantedTextAlignment.AboveRotatedOutside:
  105. page = doc.Pages.Add();
  106. g = page.Graphics;
  107. Draw(g, q0(), angle: 20, true, SlantedTextAlignment.AboveRotatedOutside, TextAlignment.Leading);
  108. Draw(g, q1(), angle: 20, true, SlantedTextAlignment.AboveRotatedOutside, TextAlignment.Trailing);
  109. Draw(g, q2(), angle: 20, true, SlantedTextAlignment.AboveRotatedOutside, TextAlignment.Center);
  110. Draw(g, q3(), angle: 20, true, SlantedTextAlignment.AboveRotatedOutside, TextAlignment.Distributed);
  111. // Vertically stacked text rotated to a positive angle using SlantedTextAlignment.CenterOutsideInside:
  112. page = doc.Pages.Add();
  113. g = page.Graphics;
  114. Draw(g, q0(), angle: 20, true, SlantedTextAlignment.CenterOutsideInside, TextAlignment.Leading);
  115. Draw(g, q1(), angle: 20, true, SlantedTextAlignment.CenterOutsideInside, TextAlignment.Trailing);
  116. Draw(g, q2(), angle: 20, true, SlantedTextAlignment.CenterOutsideInside, TextAlignment.Center);
  117. Draw(g, q3(), angle: 20, true, SlantedTextAlignment.CenterOutsideInside, TextAlignment.Distributed);
  118.  
  119. // Done:
  120. doc.Save(stream);
  121. return doc.Pages.Count;
  122.  
  123. RectangleF q0()
  124. {
  125. return new RectangleF(gap, gap, w, h);
  126. }
  127. RectangleF q1()
  128. {
  129. return new RectangleF(gap + w + gap, gap, w, h);
  130. }
  131. RectangleF q2()
  132. {
  133. return new RectangleF(gap, gap + h + gap, w, h);
  134. }
  135. RectangleF q3()
  136. {
  137. return new RectangleF(gap + w + gap, gap + h + gap, w, h);
  138. }
  139. }
  140.  
  141. static void Draw(GcGraphics g, RectangleF rect, int angle, bool verticalStacking,
  142. SlantedTextAlignment slantedAlign, TextAlignment textAlign)
  143. {
  144. // Draw a legend stating the current DrawRotatedText arguments' values:
  145. var tlLegend = g.CreateTextLayout();
  146. tlLegend.DefaultFormat.FontName = "Calibri";
  147. tlLegend.DefaultFormat.FontSize = 9;
  148. tlLegend.AppendLine($"Rotation angle: {angle}°");
  149. tlLegend.AppendLine($"Text alignment: {textAlign}");
  150. tlLegend.AppendLine($"Slanted text alignment: {slantedAlign}");
  151. tlLegend.AppendLine($"Is vertical stacking: {verticalStacking}");
  152. g.DrawTextLayout(tlLegend, rect.Location);
  153.  
  154. // The target rectangle for the DrawRotatedText call:
  155. var d = tlLegend.ContentHeight + g.Resolution;
  156. rect.Y += d;
  157. rect.Height -= d;
  158. rect.Width -= d / 2;
  159. var x = rect.X;
  160. var y = rect.Y;
  161. var w = rect.Width;
  162. var h = rect.Height;
  163.  
  164. // Draw the target rectangle:
  165. g.DrawRectangle(rect, new GCDRAW::Pen(Color.PaleGreen, 3));
  166.  
  167. if (!verticalStacking)
  168. {
  169. float dx = (float)(h / Math.Tan(Math.PI * angle / -180.0));
  170. switch (slantedAlign)
  171. {
  172. case SlantedTextAlignment.BelowRotatedInside:
  173. case SlantedTextAlignment.AboveRotatedOutside:
  174. case SlantedTextAlignment.CenterInsideOutside:
  175. g.DrawPolygon(new[] {
  176. new PointF(x + dx, y), new PointF(x + dx + w, y),
  177. new PointF(x + w, y + h), new PointF(x, y + h)},
  178. new GCDRAW::Pen(Color.Red, 1));
  179. break;
  180. case SlantedTextAlignment.BelowRotatedOutside:
  181. case SlantedTextAlignment.AboveRotatedInside:
  182. case SlantedTextAlignment.CenterOutsideInside:
  183. g.DrawPolygon(new[] {
  184. new PointF(x, y), new PointF(x + w, y),
  185. new PointF(x - dx + w, y + h), new PointF(x - dx, y + h)},
  186. new GCDRAW::Pen(Color.Red, 1));
  187. break;
  188. }
  189. }
  190. else
  191. {
  192. float dy = (float)(w * Math.Tan(Math.PI * angle / 180.0));
  193. switch (slantedAlign)
  194. {
  195. case SlantedTextAlignment.BelowRotatedInside:
  196. case SlantedTextAlignment.AboveRotatedOutside:
  197. case SlantedTextAlignment.CenterInsideOutside:
  198. if (angle >= 0)
  199. g.DrawPolygon(new[] {
  200. new PointF(x, y), new PointF(x + w, y + dy), new PointF(x + w, y + dy + h), new PointF(x, y + h)},
  201. new GCDRAW::Pen(Color.Red, 1));
  202. else
  203. g.DrawPolygon(new[] {
  204. new PointF(x, y - dy), new PointF(x + w, y), new PointF(x + w, y + h), new PointF(x, y - dy + h)},
  205. new GCDRAW::Pen(Color.Red, 1));
  206. break;
  207. case SlantedTextAlignment.BelowRotatedOutside:
  208. case SlantedTextAlignment.AboveRotatedInside:
  209. case SlantedTextAlignment.CenterOutsideInside:
  210. if (angle >= 0)
  211. g.DrawPolygon(new[] {
  212. new PointF(x, y - dy), new PointF(x + w, y), new PointF(x + w, y + h), new PointF(x, y - dy + h)},
  213. new GCDRAW::Pen(Color.Red, 1));
  214. else
  215. g.DrawPolygon(new[] {
  216. new PointF(x, y), new PointF(x + w, y + dy), new PointF(x + w, y + dy + h), new PointF(x, y + h)},
  217. new GCDRAW::Pen(Color.Red, 1));
  218. break;
  219. }
  220. }
  221. // Draw slanted text:
  222. var tl = g.CreateTextLayout();
  223. tl.DefaultFormat.FontName = "Calibri";
  224. tl.DefaultFormat.FontSize = 12;
  225. tl.TextAlignment = textAlign;
  226. tl.Append("The quick brown fox jumps over the lazy dog. ");
  227. tl.Append("The quick brown fox jumps over the lazy dog.");
  228. g.DrawSlantedText(tl, angle, verticalStacking, rect, slantedAlign);
  229. }
  230. }
  231. }
  232.