TableRoundBorders.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 rounded
  19. // table and cell borders,
  20. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  21. public class TableRoundBorders
  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, 36, 36, 36);
  38.  
  39. var ta = new TableRenderer(g,
  40. rt, FixedTableSides.TopLeftRight,
  41. rowCount: 5,
  42. columnCount: 4,
  43. gridLineColor: Color.Transparent,
  44. gridLineWidth: 1,
  45. rowMinHeight: 30,
  46. paddingAll: 3)
  47. {
  48. TableFrameStyle = new FrameStyle
  49. {
  50. FillColor = Color.AliceBlue,
  51. LineColor = Color.CornflowerBlue,
  52. LineWidth = 1,
  53. CornerRadius = 5
  54. }
  55. };
  56.  
  57. var columns = ta.ColumnRects;
  58. columns[0].SetStarWidth(1);
  59. columns[1].SetStarWidth(5);
  60. columns[2].SetStarWidth(2);
  61. columns[3].SetStarWidth(3);
  62.  
  63. var fmt = new TextFormat
  64. {
  65. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibri.ttf")),
  66. ForeColor = Color.CornflowerBlue,
  67. FontSize = 16,
  68. FontSizeInGraphicUnits = true
  69. };
  70. var csNormal = new CellStyle
  71. {
  72. TextFormat = fmt,
  73. ParagraphAlignment = ParagraphAlignment.Center,
  74. PaddingLeftRight = 10,
  75. FillColor = Color.MistyRose,
  76. LineColor = Color.CornflowerBlue,
  77. LinePaddingAll = 2,
  78. LineWidth = 1,
  79. CornerRadius = 5
  80. };
  81. var csCenter = new CellStyle(csNormal)
  82. {
  83. TextAlignment = TextAlignment.Center,
  84. PaddingLeftRight = 0,
  85. };
  86. var csHeader = new CellStyle(csCenter)
  87. {
  88. TextFormat = new TextFormat(fmt)
  89. {
  90. ForeColor = Color.White,
  91. FontBold = true
  92. },
  93. FillColor = Color.LightBlue
  94. };
  95.  
  96. ta.AddCell(csHeader, 0, 0, "#");
  97. ta.AddCell(csHeader, 0, 1, "Name");
  98. ta.AddCell(csHeader, 0, 2, "Age");
  99. ta.AddCell(csHeader, 0, 3, "Country");
  100.  
  101. ta.AddCell(csCenter, 1, 0, "1.");
  102. ta.AddCell(csNormal, 1, 1, "Alice");
  103. ta.AddCell(csCenter, 1, 2, "25");
  104. ta.AddCell(csNormal, 1, 3, "Spain");
  105.  
  106. ta.AddCell(csCenter, 2, 0, "2.");
  107. ta.AddCell(csNormal, 2, 1, "Bob");
  108. ta.AddCell(csCenter, 2, 2, "36");
  109. ta.AddCell(csNormal, 2, 3, "Germany");
  110.  
  111. ta.AddCell(csCenter, 3, 0, "3.");
  112. ta.AddCell(csNormal, 3, 1, "Ken");
  113. ta.AddCell(csCenter, 3, 2, "5");
  114. ta.AddCell(csNormal, 3, 3, "Brazil");
  115.  
  116. ta.AddCell(csCenter, 4, 0, "4.");
  117. ta.AddCell(csNormal, 4, 1, "Teddy");
  118. ta.AddCell(csCenter, 4, 2, "12");
  119. ta.AddCell(csNormal, 4, 3, "Mexico");
  120.  
  121. ta.ApplyCellConstraints();
  122.  
  123. ta.Render();
  124. }
  125. }
  126. }
  127.