TableExamples.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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(1000, 2000)
  74. .MaxPages(1000)
  75. .ShowResults()
  76. .Render(container =>
  77. {
  78. container
  79. .Table(table =>
  80. {
  81. table.ColumnsDefinition(columns =>
  82. {
  83. foreach (var size in Enumerable.Range(0, 10))
  84. columns.ConstantColumn(100);
  85. });
  86. foreach (var i in Enumerable.Range(1, 10000))
  87. {
  88. table
  89. .Cell()
  90. .RowSpan((uint)Random.Next(1, 5))
  91. .ColumnSpan((uint)Random.Next(1, 5))
  92. .Element(CreateBox(i.ToString()));
  93. }
  94. });
  95. });
  96. }
  97. private Action<IContainer> CreateBox(string label)
  98. {
  99. return container =>
  100. {
  101. var height = Random.Next(2, 7) * 25;
  102. container
  103. .Border(1)
  104. .Background(Placeholders.BackgroundColor())
  105. .Layers(layers =>
  106. {
  107. layers
  108. .PrimaryLayer()
  109. .ExtendHorizontal()
  110. .Height(height);
  111. layers
  112. .Layer()
  113. .AlignCenter()
  114. .AlignMiddle()
  115. .Text($"{label}: {height}px");
  116. });
  117. };
  118. }
  119. }
  120. }