RedNegativesTable.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 System.Collections.Generic;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Drawing;
  12. using GrapeCity.Documents.Text;
  13. using GrapeCity.Documents.Imaging;
  14. using GrapeCity.Documents.Layout;
  15. using GCTEXT = GrapeCity.Documents.Text;
  16. using GCDRAW = GrapeCity.Documents.Drawing;
  17.  
  18. namespace DsPdfWeb.Demos
  19. {
  20. // This example shows how to draw a simple table containing
  21. // some numeric data, with negative values displayed in red,
  22. // and with differently shaded odd and even rows,
  23. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  24. public class RedNegativesTable
  25. {
  26. public int CreatePDF(Stream stream)
  27. {
  28. var doc = new GcPdfDocument();
  29. var p = doc.NewPage();
  30. p.Size = new SizeF(p.Size.Height, p.Size.Width);
  31. var g = p.Graphics;
  32.  
  33. DrawTable(g, g.CanvasSize.Width, g.CanvasSize.Height);
  34.  
  35. // Save the PDF:
  36. doc.Save(stream);
  37. return doc.Pages.Count;
  38. }
  39.  
  40. class ItemLine
  41. {
  42. public ItemLine(string text, double sales, double marginNumber, double marginPercent, int totalCustomers)
  43. {
  44. Text = text;
  45. Sales = sales;
  46. MarginNumber = marginNumber;
  47. MarginPercent = marginPercent;
  48. TotalCustomers = totalCustomers;
  49. }
  50. public string Text { get; }
  51. public double Sales { get; }
  52. public double MarginNumber { get; }
  53. public double MarginPercent { get; }
  54. public int TotalCustomers { get; }
  55. }
  56.  
  57. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  58. {
  59. var items = new ItemLine[]
  60. {
  61. new ItemLine("Washers & Dryers", 1580, 196.82, 12.46, 247),
  62. new ItemLine("Projectors & Screens", 973, 211.26, 21.72, 382),
  63. new ItemLine("Refrigerators", 953, -350.57, -36.77, 331),
  64. new ItemLine("Laptops", 668, 181.17, 27.11, 301),
  65. new ItemLine("Coffee Machines", 476, -11.15, -2.34, 251),
  66. new ItemLine("Lamps", 471, 340.31, 72.28, 548),
  67. new ItemLine("Water Heaters", 443, 94.59, 21.34, 152),
  68. new ItemLine("Monitors", 424, -0.77, -0.18, 327),
  69. new ItemLine("Microwaves", 319, 14.30, 4.48, 375),
  70. new ItemLine("Desktops", 266, 23.22, 8.72, 184),
  71. new ItemLine("Air Conditioners", 253, 137.82, 54.54, 216),
  72. new ItemLine("Printers & Scanners", 219, 0.59, 0.27, 379),
  73. new ItemLine("Computers Accessories", 128, -18.23, -14.27, 654),
  74. new ItemLine("Fans", 83, -38.64, -46.35, 287)
  75. };
  76.  
  77. var host = new LayoutHost();
  78. var view = host.CreateView(pageWidth, pageHeight);
  79.  
  80. var rt = view.CreateRect();
  81. rt.AnchorTopLeft(null, 72, 72);
  82.  
  83. var ta = new TableRenderer(g,
  84. rt, FixedTableSides.TopLeft,
  85. rowCount: items.Length + 1,
  86. columnCount: 5,
  87. gridLineColor: Color.Transparent,
  88. gridLineWidth: 0);
  89.  
  90. var columns = ta.ColumnRects;
  91. columns[0].SetWidth(200);
  92. columns[1].SetWidth(120);
  93. columns[2].SetWidth(120);
  94. columns[3].SetWidth(120);
  95. columns[4].SetWidth(120);
  96.  
  97. var fmt = new TextFormat
  98. {
  99. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "consola.ttf")),
  100. ForeColor = Color.FromArgb(38, 38, 38),
  101. FontSize = 14,
  102. FontSizeInGraphicUnits = true,
  103. };
  104. var fmtSmall = new TextFormat(fmt)
  105. {
  106. FontSize = 11
  107. };
  108.  
  109. var csCaption = new CellStyle
  110. {
  111. TextAlignment = TextAlignment.Trailing,
  112. ParagraphAlignment = ParagraphAlignment.Center,
  113. TextFormat = fmt,
  114. PaddingBottom = 10,
  115. PaddingRight = 4
  116. };
  117. ta.AddCell(new CellStyle(csCaption)
  118. {
  119. CustomDraw = (g, tc) =>
  120. {
  121. float y = tc.Height * 0.5f;
  122. float x = 10;
  123. var pen = new GCDRAW.Pen(Color.DimGray, 1);
  124. g.DrawLine(new PointF(x, y - 7), new PointF(x, y + 7), pen);
  125. g.DrawLine(new PointF(x - 3, y + 3), new PointF(x, y + 7), pen);
  126. g.DrawLine(new PointF(x + 3, y + 3), new PointF(x, y + 7), pen);
  127. },
  128. CreateTextLayout = (g, cs, data) =>
  129. {
  130. var tl = g.CreateTextLayout();
  131. tl.AppendLine("Sales", fmt);
  132. tl.Append("in Thousands", fmtSmall);
  133. return tl;
  134. }
  135. }, 0, 1, null);
  136. ta.AddCell(new CellStyle(csCaption)
  137. {
  138. CreateTextLayout = (g, cs, data) =>
  139. {
  140. var tl = g.CreateTextLayout();
  141. tl.AppendLine("Margin", fmt);
  142. tl.Append("in Thousands", fmtSmall);
  143. return tl;
  144. }
  145. }, 0, 2, null);
  146. ta.AddCell(csCaption, 0, 3, "Margin %");
  147. ta.AddCell(csCaption, 0, 4, "Total\nCustomers");
  148.  
  149. var csName = new CellStyle
  150. {
  151. TextFormat = fmt,
  152. PaddingTopBottom = 5,
  153. PaddingLeft = 4
  154. };
  155. var csNum = new CellStyle
  156. {
  157. TextFormat = fmt,
  158. TextAlignment = TextAlignment.Trailing,
  159. PaddingTopBottom = 5,
  160. PaddingRight = 4
  161. };
  162. var csNumNegative = new CellStyle(csNum)
  163. {
  164. TextFormat = new TextFormat(fmt)
  165. {
  166. ForeColor = Color.Red
  167. }
  168. };
  169. var csBand = new CellStyle
  170. {
  171. FillColor = Color.FromArgb(244, 241, 250),
  172. Background = true
  173. };
  174.  
  175. var ci = new System.Globalization.CultureInfo("en-US");
  176.  
  177. for (int i = 0; i < items.Length; i++)
  178. {
  179. var item = items[i];
  180. int rowIndex = i + 1;
  181.  
  182. if ((i & 1) != 0)
  183. {
  184. ta.AddCell(csBand, rowIndex, 0, 1, 5);
  185. }
  186. ta.AddCell(csName, rowIndex, 0, item.Text);
  187. ta.AddCell(csNum, rowIndex, 1, item.Sales.ToString("c0", ci));
  188. var v = item.MarginNumber;
  189. ta.AddCell(v >= 0.0 ? csNum : csNumNegative, rowIndex, 2, v.ToString("n2"));
  190. v = item.MarginPercent;
  191. ta.AddCell(v >= 0.0 ? csNum : csNumNegative, rowIndex, 3, v.ToString("n2"));
  192. ta.AddCell(csNum, rowIndex, 4, item.TotalCustomers.ToString("n0"));
  193. }
  194.  
  195. ta.SetVerticalGridLineWidth(1, 2);
  196. ta.SetHorizontalGridLineWidth(1, 2);
  197.  
  198. var pen = new GCDRAW.Pen(Color.FromArgb(159, 146, 213), 2)
  199. {
  200. DashStyle = DashStyle.Solid,
  201. LineCap = PenLineCap.Round
  202. };
  203. ta.AddCell(new CellStyle
  204. {
  205. Background = true,
  206. Borders = FrameBorders.LeftBorder | FrameBorders.TopBorder,
  207. Pen = pen,
  208. LinePaddingLeft = -2,
  209. LinePaddingTop = -2,
  210. }, 1, 1, items.Length, 4);
  211.  
  212. ta.Render();
  213. }
  214. }
  215. }
  216.