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