TableNoSides.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 simple table
  19. // with a grid that has no left and right borders,
  20. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  21. public class TableNoSides
  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. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  32. {
  33. var host = new LayoutHost();
  34. var view = host.CreateView(pageWidth, pageHeight);
  35.  
  36. var rt = view.CreateRect();
  37. rt.AnchorTopLeftRight(null, 30, 20, 20);
  38.  
  39. var ta = new TableRenderer(g,
  40. rt, FixedTableSides.TopLeftRight,
  41. rowCount: 17,
  42. columnCount: 4,
  43. gridLineColor: Color.SlateBlue,
  44. gridLineWidth: 2,
  45. rowMinHeight: 20);
  46.  
  47. var columns = ta.ColumnRects;
  48. columns[0].SetWidth(40);
  49. columns[1].SetStarWidth(150);
  50. columns[2].SetStarWidth(250);
  51. columns[3].SetStarWidth(250);
  52.  
  53. ta.SetVerticalGridLineWidth(0, 0);
  54. ta.SetVerticalGridLineWidth(4, 0);
  55.  
  56. ta.SetHorizontalGridLineWidth(0, 5);
  57. ta.SetHorizontalGridLineWidth(1, 5);
  58. ta.SetHorizontalGridLineWidth(17, 5);
  59.  
  60. var fmt1 = new TextFormat
  61. {
  62. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibri.ttf")),
  63. FontSize = 16f,
  64. FontSizeInGraphicUnits = true
  65. };
  66. var cs = new CellStyle
  67. {
  68. PaddingTop = 2,
  69. PaddingBottom = 5,
  70. PaddingLeftRight = 10,
  71. TextFormat = fmt1,
  72. TextAlignment = TextAlignment.Trailing
  73. };
  74. var csBold = new CellStyle(cs)
  75. {
  76. TextAlignment = TextAlignment.Center,
  77. TextFormat = new TextFormat(fmt1)
  78. {
  79. FontBold = true
  80. }
  81. };
  82. var csText = new CellStyle(cs)
  83. {
  84. TextAlignment = TextAlignment.Leading,
  85. };
  86.  
  87. ta.AddCell(csBold, 0, 0, "#");
  88. ta.AddCell(csBold, 0, 1, "Name");
  89. ta.AddCell(csBold, 0, 2, "Value");
  90. ta.AddCell(csBold, 0, 3, "Comment");
  91. for (int i = 1; i <= 16; i++)
  92. {
  93. ta.AddCell(cs, i, 0, $"{i}");
  94. ta.AddCell(csText, i, 1, $"Math.Sqrt({i})");
  95. ta.AddCell(cs, i, 2, Math.Sqrt(i).ToString());
  96. ta.AddCell(csText, i, 3, $"Square root of {i}");
  97. }
  98.  
  99. ta.Render();
  100. }
  101. }
  102. }
  103.