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. class Record
  39. {
  40. public Record(string name, int take1, int take2, int take3)
  41. {
  42. Name = name;
  43. Take1 = take1;
  44. Take2 = take2;
  45. Take3 = take3;
  46. }
  47. public string Name { get; }
  48. public int Take1 { get; }
  49. public int Take2 { get; }
  50. public int Take3 { get; }
  51. }
  52.  
  53. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  54. {
  55. var rnd = Common.Util.NewRandom();
  56. var records = new List<Record>();
  57. for (int i = 0; i < 10; ++i)
  58. records.Add(new Record(Common.Util.LoremIpsum(1, 1, 1, 1, 4), rnd.Next(100), rnd.Next(100), rnd.Next(100)));
  59.  
  60. var host = new LayoutHost();
  61. var view = host.CreateView(pageWidth, pageHeight);
  62.  
  63. var rt = view.CreateRect();
  64. rt.AnchorTopLeft(null, 36, 36);
  65.  
  66. var ta = new TableRenderer(g,
  67. rt, FixedTableSides.TopLeft,
  68. rowCount: records.Count + 1,
  69. columnCount: 4,
  70. gridLineColor: Color.DarkGray,
  71. gridLineWidth: 1,
  72. rowMinHeight: 10);
  73.  
  74. var cs = new CellStyle
  75. {
  76. PaddingTopBottom = 3,
  77. PaddingLeftRight = 4,
  78. FixedWidth = false,
  79. TextFormat = new TextFormat
  80. {
  81. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibri.ttf")),
  82. FontSize = 18,
  83. FontSizeInGraphicUnits = true
  84. }
  85. };
  86. var csHeader = new CellStyle(cs)
  87. {
  88. PaddingRight = 9,
  89. ParagraphAlignment = ParagraphAlignment.Center,
  90. TextAlignment = TextAlignment.Center,
  91. };
  92. var csName = new CellStyle(cs)
  93. {
  94. MaxWidth = pageWidth / 2,
  95. };
  96. var csNumber = new CellStyle(cs)
  97. {
  98. TextAlignment = TextAlignment.Trailing
  99. };
  100.  
  101. ta.DefaultCellStyle = csHeader;
  102. ta.AddCell(0, 0, "Name");
  103. ta.AddCell(0, 1, "Take 1\nThe Good");
  104. ta.AddCell(0, 2, "Take 2\nThe Bad");
  105. ta.AddCell(0, 3, "Take 3\nThe Ugly");
  106.  
  107. ta.DefaultCellStyle = csNumber;
  108. for (int i = 0; i < records.Count; i++)
  109. {
  110. var p = records[i];
  111. int r = i + 1;
  112. ta.AddCell(csName, r, 0, p.Name);
  113. ta.AddCell(r, 1, p.Take1.ToString());
  114. ta.AddCell(r, 2, p.Take2.ToString());
  115. ta.AddCell(r, 3, p.Take3.ToString());
  116. }
  117.  
  118. ta.ApplyCellConstraints();
  119. ta.Render();
  120. }
  121. }
  122. }
  123.