TableExamples.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using QuestPDF.Examples.Engine;
  5. using QuestPDF.Fluent;
  6. using QuestPDF.Helpers;
  7. using QuestPDF.Infrastructure;
  8. namespace QuestPDF.Examples
  9. {
  10. public class TableExamples
  11. {
  12. public static Random Random { get; } = new Random();
  13. [Test]
  14. public void Example()
  15. {
  16. RenderingTest
  17. .Create()
  18. .ProduceImages()
  19. .PageSize(PageSizes.A4)
  20. .ShowResults()
  21. .Render(container =>
  22. {
  23. container
  24. .Padding(25)
  25. .Box()
  26. .Border(2)
  27. .Table(table =>
  28. {
  29. table.ColumnsDefinition(columns =>
  30. {
  31. columns.ConstantColumn(100);
  32. columns.RelativeColumn();
  33. columns.ConstantColumn(100);
  34. columns.ConstantColumn(200);
  35. });
  36. table.Cell().ColumnSpan(2).Element(CreateBox("A"));
  37. table.Cell().Element(CreateBox("B"));
  38. table.Cell().Element(CreateBox("C"));
  39. table.Cell().Element(CreateBox("D"));
  40. table.Cell().RowSpan(2).Element(CreateBox("E"));
  41. table.Cell().RowSpan(3).ColumnSpan(2).Element(CreateBox("F"));
  42. table.Cell().RowSpan(2).Element(CreateBox("G"));
  43. table.Cell().RowSpan(2).Element(CreateBox("H"));
  44. table.Cell().Element(CreateBox("I"));
  45. table.Cell().Element(CreateBox("J"));
  46. table.Cell().RowSpan(2).Element(CreateBox("K"));
  47. table.Cell().ColumnSpan(2).Element(CreateBox("L"));
  48. table.Cell().Element(CreateBox("M"));
  49. // table.Cell().Row(1).Column(1).ColumnSpan(2).Element(CreateBox("A"));
  50. // table.Cell().Row(1).Column(3).Element(CreateBox("B"));
  51. // table.Cell().Row(1).Column(4).Element(CreateBox("C"));
  52. //
  53. // table.Cell().Row(2).Column(1).Element(CreateBox("D"));
  54. // table.Cell().Row(2).RowSpan(2).Column(2).Element(CreateBox("E"));
  55. // table.Cell().Row(2).RowSpan(3).Column(3).ColumnSpan(2).Element(CreateBox("F"));
  56. //
  57. // table.Cell().Row(3).RowSpan(2).Column(1).Element(CreateBox("G"));
  58. // table.Cell().Row(4).RowSpan(2).Column(2).Element(CreateBox("H"));
  59. // table.Cell().Row(5).Column(3).Element(CreateBox("I"));
  60. // table.Cell().Row(5).Column(4).Element(CreateBox("J"));
  61. // table.Cell().Row(5).RowSpan(2).Column(1).Element(CreateBox("K"));
  62. // table.Cell().Row(6).Column(2).ColumnSpan(2).Element(CreateBox("L"));
  63. // table.Cell().Row(6).Column(4).Element(CreateBox("M"));
  64. });
  65. });
  66. }
  67. [Test]
  68. public void PerformanceTest()
  69. {
  70. RenderingTest
  71. .Create()
  72. .ProducePdf()
  73. .PageSize(1002, 2002)
  74. .MaxPages(1000)
  75. .EnableCaching()
  76. .EnableDebugging(false)
  77. .ShowResults()
  78. .Render(container =>
  79. {
  80. container
  81. .Padding(1)
  82. .Table(table =>
  83. {
  84. table.ColumnsDefinition(columns =>
  85. {
  86. foreach (var size in Enumerable.Range(0, 10))
  87. columns.ConstantColumn(100);
  88. });
  89. foreach (var i in Enumerable.Range(1, 10_000))
  90. {
  91. table
  92. .Cell()
  93. .RowSpan((uint)Random.Next(1, 5))
  94. .ColumnSpan((uint)Random.Next(1, 5))
  95. .Element(CreateBox(i.ToString()));
  96. }
  97. });
  98. });
  99. }
  100. private Action<IContainer> CreateBox(string label)
  101. {
  102. return container =>
  103. {
  104. var height = Random.Next(2, 7) * 25;
  105. container
  106. .Border(2)
  107. .Background(Placeholders.BackgroundColor())
  108. .Layers(layers =>
  109. {
  110. layers
  111. .PrimaryLayer()
  112. .AlignCenter()
  113. .AlignMiddle()
  114. .Height(height)
  115. .Width(80)
  116. .Border(1);
  117. layers
  118. .Layer()
  119. .AlignCenter()
  120. .AlignMiddle()
  121. .Text($"{label}: {height}px");
  122. });
  123. };
  124. }
  125. }
  126. }