TableCellSpans.cs
  1. //
  2. // This code is part of Document Solutions for PDF 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.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Common;
  12. using GrapeCity.Documents.Drawing;
  13. using GrapeCity.Documents.Layout;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsPdfWeb.Demos
  18. {
  19. // This example shows how to draw a table with cells spanning multiple rows and columns,
  20. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  21. public class TableCellSpans
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. var doc = new GcPdfDocument();
  26. var g = doc.NewPage().Graphics;
  27.  
  28. DrawTable(g, g.CanvasSize.Width, g.CanvasSize.Height);
  29.  
  30. // Save the PDF:
  31. doc.Save(stream);
  32. return doc.Pages.Count;
  33. }
  34.  
  35. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  36. {
  37. var host = new LayoutHost();
  38. var view = host.CreateView(pageWidth, pageHeight);
  39.  
  40. var rt = view.CreateRect();
  41. rt.AnchorTopLeftRight(null, 30, 20, 20);
  42.  
  43. var ta = new TableRenderer(g,
  44. rt, FixedTableSides.TopLeftRight,
  45. rowCount: 5,
  46. columnCount: 4,
  47. gridLineColor: Color.CornflowerBlue,
  48. gridLineWidth: 5,
  49. rowMinHeight: 50,
  50. columnMinWidth: 120);
  51.  
  52. ta.ColumnRects[2].SetStarWidth(1f);
  53.  
  54. ta.DefaultCellStyle = new CellStyle
  55. {
  56. LineWidth = 1,
  57. LineColor = Color.Coral,
  58. LinePaddingAll = 2,
  59. PaddingAll = 5,
  60. TextFormat = new TextFormat
  61. {
  62. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
  63. FontSizeInGraphicUnits = true,
  64. FontSize = 16,
  65. }
  66. };
  67.  
  68. // AddCell()'s first 4 arguments are rowIndex, columnIndex, rowSpan, columnSpan:
  69. ta.AddCell(0, 0, 1, 1, "1 2 3 4 5 6 7 8 9 0.");
  70. 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 " +
  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.");
  73. 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 " +
  74. "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 " +
  75. "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 " +
  76. "7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0.");
  77. 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 " +
  78. "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0.");
  79. ta.AddCell(4, 1, 1, 1, "1 2 3 4 5.");
  80. ta.AddCell(3, 0, 2, 1);
  81.  
  82. ta.AddMissingCells();
  83.  
  84. ta.Render();
  85. }
  86. }
  87. }
  88.