TimeChartTable.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 representing a time chart,
  20. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  21. public class TimeChartTable
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. var doc = new GcPdfDocument();
  26. var p = doc.Pages.Add(new SizeF(doc.PageSize.Height, doc.PageSize.Width));
  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. class Range
  37. {
  38. public Range(double valueLow, double deltaLow, double valueHigh, double deltaHigh)
  39. {
  40. ValueLow = valueLow;
  41. DeltaLow = deltaLow;
  42. ValueHigh = valueHigh;
  43. DeltaHigh = deltaHigh;
  44. }
  45. public double ValueLow { get; }
  46. public double DeltaLow { get; }
  47. public double ValueHigh { get; }
  48. public double DeltaHigh { get; }
  49. }
  50.  
  51. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  52. {
  53. var host = new LayoutHost();
  54. var view = host.CreateView(pageWidth, pageHeight * 0.7f);
  55.  
  56. var rt = view.CreateRect();
  57.  
  58. // Pad the table rectangle equally from left, right, top and bottom:
  59. rt.AnchorDeflate(null, 10);
  60.  
  61. // All sides of the table are fixed, so we can apply
  62. // star widths to columns and star heights to rows:
  63. var ta = new TableRenderer(g,
  64. rt, FixedTableSides.All,
  65. rowCount: 10, columnCount: 32,
  66. gridLineColor: Color.DimGray,
  67. gridLineWidth: 1);
  68.  
  69. var columns = ta.ColumnRects;
  70. columns[0].SetWidth(120);
  71. for (int i = 1; i < 32; i++)
  72. {
  73. columns[i].SetStarWidth(1f);
  74. }
  75.  
  76. // Table header is a part of the table;
  77. // the first row (rows[0]) is for the table header:
  78. var rows = ta.RowRects;
  79. rows[0].SetHeight(40);
  80. rows[1].SetHeight(30);
  81. for (int i = 2; i < 10; i++)
  82. {
  83. rows[i].SetStarHeight(1f);
  84. }
  85.  
  86. var fmt = new TextFormat
  87. {
  88. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
  89. FontSize = 16,
  90. FontSizeInGraphicUnits = true
  91. };
  92. var cs = new CellStyle
  93. {
  94. TextFormat = fmt,
  95. TextAlignment = TextAlignment.Center
  96. };
  97.  
  98. var csTitle = new CellStyle(cs)
  99. {
  100. Background = true,
  101. TextFormat = new TextFormat(fmt)
  102. {
  103. FontSize = 16,
  104. FontBold = true
  105. }
  106. };
  107. ta.AddCell(csTitle, 0, 0, 1, 32, "Small Business Marketing Plan");
  108.  
  109. ta.DefaultCellStyle = new CellStyle(cs)
  110. {
  111. ParagraphAlignment = ParagraphAlignment.Center
  112. };
  113. for (int i = 1; i < 32; i++)
  114. {
  115. ta.AddCell(1, i, i.ToString());
  116. }
  117.  
  118. ta.DefaultCellStyle = new CellStyle(cs)
  119. {
  120. TextAlignment = TextAlignment.Leading,
  121. PaddingLeft = 3
  122. };
  123. ta.AddCell(2, 0, "Business Overview");
  124. ta.AddCell(3, 0, "Market Analysis");
  125. ta.AddCell(4, 0, "Marketing Strategy");
  126. ta.AddCell(5, 0, "Operations Plan");
  127. ta.AddCell(6, 0, "Organization");
  128. ta.AddCell(7, 0, "Management");
  129. ta.AddCell(8, 0, "Legal aspects");
  130. ta.AddCell(9, 0, "Financial Plan");
  131.  
  132. // Add merged background cells for color highlighting:
  133. AddBand(2, 1, 6, Color.FromArgb(247, 202, 171));
  134. AddBand(3, 2, 7, Color.FromArgb(179, 198, 231));
  135. AddBand(4, 5, 6, Color.FromArgb(255, 229, 154));
  136. AddBand(5, 11, 6, Color.FromArgb(45, 116, 182));
  137. AddBand(6, 17, 6, Color.FromArgb(255, 155, 155));
  138. AddBand(7, 18, 6, Color.FromArgb(197, 224, 179));
  139. AddBand(8, 24, 4, Color.FromArgb(3, 174, 80));
  140. AddBand(9, 28, 4, Color.FromArgb(1, 176, 241));
  141.  
  142. void AddBand(int rowIndex, int columnIndex, int columnCount, Color color)
  143. {
  144. ta.AddCell(new CellStyle
  145. {
  146. Background = true,
  147. FillColor = color
  148. }, rowIndex, columnIndex, 1, columnCount);
  149. }
  150.  
  151. // We exclude the first row when we add missing cells
  152. // to avoid drawing grid lines for the table header:
  153. ta.AddMissingCells(1, 0, 9, 32);
  154.  
  155. ta.Render();
  156. }
  157. }
  158. }
  159.