SlantedTableHeaders.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 System.Numerics;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12. using GCTEXT = GrapeCity.Documents.Text;
  13. using GCDRAW = GrapeCity.Documents.Drawing;
  14.  
  15. namespace DsPdfWeb.Demos.Basics
  16. {
  17. // This demo shows how to draw a table with slanted column headers,
  18. // so that long headers can be squeezed into narrow columns' headers.
  19. // The demo uses the GcGraphics.DrawSlantedText() method to draw the headers.
  20. // See also the DrawRotatedText and DrawSlantedText demos.
  21. public class SlantedTableHeaders
  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.  
  29. // This is just to better position the table in the page:
  30. g.Transform = Matrix3x2.CreateTranslation(g.Resolution, g.Resolution);
  31.  
  32. var x = new float[] { 10, 70, 120, 170, 220, 270, 320, 370 };
  33. var y = new float[] { 10, 70, 90, 110, 130 };
  34. int lastX = x.Length - 1;
  35. int lastY = y.Length - 1;
  36.  
  37. int angle = -50;
  38. float dx = (y[0] - y[1]) / (float)Math.Tan(Math.PI * angle / 180);
  39.  
  40. using (var path = g.CreatePath())
  41. {
  42. path.BeginFigure(new PointF(x[1] + dx, y[0]));
  43. path.AddLine(new PointF(x[lastX] + dx, y[0]));
  44. path.AddLine(new PointF(x[lastX], y[1]));
  45. path.AddLine(new PointF(x[1], y[1]));
  46. path.EndFigure(FigureEnd.Closed);
  47. g.FillPath(path, Color.Bisque);
  48. }
  49. DrawLine(g, x[1] + dx, y[0], x[lastX] + dx, y[0]);
  50. for (int i = 1; i <= lastX; i++)
  51. DrawLine(g, x[i], y[1], x[i] + dx, y[0]);
  52.  
  53. var arrH = new string[] { "January", "February", "March", "April", "May", "June" };
  54. for (int i = 1; i < lastX; i++)
  55. DrawSlantedText(g, angle, x[i], y[0], x[i + 1], y[1], arrH[i - 1]);
  56.  
  57. for (int i = 1; i <= lastY; i++)
  58. DrawLine(g, x[0], y[i], x[lastX], y[i]);
  59. for (int i = 0; i <= lastX; i++)
  60. DrawLine(g, x[i], y[1], x[i], y[lastY]);
  61.  
  62. var arr1 = new string[] { "Store 1", "83424", "13558", "2348", "6429", "7565", "2833" };
  63. var arr2 = new string[] { "Store 2", "53423", "3573", "32321", "16474", "97553", "8364" };
  64. var arr3 = new string[] { "Store 3", "3421", "83553", "2343", "6428", "77557", "8347" };
  65.  
  66. bool bold = true;
  67. for (int i = 0; i < lastX; i++)
  68. {
  69. DrawText(g, y[1], x[i], x[i + 1], arr1[i], bold);
  70. DrawText(g, y[2], x[i], x[i + 1], arr2[i], bold);
  71. DrawText(g, y[3], x[i], x[i + 1], arr3[i], bold);
  72. bold = false;
  73. }
  74.  
  75. // Done:
  76. doc.Save(stream);
  77. return doc.Pages.Count;
  78. }
  79.  
  80. static void DrawLine(GcGraphics g, float x1, float y1, float x2, float y2)
  81. {
  82. g.DrawLine(new PointF(x1, y1), new PointF(x2, y2), new GCDRAW::Pen(Color.Gray, 1));
  83. }
  84.  
  85. static void DrawSlantedText(GcGraphics g, int angle, float x1, float y1, float x2, float y2, string s)
  86. {
  87. var tl = g.CreateTextLayout();
  88. tl.TextAlignment = TextAlignment.Center;
  89. tl.Append(s, new TextFormat
  90. {
  91. FontName = "Segoe UI",
  92. FontSize = 10,
  93. FontBold = true
  94. });
  95. var rc = new RectangleF(x1, y1, x2 - x1, y2 - y1);
  96. g.DrawSlantedText(tl, angle, false, rc, SlantedTextAlignment.CenterInsideOutside);
  97. }
  98.  
  99. static void DrawText(GcGraphics g, float y1, float x1, float x2, string s, bool bold = false)
  100. {
  101. var tl = g.CreateTextLayout();
  102. tl.TextAlignment = bold ? TextAlignment.Leading : TextAlignment.Trailing;
  103. tl.MaxWidth = x2 - x1 - 6;
  104. tl.Append(s, new TextFormat
  105. {
  106. FontName = "Segoe UI",
  107. FontSize = 10,
  108. FontBold = bold
  109. });
  110. g.DrawTextLayout(tl, new PointF(x1 + 3, y1));
  111. }
  112. }
  113. }
  114.