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