DrawRotatedText.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.DrawRotatedText() method
  17. // with different combinations of arguments.
  18. // The DrawRotatedText() method draws text rotated within a specified rectangle
  19. // similar to how rotated text is drawn in MS Excel cells without borders.
  20. // See also the DrawSlantedText demo.
  21. public class DrawRotatedText
  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. // Draw text at different negative angles:
  34. Draw(g, q0(), angle: -90, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  35. Draw(g, q1(), angle: -60, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  36. Draw(g, q2(), angle: -45, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  37. Draw(g, q3(), angle: -30, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  38. // Draw text at different positive angles:
  39. page = doc.Pages.Add();
  40. g = page.Graphics;
  41. Draw(g, q0(), angle: -90, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
  42. Draw(g, q1(), angle: -60, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
  43. Draw(g, q2(), angle: -45, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
  44. Draw(g, q3(), angle: -30, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
  45. // Draw text using different vertical stacking:
  46. page = doc.Pages.Add();
  47. g = page.Graphics;
  48. Draw(g, q0(), angle: -30, verticalStacking: true, RotatedTextAlignment.TopRight, TextAlignment.Trailing);
  49. Draw(g, q1(), angle: -30, verticalStacking: false, RotatedTextAlignment.TopRight, TextAlignment.Trailing);
  50. Draw(g, q2(), angle: -30, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  51. Draw(g, q3(), angle: -30, verticalStacking: true, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  52. // RotatedTextAlignment affects the location of text (red) within the target rectangle (green):
  53. page = doc.Pages.Add();
  54. g = page.Graphics;
  55. Draw(g, q0(), angle: -30, verticalStacking: true, RotatedTextAlignment.TopRight, TextAlignment.Trailing);
  56. Draw(g, q1(), angle: 60, verticalStacking: false, RotatedTextAlignment.BottomRight, TextAlignment.Trailing);
  57. Draw(g, q2(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Leading);
  58. Draw(g, q3(), angle: -60, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  59. // Draw vertically stacked text using different TextAlignment values:
  60. page = doc.Pages.Add();
  61. g = page.Graphics;
  62. Draw(g, q0(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Leading);
  63. Draw(g, q1(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Trailing);
  64. Draw(g, q2(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Center);
  65. Draw(g, q3(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Distributed);
  66. // Draw horizontally stacked text using different TextAlignment values:
  67. page = doc.Pages.Add();
  68. g = page.Graphics;
  69. Draw(g, q0(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  70. Draw(g, q1(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Trailing);
  71. Draw(g, q2(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Center);
  72. Draw(g, q3(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Distributed);
  73.  
  74. // Done:
  75. doc.Save(stream);
  76. return doc.Pages.Count;
  77.  
  78. RectangleF q0()
  79. {
  80. return new RectangleF(gap, gap, w, h);
  81. }
  82. RectangleF q1()
  83. {
  84. return new RectangleF(gap + w + gap, gap, w, h);
  85. }
  86. RectangleF q2()
  87. {
  88. return new RectangleF(gap, gap + h + gap, w, h);
  89. }
  90. RectangleF q3()
  91. {
  92. return new RectangleF(gap + w + gap, gap + h + gap, w, h);
  93. }
  94. }
  95.  
  96. static void Draw(GcGraphics g, RectangleF rect, int angle, bool verticalStacking,
  97. RotatedTextAlignment rotatedAlign, TextAlignment textAlign)
  98. {
  99. // Draw a legend stating the current DrawRotatedText arguments' values:
  100. var tlLegend = g.CreateTextLayout();
  101. tlLegend.DefaultFormat.FontName = "Calibri";
  102. tlLegend.DefaultFormat.FontSize = 9;
  103. tlLegend.AppendLine($"Rotation angle: {angle}°");
  104. tlLegend.AppendLine($"Text alignment: {textAlign}");
  105. tlLegend.AppendLine($"Rotated text alignment: {rotatedAlign}");
  106. tlLegend.AppendLine($"Is vertical stacking: {verticalStacking}");
  107. g.DrawTextLayout(tlLegend, rect.Location);
  108.  
  109. // The target rectangle for the DrawRotatedText call:
  110. var d = tlLegend.ContentHeight + 12;
  111. rect.Y += d;
  112. rect.Height -= d;
  113.  
  114. // Text layout to draw:
  115. var tl = g.CreateTextLayout();
  116. tl.DefaultFormat.FontName = "Calibri";
  117. tl.DefaultFormat.FontSize = 12;
  118. tl.Append("The quick brown fox jumps over the lazy dog. ");
  119. tl.Append("The quick brown fox jumps over the lazy dog.");
  120. tl.TextAlignment = textAlign;
  121.  
  122. // Outline the target rectangle in green:
  123. g.DrawRectangle(rect, new GCDRAW::Pen(Color.PaleGreen, 3));
  124. // Outline the actual text rectangle in red:
  125. var tlCopy = tl.Clone(true);
  126. var tlRect = g.MeasureRotatedText(tlCopy, angle, verticalStacking, rect, rotatedAlign);
  127. g.DrawRectangle(tlRect, new GCDRAW::Pen(Color.Red, 1));
  128.  
  129. // Draw rotated text:
  130. g.DrawRotatedText(tl, angle, verticalStacking, rect, rotatedAlign);
  131. }
  132. }
  133. }
  134.