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