PlainTable.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 System.Collections.Generic;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Common;
  13. using GrapeCity.Documents.Drawing;
  14. using GrapeCity.Documents.Layout;
  15. using GCTEXT = GrapeCity.Documents.Text;
  16. using GCDRAW = GrapeCity.Documents.Drawing;
  17.  
  18. namespace DsPdfWeb.Demos
  19. {
  20. // This example shows how to draw a plain table with random data,
  21. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  22. public class PlainTable
  23. {
  24. public int CreatePDF(Stream stream)
  25. {
  26. var doc = new GcPdfDocument();
  27. var p = doc.NewPage();
  28. p.Size = new SizeF(p.Size.Height, p.Size.Width);
  29. var g = p.Graphics;
  30.  
  31. DrawTable(g, g.CanvasSize.Width, g.CanvasSize.Height);
  32.  
  33. // Save the PDF:
  34. doc.Save(stream);
  35. return doc.Pages.Count;
  36. }
  37.  
  38. // The class to store sample data:
  39. class Record(string name, int take1, int take2, int take3)
  40. {
  41. public string Name { get; } = name;
  42. public int Take1 { get; } = take1;
  43. public int Take2 { get; } = take2;
  44. public int Take3 { get; } = take3;
  45. }
  46.  
  47. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  48. {
  49. var rnd = Common.Util.NewRandom();
  50. var records = new List<Record>();
  51. for (int i = 0; i < 10; ++i)
  52. records.Add(new Record(Common.Util.LoremIpsum(1, 1, 1, 1, 4), rnd.Next(100), rnd.Next(100), rnd.Next(100)));
  53.  
  54. var host = new LayoutHost();
  55. var view = host.CreateView(pageWidth, pageHeight);
  56.  
  57. var rt = view.CreateRect();
  58. rt.AnchorTopLeft(null, 36, 36);
  59.  
  60. var ta = new TableRenderer(g,
  61. rt, FixedTableSides.TopLeft,
  62. rowCount: records.Count + 1,
  63. columnCount: 4,
  64. gridLineColor: Color.DarkGray,
  65. gridLineWidth: 1,
  66. rowMinHeight: 10);
  67.  
  68. var cs = new CellStyle
  69. {
  70. PaddingTopBottom = 3,
  71. PaddingLeftRight = 4,
  72. FixedWidth = false,
  73. TextFormat = new TextFormat
  74. {
  75. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Regular.ttf")),
  76. FontSize = 16,
  77. FontSizeInGraphicUnits = true
  78. }
  79. };
  80. var csHeader = new CellStyle(cs)
  81. {
  82. PaddingRight = 9,
  83. ParagraphAlignment = ParagraphAlignment.Center,
  84. TextAlignment = TextAlignment.Center,
  85. };
  86. var csName = new CellStyle(cs)
  87. {
  88. MaxWidth = pageWidth / 2,
  89. };
  90. var csNumber = new CellStyle(cs)
  91. {
  92. TextAlignment = TextAlignment.Trailing
  93. };
  94.  
  95. ta.DefaultCellStyle = csHeader;
  96. ta.AddCell(0, 0, "Name");
  97. ta.AddCell(0, 1, "Take 1\nThe Good");
  98. ta.AddCell(0, 2, "Take 2\nThe Bad");
  99. ta.AddCell(0, 3, "Take 3\nThe Ugly");
  100.  
  101. ta.DefaultCellStyle = csNumber;
  102. for (int i = 0; i < records.Count; i++)
  103. {
  104. var p = records[i];
  105. int r = i + 1;
  106. ta.AddCell(csName, r, 0, p.Name);
  107. ta.AddCell(r, 1, p.Take1.ToString());
  108. ta.AddCell(r, 2, p.Take2.ToString());
  109. ta.AddCell(r, 3, p.Take3.ToString());
  110. }
  111.  
  112. ta.ApplyCellConstraints();
  113. ta.Render();
  114. }
  115. }
  116. }
  117.