OddEvenRows.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.Numerics;
  9. using GrapeCity.Documents.Drawing;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Imaging;
  12. using GrapeCity.Documents.Layout;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15.  
  16. namespace DsImagingWeb.Demos
  17. {
  18. // This example shows how to draw a table with differently shaded odd and even rows,
  19. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  20. public class OddEvenRows
  21. {
  22. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  25. using var g = bmp.CreateGraphics(Color.White);
  26. g.Transform = Matrix3x2.CreateTranslation(-pixelSize.Width / 2, 0) * Matrix3x2.CreateScale(2);
  27. DrawTable(g, pixelSize.Width, pixelSize.Height);
  28. g.Transform = Matrix3x2.Identity;
  29. return bmp;
  30. }
  31.  
  32. // Data source for the table:
  33. static readonly Team[] _teams = new Team[]
  34. {
  35. new Team("England"),
  36. new Team("France"),
  37. new Team("Ireland"),
  38. new Team("Italy"),
  39. new Team("Scotland"),
  40. new Team("Wales"),
  41. };
  42.  
  43. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  44. {
  45. var host = new LayoutHost();
  46. var view = host.CreateView(pageWidth, pageHeight);
  47.  
  48. var rt = view.CreateRect();
  49. rt.AnchorTopRight(null, 36, 36);
  50.  
  51. var ta = new TableRenderer(g,
  52. rt, FixedTableSides.TopRight,
  53. rowCount: _teams.Length + 2,
  54. columnCount: 11,
  55. gridLineColor: Color.FromArgb(173, 223, 252),
  56. gridLineWidth: 1,
  57. rowMinHeight: 10,
  58. columnMinWidth: 10);
  59.  
  60. var fmt = new TextFormat
  61. {
  62. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "tahoma.ttf")),
  63. FontSize = 18,
  64. FontSizeInGraphicUnits = true
  65. };
  66. var cs = new CellStyle
  67. {
  68. TextFormat = fmt,
  69. FixedWidth = false,
  70. PaddingAll = 4
  71. };
  72. var csHeaderH = new CellStyle(cs)
  73. {
  74. TextFormat = new TextFormat(fmt)
  75. {
  76. ForeColor = Color.White,
  77. FontBold = true
  78. },
  79. FillColor = Color.FromArgb(17, 93, 140),
  80. TextAlignment = TextAlignment.Center,
  81. ParagraphAlignment = ParagraphAlignment.Center,
  82. };
  83. var csHeaderV = new CellStyle(csHeaderH)
  84. {
  85. RotationAngle = 270,
  86. TextAlignment = TextAlignment.Leading,
  87. PaddingLeft = 3
  88. };
  89. var csNumber = new CellStyle(cs)
  90. {
  91. TextAlignment = TextAlignment.Center
  92. };
  93. var csNation = new CellStyle(cs)
  94. {
  95. TextFormat = new TextFormat(fmt)
  96. {
  97. ForeColor = Color.FromArgb(50, 123, 197)
  98. },
  99. };
  100.  
  101. // add cells to the header
  102. ta.AddCell(csHeaderV, 0, 0, 2, 1, "Position");
  103. ta.AddCell(csHeaderH, 0, 1, 2, 1, "Nation");
  104. ta.AddCell(csHeaderH, 0, 2, 1, 4, "Games");
  105. ta.AddCell(csHeaderV, 1, 2, "Played");
  106. ta.AddCell(csHeaderV, 1, 3, "Won");
  107. ta.AddCell(csHeaderV, 1, 4, "Drawn");
  108. ta.AddCell(csHeaderV, 1, 5, "Lost");
  109. ta.AddCell(csHeaderH, 0, 6, 1, 4, "Points");
  110. ta.AddCell(csHeaderV, 1, 6, "For");
  111. ta.AddCell(csHeaderV, 1, 7, "Against");
  112. ta.AddCell(csHeaderV, 1, 8, "Difference");
  113. ta.AddCell(csHeaderV, 1, 9, "Tries");
  114. ta.AddCell(csHeaderH, 0, 10, 2, 1, "Table\npoints");
  115.  
  116. // add the data cells
  117. for (int i = 0; i < _teams.Length; i++)
  118. {
  119. int rowIndex = i + 2;
  120. var team = _teams[i];
  121. ta.AddCell(csNumber, rowIndex, 0, $"{i + 1}");
  122. ta.AddCell(csNation, rowIndex, 1, team.Nation);
  123. ta.AddCell(csNumber, rowIndex, 2, $"{team.Played}");
  124. ta.AddCell(csNumber, rowIndex, 3, $"{team.Won}");
  125. ta.AddCell(csNumber, rowIndex, 4, $"{team.Drawn}");
  126. ta.AddCell(csNumber, rowIndex, 5, $"{team.Lost}");
  127. ta.AddCell(csNumber, rowIndex, 6, $"{team.For}");
  128. ta.AddCell(csNumber, rowIndex, 7, $"{team.Against}");
  129. ta.AddCell(csNumber, rowIndex, 8, $"{team.Diff}");
  130. ta.AddCell(csNumber, rowIndex, 9, $"{team.Tries}");
  131. ta.AddCell(csNumber, rowIndex, 10, $"{team.TablePoints}");
  132. }
  133.  
  134. // change background for odd rows
  135. ta.DefaultCellStyle = new CellStyle
  136. {
  137. Background = true,
  138. FillColor = Color.FromArgb(238, 238, 238)
  139. };
  140. for (int i = 0; i < _teams.Length; i += 2)
  141. {
  142. ta.AddCell(i + 2, 0, 1, 11);
  143. }
  144.  
  145. ta.Render();
  146. }
  147.  
  148. // A class to store example data:
  149. class Team
  150. {
  151. static readonly Random _rnd = Common.Util.NewRandom();
  152.  
  153. public string Nation;
  154. public int Played, Won, Drawn, Lost;
  155. public int For, Against, Diff, Tries;
  156. public int TablePoints;
  157.  
  158. internal Team(string nation)
  159. {
  160. Nation = nation;
  161. Played = _rnd.Next(0, 50);
  162. Won = _rnd.Next(0, 50);
  163. Drawn = _rnd.Next(0, 50);
  164. Lost = _rnd.Next(0, 50);
  165. For = _rnd.Next(0, 50);
  166. Against = _rnd.Next(0, 50);
  167. Diff = _rnd.Next(0, 50);
  168. Tries = _rnd.Next(0, 50);
  169. TablePoints = _rnd.Next(0, 150);
  170. }
  171. }
  172. }
  173. }
  174.