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