TableCellSpans.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 cells spanning multiple rows and columns,
  19. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  20. public class TableCellSpans
  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.AnchorTopLeftRight(null, 30, 20, 20);
  37.  
  38. var ta = new TableRenderer(g,
  39. rt, FixedTableSides.TopLeftRight,
  40. rowCount: 5,
  41. columnCount: 4,
  42. gridLineColor: Color.CornflowerBlue,
  43. gridLineWidth: 5,
  44. rowMinHeight: 50,
  45. columnMinWidth: 120);
  46.  
  47. // We set a "star" (weighted) width for the third column so that the table
  48. // can expand since both the left and right sides of the table are fixed.
  49. ta.ColumnRects[2].SetStarWidth(1f);
  50.  
  51. ta.DefaultCellStyle = new CellStyle
  52. {
  53. LineWidth = 1,
  54. LineColor = Color.Coral,
  55. LinePaddingAll = 2,
  56. PaddingAll = 5,
  57. TextFormat = new TextFormat
  58. {
  59. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
  60. FontSizeInGraphicUnits = true,
  61. FontSize = 16,
  62. }
  63. };
  64.  
  65. // AddCell()'s first 4 arguments are rowIndex, columnIndex, rowSpan, columnSpan:
  66. ta.AddCell(0, 0, 1, 1, "1 2 3 4 5 6 7 8 9 0.");
  67. ta.AddCell(0, 1, 2, 2, "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 " +
  68. "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 " +
  69. "4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0.");
  70. ta.AddCell(2, 2, 3, 1, "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 " +
  71. "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 " +
  72. "4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 " +
  73. "7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0.");
  74. ta.AddCell(1, 3, 3, 1, "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 " +
  75. "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0.");
  76. ta.AddCell(4, 1, 1, 1, "1 2 3 4 5.");
  77. ta.AddCell(3, 0, 2, 1);
  78.  
  79. ta.AddMissingCells();
  80.  
  81. ta.Render();
  82. }
  83. }
  84. }
  85.