RotatedTable.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 System.Collections.Generic;
  10. using GrapeCity.Documents.Drawing;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Imaging;
  13. using GrapeCity.Documents.Layout;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsImagingWeb.Demos
  18. {
  19. // This example shows how to draw a rotated table using an angle constraint.
  20. // An angle constraint is similar to rotation with a transformation matrix,
  21. // but unlike transformation, using angle constraint makes it easier to add
  22. // other visual elements to the view.
  23. public class RotatedTable
  24. {
  25. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  26. {
  27. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  28. using var g = bmp.CreateGraphics(Color.White);
  29. DrawTable(g, pixelSize.Width, pixelSize.Height);
  30. return bmp;
  31. }
  32.  
  33. class ItemLine
  34. {
  35. public ItemLine(string text, double sales, double marginNumber, double marginPercent, int totalCustomers)
  36. {
  37. Text = text;
  38. Sales = sales;
  39. MarginNumber = marginNumber;
  40. MarginPercent = marginPercent;
  41. TotalCustomers = totalCustomers;
  42. }
  43. public string Text { get; }
  44. public double Sales { get; }
  45. public double MarginNumber { get; }
  46. public double MarginPercent { get; }
  47. public int TotalCustomers { get; }
  48. }
  49.  
  50. static void DrawTable(GcGraphics g, int pageWidth, int pageHeight)
  51. {
  52. float marginX = 20, marginY = 20;
  53. var host = new LayoutHost();
  54. var view = host.CreateView(0, 0);
  55.  
  56. // Note that the table rectangle is rotated using an AngleConstraint.
  57. // We don't need a separate View with a transformation matrix.
  58.  
  59. var rt = view.CreateRect();
  60. rt.SetAngle(null, 270);
  61. rt.SetTop(null, AnchorParam.Left, marginX);
  62. rt.SetRight(null, AnchorParam.Top, -marginY);
  63.  
  64. var ta = new TableRenderer(g,
  65. rt, FixedTableSides.TopRight,
  66. rowCount: 9,
  67. columnCount: 5,
  68. gridLineColor: Color.Black,
  69. gridLineWidth: 1,
  70. paddingLeft: 20,
  71. paddingRight: 20,
  72. paddingBottom: 20,
  73. paddingTop: 12);
  74.  
  75. var lgb = new LinearGradientBrush(Color.FromArgb(240, 234, 249), new PointF(0, 0), Color.FromArgb(201, 181, 232), new PointF(0, 1));
  76. ta.TableFrameStyle = new FrameStyle
  77. {
  78. LineColor = Color.FromArgb(126, 96, 160),
  79. LineWidth = 1,
  80. FillBrush = lgb
  81. };
  82.  
  83. var columns = ta.ColumnRects;
  84. columns[0].SetWidth(80);
  85. columns[1].SetWidth(125);
  86. columns[2].SetWidth(110);
  87. columns[3].SetWidth(80);
  88. columns[4].SetWidth(80);
  89.  
  90. var fmt = new TextFormat
  91. {
  92. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibri.ttf")),
  93. FontSize = 16,
  94. FontSizeInGraphicUnits = true,
  95. FontFeatures = new FontFeature[] { new FontFeature(FeatureTag.liga, false) }
  96. };
  97. var fmtHeader = new TextFormat(fmt)
  98. {
  99. ForeColor = Color.White,
  100. FontBold = true
  101. };
  102.  
  103. var csTitle = new CellStyle
  104. {
  105. Background = true,
  106. TextFormat = new TextFormat(fmt)
  107. {
  108. FontBold = true,
  109. FontSize = 20
  110. },
  111. TextAlignment = TextAlignment.Center,
  112. PaddingBottom = 8
  113. };
  114.  
  115. ta.AddCell(csTitle, 0, 0, 1, 5, "Class Schedule");
  116.  
  117. var csHeader = new CellStyle
  118. {
  119. TextFormat = fmtHeader,
  120. TextAlignment = TextAlignment.Center,
  121. PaddingTop = 5,
  122. PaddingBottom = 6
  123. };
  124. ta.AddCell(csHeader, 1, 0, "LESSON");
  125. ta.AddCell(csHeader, 1, 1, "TOPIC");
  126. ta.AddCell(csHeader, 1, 2, "ASSIGNMENT");
  127. ta.AddCell(csHeader, 1, 3, "POINTS");
  128. ta.AddCell(csHeader, 1, 4, "DUE");
  129.  
  130. var csCenter = new CellStyle(csHeader)
  131. {
  132. TextFormat = fmt,
  133. PaddingLeftRight = 6
  134. };
  135. var csLeft = new CellStyle(csCenter)
  136. {
  137. TextAlignment = TextAlignment.Leading
  138. };
  139.  
  140. ta.AddCell(csCenter, 2, 0, 2, 1, "1");
  141. ta.AddCell(csLeft, 2, 1, 2, 1, "What is Distance Learning?");
  142. ta.AddCell(csLeft, 2, 2, "Wiki #1");
  143. ta.AddCell(csCenter, 2, 3, "10");
  144. ta.AddCell(csCenter, 2, 4, "March 10");
  145. ta.AddCell(csLeft, 3, 2, "Presentation");
  146. ta.AddCell(csCenter, 3, 3, "20");
  147. ta.AddCell(csCenter, 3, 4);
  148.  
  149. ta.AddCell(csCenter, 4, 0, "2");
  150. ta.AddCell(csLeft, 4, 1, "History & Theories");
  151. ta.AddCell(csLeft, 4, 2, "Brief Paper");
  152. ta.AddCell(csCenter, 4, 3, "20");
  153. ta.AddCell(csCenter, 4, 4, "March 24");
  154.  
  155. ta.AddCell(csCenter, 5, 0, 1, 5, "Spring Break");
  156.  
  157. ta.AddCell(csCenter, 6, 0, 2, 1, "3");
  158. ta.AddCell(csLeft, 6, 1, 2, 1, "Distance Learners");
  159. ta.AddCell(csLeft, 6, 2, "Discussion #1");
  160. ta.AddCell(csCenter, 6, 3, "10");
  161. ta.AddCell(csCenter, 6, 4, "April 7");
  162. ta.AddCell(csLeft, 7, 2, "Group Project");
  163. ta.AddCell(csCenter, 7, 3, "50");
  164. ta.AddCell(csCenter, 7, 4, "April 14");
  165.  
  166. ta.AddCell(csCenter, 8, 0, "4");
  167. ta.AddCell(csLeft, 8, 1, "Media Selection");
  168. ta.AddCell(csLeft, 8, 2, "Blog #1");
  169. ta.AddCell(csCenter, 8, 3, "10");
  170. ta.AddCell(csCenter, 8, 4, "April 21");
  171.  
  172. ta.AddCell(new CellStyle
  173. {
  174. Background = true,
  175. FillColor = Color.FromArgb(79, 129, 189)
  176. }, 1, 0, 1, 5);
  177.  
  178. ta.AddCell(new CellStyle
  179. {
  180. Background = true,
  181. FillColor = Color.FromArgb(208, 216, 232)
  182. }, 2, 0, 7, 5);
  183.  
  184. var bkHighlight = new CellStyle
  185. {
  186. Background = true,
  187. FillColor = Color.FromArgb(233, 237, 244)
  188. };
  189. ta.AddCell(bkHighlight, 3, 2, 1, 3);
  190. ta.AddCell(bkHighlight, 5, 0, 1, 5);
  191. ta.AddCell(bkHighlight, 7, 2, 1, 3);
  192.  
  193. ta.ApplyCellConstraints();
  194.  
  195. g.Transform = Matrix3x2.CreateScale(1.8f);
  196. ta.Render(g);
  197. }
  198. }
  199. }
  200.