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