EmptyTable.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 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 simple empty table,
  20. // using the GrapeCity.Documents.Layout.Composition.Surface and
  21. // GrapeCity.Documents.Drawing.TableRenderer classes.
  22. // This example highlights the main elements of the table layout
  23. // that can be created using the TableRenderer class.
  24. public class EmptyTable
  25. {
  26. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  27. {
  28. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  29. using var g = bmp.CreateGraphics(Color.White);
  30. DrawTable(g, pixelSize.Width, pixelSize.Height);
  31. return bmp;
  32. }
  33.  
  34. static readonly TextFormat FormatDesc = new TextFormat()
  35. {
  36. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
  37. FontSize = 24,
  38. FontSizeInGraphicUnits = true,
  39. ForeColor = Color.White
  40. };
  41.  
  42. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  43. {
  44. var surf = new Surface();
  45. var view = surf.CreateView(pageWidth, pageHeight);
  46.  
  47. // Fill the page with Gray color.
  48. view.CreateVisual((g, v) => {
  49. g.FillRectangle(v.AsRectF(), Color.Gray);
  50. }).LayoutRect.AnchorExact(null);
  51.  
  52. var vTable = view.CreateVisual();
  53. var tableRect = vTable.LayoutRect;
  54. tableRect.AnchorTopLeftRight(null, 36, 36, 36);
  55.  
  56. // The paddingAll parameter adds padding to the table grid relative to the table rectangle.
  57. var ta = new TableRenderer(g,
  58. tableRect, FixedTableSides.TopLeftRight,
  59. rowCount: 3,
  60. columnCount: 3,
  61. gridLineColor: Color.Yellow,
  62. gridLineWidth: 5,
  63. rowMinHeight: 100,
  64. paddingAll: 40);
  65.  
  66. var columns = ta.ColumnRects;
  67. columns[0].SetStarWidth(1);
  68. columns[1].SetWidth(100);
  69. columns[2].SetStarWidth(3);
  70.  
  71. ta.TableFrameStyle = new FrameStyle
  72. {
  73. LinePaddingAll = 20,
  74. LineColor = Color.SlateBlue,
  75. FillColor = Color.LightGreen,
  76. LineWidth = 10f
  77. };
  78.  
  79. ta.ApplyCellConstraints();
  80.  
  81. vTable.Draw = (g, v) =>
  82. {
  83. var rc = v.AsRectF();
  84. g.FillRectangle(rc, Color.White);
  85.  
  86. var tl = g.CreateTextLayout();
  87. tl.AppendLine("Table rectangle (tableRect) is White,", FormatDesc);
  88. tl.AppendLine("Padding of the table frame is 20 (TableFrameStyle.LinePaddingAll),", FormatDesc);
  89. tl.AppendLine("Frame line width is 10 (TableFrameStyle.LineWidth),", FormatDesc);
  90. tl.AppendLine("Frame line color is SlateBlue (TableFrameStyle.LineColor),", FormatDesc);
  91. tl.AppendLine("Frame area color is LightGreen (TableFrameStyle.FillColor),", FormatDesc);
  92. tl.AppendLine("Padding of the table grid is 40 (the paddingAll parameter),", FormatDesc);
  93. tl.AppendLine("Table grid line width is 5 (the gridLineWidth parameter),", FormatDesc);
  94. tl.AppendLine("Grid line color is Yellow (the gridLineColor parameter).", FormatDesc);
  95. g.DrawTextLayout(tl, new PointF(0, rc.Bottom + 10));
  96.  
  97. g.Transform = v.Layer.Surface.BaseTransform;
  98. ta.Render();
  99. };
  100.  
  101. surf.Render(g);
  102. }
  103. }
  104. }
  105.