TableExamples.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using NUnit.Framework;
  6. using QuestPDF.Drawing;
  7. using QuestPDF.Examples.Engine;
  8. using QuestPDF.Fluent;
  9. using QuestPDF.Helpers;
  10. using QuestPDF.Infrastructure;
  11. using IContainer = QuestPDF.Infrastructure.IContainer;
  12. namespace QuestPDF.Examples
  13. {
  14. public class TableExamples
  15. {
  16. public static Random Random { get; } = new Random();
  17. [Test]
  18. public void Example()
  19. {
  20. RenderingTest
  21. .Create()
  22. .ProduceImages()
  23. .PageSize(PageSizes.A4)
  24. .ShowResults()
  25. .Render(container =>
  26. {
  27. container
  28. .Padding(25)
  29. .Box()
  30. .Border(2)
  31. .Table(table =>
  32. {
  33. table.ColumnsDefinition(columns =>
  34. {
  35. columns.ConstantColumn(100);
  36. columns.RelativeColumn();
  37. columns.ConstantColumn(100);
  38. columns.ConstantColumn(200);
  39. });
  40. table.Cell().ColumnSpan(2).Element(CreateBox("A"));
  41. table.Cell().Element(CreateBox("B"));
  42. table.Cell().Element(CreateBox("C"));
  43. table.Cell().Element(CreateBox("D"));
  44. table.Cell().RowSpan(2).Element(CreateBox("E"));
  45. table.Cell().RowSpan(3).ColumnSpan(2).Element(CreateBox("F"));
  46. table.Cell().RowSpan(2).Element(CreateBox("G"));
  47. table.Cell().RowSpan(2).Element(CreateBox("H"));
  48. table.Cell().Element(CreateBox("I"));
  49. table.Cell().Element(CreateBox("J"));
  50. table.Cell().RowSpan(2).Element(CreateBox("K"));
  51. table.Cell().ColumnSpan(2).Element(CreateBox("L"));
  52. table.Cell().Element(CreateBox("M"));
  53. });
  54. });
  55. }
  56. [Test]
  57. public void TreeTable()
  58. {
  59. RenderingTest
  60. .Create()
  61. .ProducePdf()
  62. .PageSize(PageSizes.A4)
  63. .ShowResults()
  64. .Render(container =>
  65. {
  66. container
  67. .Padding(25)
  68. .Box()
  69. .Border(2)
  70. .Table(table =>
  71. {
  72. table.ColumnsDefinition(columns =>
  73. {
  74. columns.RelativeColumn(100);
  75. columns.RelativeColumn(100);
  76. columns.RelativeColumn(100);
  77. });
  78. table.Cell().RowSpan(4).Element(CreateBox("A"));
  79. table.Cell().RowSpan(2).Element(CreateBox("B"));
  80. table.Cell().Element(CreateBox("C"));
  81. table.Cell().Element(CreateBox("D"));
  82. table.Cell().RowSpan(2).Element(CreateBox("E"));
  83. table.Cell().Element(CreateBox("F"));
  84. table.Cell().Element(CreateBox("G"));
  85. });
  86. });
  87. }
  88. [Test]
  89. public void TemperatureReport()
  90. {
  91. RenderingTest
  92. .Create()
  93. .ProducePdf()
  94. .PageSize(PageSizes.A4)
  95. .ShowResults()
  96. .Render(container => GeneratePerformanceStructure(container, 100));
  97. }
  98. [Test]
  99. public void TemperatureReport_PerformanceTest()
  100. {
  101. RenderingTest
  102. .Create()
  103. .ProducePdf()
  104. .PageSize(PageSizes.A4)
  105. .MaxPages(10000)
  106. .EnableCaching()
  107. .EnableDebugging(false)
  108. .ShowResults()
  109. .Render(container => GeneratePerformanceStructure(container, 1000));
  110. }
  111. public static void GeneratePerformanceStructure(IContainer container, int repeats)
  112. {
  113. container
  114. .Padding(25)
  115. .Box()
  116. .Border(2)
  117. .Table(table =>
  118. {
  119. table.ColumnsDefinition(columns =>
  120. {
  121. columns.ConstantColumn(100);
  122. columns.RelativeColumn();
  123. columns.ConstantColumn(100);
  124. columns.RelativeColumn();
  125. });
  126. foreach (var _ in Enumerable.Range(0, repeats))
  127. {
  128. table.Cell().RowSpan(3).LabelCell("Project");
  129. table.Cell().RowSpan(3).ValueCell(Placeholders.Sentence());
  130. table.Cell().LabelCell("Date");
  131. table.Cell().ValueCell(Placeholders.ShortDate());
  132. table.Cell().LabelCell("Report number");
  133. table.Cell().ValueCell(Placeholders.Integer());
  134. table.Cell().LabelCell("Inspector");
  135. table.Cell().ValueCell("Marcin Ziąbek");
  136. table.Cell().ColumnSpan(2).LabelCell("Morning weather");
  137. table.Cell().ColumnSpan(2).LabelCell("Evening weather");
  138. table.Cell().ValueCell("Time");
  139. table.Cell().ValueCell("7:13");
  140. table.Cell().ValueCell("Time");
  141. table.Cell().ValueCell("18:25");
  142. table.Cell().ValueCell("Description");
  143. table.Cell().ValueCell("Sunny");
  144. table.Cell().ValueCell("Description");
  145. table.Cell().ValueCell("Windy");
  146. table.Cell().ValueCell("Wind");
  147. table.Cell().ValueCell("Mild");
  148. table.Cell().ValueCell("Wind");
  149. table.Cell().ValueCell("Strong");
  150. table.Cell().ValueCell("Temperature");
  151. table.Cell().ValueCell("17°C");
  152. table.Cell().ValueCell("Temperature");
  153. table.Cell().ValueCell("32°C");
  154. table.Cell().LabelCell("Remarks");
  155. table.Cell().ColumnSpan(3).ValueCell(Placeholders.Paragraph());
  156. table.Cell().ColumnSpan(4).BorderBottom(2);
  157. }
  158. });
  159. }
  160. private Action<IContainer> CreateBox(string label)
  161. {
  162. return container =>
  163. {
  164. var height = Random.Next(2, 6) * 10;
  165. container
  166. .Background(Placeholders.BackgroundColor())
  167. // .AlignCenter()
  168. // .AlignMiddle()
  169. .Height(height);
  170. // .Text($"{label}: {height}px");
  171. };
  172. }
  173. }
  174. }