GraphicsInTable.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 graphics drawings in cells,
  20. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  21. public class GraphicsInTable
  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. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  37. {
  38. var host = new LayoutHost();
  39. var view = host.CreateView(pageWidth, pageHeight);
  40.  
  41. var rt = view.CreateRect();
  42. rt.AnchorTopLeft(null, 36, 30);
  43.  
  44. var ta = new TableRenderer(g,
  45. rt, FixedTableSides.TopLeft,
  46. rowCount: 7,
  47. columnCount: 6,
  48. gridLineColor: Color.Black,
  49. gridLineWidth: 1);
  50.  
  51. ta.RowRects[0].SetHeight(50);
  52. ta.ColumnRects[0].SetWidth(80);
  53.  
  54. var cs = new CellStyle
  55. {
  56. TextAlignment = TextAlignment.Center,
  57. ParagraphAlignment = ParagraphAlignment.Center,
  58. TextFormat = new TextFormat
  59. {
  60. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibrib.ttf")),
  61. FontSize = 14,
  62. FontSizeInGraphicUnits = true,
  63. }
  64. };
  65.  
  66. // Background style for displaying "Shape" with a diagonal line:
  67. var csCornerTopRight = new CellStyle(cs)
  68. {
  69. Background = true,
  70. LineWidth = 1f,
  71. Borders = FrameBorders.MainDiagonal,
  72. TextAlignment = TextAlignment.Trailing,
  73. ParagraphAlignment = ParagraphAlignment.Near,
  74. PaddingRight = 10,
  75. PaddingTop = 5
  76. };
  77.  
  78. // Foreground style for displaying "Color" at the bottom left corner of the same cell:
  79. var csCornerBottomLeft = new CellStyle(cs)
  80. {
  81. TextAlignment = TextAlignment.Leading,
  82. ParagraphAlignment = ParagraphAlignment.Far,
  83. PaddingLeft = 10,
  84. PaddingBottom = 5
  85. };
  86.  
  87. // Add a background cell at the top left corner:
  88. ta.AddCell(csCornerTopRight, 0, 0, "Shape");
  89.  
  90. ta.AddCell(cs, 0, 1, "Circle");
  91. ta.AddCell(cs, 0, 2, "Triangle");
  92. ta.AddCell(cs, 0, 3, "Rectangle");
  93. ta.AddCell(cs, 0, 4, "Oval");
  94. ta.AddCell(cs, 0, 5, "Square");
  95.  
  96. // Add a foreground cell at the top left corner:
  97. ta.AddCell(csCornerBottomLeft, 0, 0, "Color");
  98.  
  99. ta.AddCell(cs, 1, 0, "Red");
  100. ta.AddCell(cs, 2, 0, "Green");
  101. ta.AddCell(cs, 3, 0, "Blue");
  102. ta.AddCell(cs, 4, 0, "Cyan");
  103. ta.AddCell(cs, 5, 0, "Magenta");
  104. ta.AddCell(cs, 6, 0, "Yellow");
  105.  
  106. ta.DefaultCellStyle = new CellStyle
  107. {
  108. PaddingTop = 3,
  109. PaddingLeftRight = 20,
  110. PaddingBottom = 55,
  111. FixedWidth = false,
  112. TextAlignment = TextAlignment.Center,
  113. TextFormat = new TextFormat
  114. {
  115. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibri.ttf")),
  116. FontSizeInGraphicUnits = true,
  117. FontSize = 14
  118. },
  119. CreateTextLayout = (g, cs, data) =>
  120. {
  121. var tl = g.CreateTextLayout();
  122. tl.Append(((Figure)data).Title, cs.TextFormat);
  123. return tl;
  124. },
  125. CustomDraw = (g, tc) =>
  126. {
  127. // A Figure can draw itself:
  128. ((Figure)tc.Data).Draw(g, tc.Width, tc.Height);
  129. }
  130. };
  131.  
  132. ta.AddCell(1, 1, new Figure("Red Circle", Shape.Circle, Color.Red));
  133. ta.AddCell(1, 2, new Figure("Red Triangle", Shape.Triangle, Color.Red));
  134. ta.AddCell(1, 3, new Figure("Red Rectangle", Shape.Rectangle, Color.Red));
  135. ta.AddCell(1, 4, new Figure("Red Oval", Shape.Oval, Color.Red));
  136. ta.AddCell(1, 5, new Figure("Red Square", Shape.Square, Color.Red));
  137.  
  138. ta.AddCell(2, 1, new Figure("Green Circle", Shape.Circle, Color.Green));
  139. ta.AddCell(2, 2, new Figure("Green Triangle", Shape.Triangle, Color.Green));
  140. ta.AddCell(2, 3, new Figure("Green Rectangle", Shape.Rectangle, Color.Green));
  141. ta.AddCell(2, 4, new Figure("Green Oval", Shape.Oval, Color.Green));
  142. ta.AddCell(2, 5, new Figure("Green Square", Shape.Square, Color.Green));
  143.  
  144. ta.AddCell(3, 1, new Figure("Blue Circle", Shape.Circle, Color.Blue));
  145. ta.AddCell(3, 2, new Figure("Blue Triangle", Shape.Triangle, Color.Blue));
  146. ta.AddCell(3, 3, new Figure("Blue Rectangle", Shape.Rectangle, Color.Blue));
  147. ta.AddCell(3, 4, new Figure("Blue Oval", Shape.Oval, Color.Blue));
  148. ta.AddCell(3, 5, new Figure("Blue Square", Shape.Square, Color.Blue));
  149.  
  150. ta.AddCell(4, 1, new Figure("Cyan Circle", Shape.Circle, Color.Cyan));
  151. ta.AddCell(4, 2, new Figure("Cyan Triangle", Shape.Triangle, Color.Cyan));
  152. ta.AddCell(4, 3, new Figure("Cyan Rectangle", Shape.Rectangle, Color.Cyan));
  153. ta.AddCell(4, 4, new Figure("Cyan Oval", Shape.Oval, Color.Cyan));
  154. ta.AddCell(4, 5, new Figure("Cyan Square", Shape.Square, Color.Cyan));
  155.  
  156. ta.AddCell(5, 1, new Figure("Magenta Circle", Shape.Circle, Color.Magenta));
  157. ta.AddCell(5, 2, new Figure("Magenta Triangle", Shape.Triangle, Color.Magenta));
  158. ta.AddCell(5, 3, new Figure("Magenta Rectangle", Shape.Rectangle, Color.Magenta));
  159. ta.AddCell(5, 4, new Figure("Magenta Oval", Shape.Oval, Color.Magenta));
  160. ta.AddCell(5, 5, new Figure("Magenta Square", Shape.Square, Color.Magenta));
  161.  
  162. ta.AddCell(6, 1, new Figure("Yellow Circle", Shape.Circle, Color.Yellow));
  163. ta.AddCell(6, 2, new Figure("Yellow Triangle", Shape.Triangle, Color.Yellow));
  164. ta.AddCell(6, 3, new Figure("Yellow Rectangle", Shape.Rectangle, Color.Yellow));
  165. ta.AddCell(6, 4, new Figure("Yellow Oval", Shape.Oval, Color.Yellow));
  166. ta.AddCell(6, 5, new Figure("Yellow Square", Shape.Square, Color.Yellow));
  167.  
  168. ta.Render();
  169. }
  170. enum Shape
  171. {
  172. Circle,
  173. Triangle,
  174. Rectangle,
  175. Oval,
  176. Square
  177. }
  178.  
  179. class Figure
  180. {
  181. public string Title;
  182. public Shape Shape;
  183. public Color Color;
  184.  
  185. public Figure(string title, Shape shape, Color color)
  186. {
  187. Title = title;
  188. Shape = shape;
  189. Color = color;
  190. }
  191.  
  192. public void Draw(GcGraphics g, float w, float h)
  193. {
  194. RectangleF rc;
  195. var pen = new GrapeCity.Documents.Drawing.Pen(Color.Black, 1);
  196. switch (Shape)
  197. {
  198. case Shape.Circle:
  199. rc = new RectangleF(w / 2 - 20, h - 50, 40, 40);
  200. g.FillEllipse(rc, Color);
  201. g.DrawEllipse(rc, pen);
  202. break;
  203. case Shape.Triangle:
  204. var points = new PointF[]
  205. {
  206. new PointF(w / 2, h - 50),
  207. new PointF(w / 2 + 25, h - 10),
  208. new PointF(w / 2 - 25, h - 10)
  209. };
  210. g.FillPolygon(points, Color);
  211. g.DrawPolygon(points, pen);
  212. break;
  213. case Shape.Rectangle:
  214. rc = new RectangleF(w / 2 - 35, h - 50, 70, 40);
  215. g.FillRectangle(rc, Color);
  216. g.DrawRectangle(rc, pen);
  217. break;
  218. case Shape.Oval:
  219. rc = new RectangleF(w / 2 - 35, h - 50, 70, 40);
  220. g.FillEllipse(rc, Color);
  221. g.DrawEllipse(rc, pen);
  222. break;
  223. case Shape.Square:
  224. rc = new RectangleF(w / 2 - 20, h - 50, 40, 40);
  225. g.FillRectangle(rc, Color);
  226. g.DrawRectangle(rc, pen);
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. }
  233.  
  234.