PlainTable.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.Composition;
  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 plain table with random data,
  20. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  21. public class PlainTable
  22. {
  23. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  24. {
  25. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  26. using var g = bmp.CreateGraphics(Color.White);
  27. DrawTable(g, pixelSize.Width, pixelSize.Height);
  28. return bmp;
  29. }
  30.  
  31. class Record
  32. {
  33. public Record(string name, int take1, int take2, int take3)
  34. {
  35. Name = name;
  36. Take1 = take1;
  37. Take2 = take2;
  38. Take3 = take3;
  39. }
  40. public string Name { get; }
  41. public int Take1 { get; }
  42. public int Take2 { get; }
  43. public int Take3 { get; }
  44. }
  45.  
  46. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  47. {
  48. var rnd = Common.Util.NewRandom();
  49. var records = new List<Record>();
  50. for (int i = 0; i < 10; ++i)
  51. records.Add(new Record(Common.Util.LoremIpsum(1, 1, 1, 1, 4), rnd.Next(100), rnd.Next(100), rnd.Next(100)));
  52.  
  53. // The Surface object helps with calculating layout
  54. // of the table and drawing the table on a graphics.
  55. var surf = new Surface();
  56. var view = surf.CreateView(pageWidth, pageHeight);
  57. var visual = view.CreateVisual();
  58.  
  59. // The table is always contained within a LayoutRect which is passed to the TableRenderer constructor.
  60. var rt = visual.LayoutRect;
  61. rt.AnchorTopLeft(null, 36, 36);
  62.  
  63. var ta = new TableRenderer(g,
  64. rt, FixedTableSides.TopLeft,
  65. rowCount: records.Count + 1,
  66. columnCount: 4,
  67. gridLineColor: Color.DarkGray,
  68. gridLineWidth: 1,
  69. rowMinHeight: 10);
  70.  
  71. var cs = new CellStyle
  72. {
  73. PaddingTopBottom = 3,
  74. PaddingLeftRight = 4,
  75. FixedWidth = false,
  76. TextFormat = new TextFormat
  77. {
  78. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibri.ttf")),
  79. FontSize = 21,
  80. FontSizeInGraphicUnits = true
  81. }
  82. };
  83. var csHeader = new CellStyle(cs)
  84. {
  85. PaddingRight = 9,
  86. ParagraphAlignment = ParagraphAlignment.Center,
  87. TextAlignment = TextAlignment.Center,
  88. };
  89. var csName = new CellStyle(cs)
  90. {
  91. MaxWidth = pageWidth / 2,
  92. };
  93. var csNumber = new CellStyle(cs)
  94. {
  95. TextAlignment = TextAlignment.Trailing
  96. };
  97.  
  98. ta.DefaultCellStyle = csHeader;
  99. ta.AddCell(0, 0, "Name");
  100. ta.AddCell(0, 1, "Take 1\nThe Good");
  101. ta.AddCell(0, 2, "Take 2\nThe Bad");
  102. ta.AddCell(0, 3, "Take 3\nThe Ugly");
  103.  
  104. ta.DefaultCellStyle = csNumber;
  105. for (int i = 0; i < records.Count; i++)
  106. {
  107. var p = records[i];
  108. int r = i + 1;
  109. ta.AddCell(csName, r, 0, p.Name);
  110. ta.AddCell(r, 1, p.Take1.ToString());
  111. ta.AddCell(r, 2, p.Take2.ToString());
  112. ta.AddCell(r, 3, p.Take3.ToString());
  113. }
  114.  
  115. // Applying layout to the table cells must precede drawing the Surface.
  116. ta.ApplyCellConstraints();
  117.  
  118. visual.Draw = (g, v) =>
  119. {
  120. // Move from Visual transform to Surface transform before drawing the table.
  121. g.Transform = v.Layer.Surface.BaseTransform;
  122. ta.Render();
  123. };
  124.  
  125. surf.Render(g);
  126. }
  127. }
  128. }
  129.