DynamicSimpleTableExample.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System.Collections.Generic;
  2. using System.Drawing;
  3. using System.Linq;
  4. using NUnit.Framework;
  5. using QuestPDF.Elements;
  6. using QuestPDF.Examples.Engine;
  7. using QuestPDF.Fluent;
  8. using QuestPDF.Helpers;
  9. using QuestPDF.Infrastructure;
  10. namespace QuestPDF.Examples
  11. {
  12. public class OrdersTable : IDynamicComponent<OrdersTableState>
  13. {
  14. private IList<OrderItem> Items { get; }
  15. public OrdersTableState State { get; set; }
  16. public OrdersTable(IList<OrderItem> items)
  17. {
  18. Items = items;
  19. State = new OrdersTableState
  20. {
  21. ShownItemsCount = 0
  22. };
  23. }
  24. public DynamicComponentComposeResult Compose(DynamicContext context)
  25. {
  26. var possibleItems = Enumerable
  27. .Range(1, Items.Count - State.ShownItemsCount)
  28. .Select(itemsToDisplay => ComposeContent(context, itemsToDisplay))
  29. .TakeWhile(x => x.Size.Height <= context.AvailableSize.Height)
  30. .ToList();
  31. State = new OrdersTableState
  32. {
  33. ShownItemsCount = State.ShownItemsCount + possibleItems.Count
  34. };
  35. return new DynamicComponentComposeResult
  36. {
  37. Content = possibleItems.Last(),
  38. HasMoreContent = State.ShownItemsCount < Items.Count
  39. };
  40. }
  41. private IDynamicElement ComposeContent(DynamicContext context, int itemsToDisplay)
  42. {
  43. var total = Items.Skip(State.ShownItemsCount).Take(itemsToDisplay).Sum(x => x.Count * x.Price);
  44. return context.CreateElement(container =>
  45. {
  46. container
  47. .MinimalBox()
  48. .Width(context.AvailableSize.Width)
  49. .Table(table =>
  50. {
  51. table.ColumnsDefinition(columns =>
  52. {
  53. columns.ConstantColumn(30);
  54. columns.RelativeColumn();
  55. columns.ConstantColumn(50);
  56. columns.ConstantColumn(50);
  57. columns.ConstantColumn(50);
  58. });
  59. table.Header(header =>
  60. {
  61. header.Cell().Element(Style).Text("#");
  62. header.Cell().Element(Style).Text("Item name");
  63. header.Cell().Element(Style).AlignRight().Text("Count");
  64. header.Cell().Element(Style).AlignRight().Text("Price");
  65. header.Cell().Element(Style).AlignRight().Text("Total");
  66. IContainer Style(IContainer container)
  67. {
  68. return container
  69. .DefaultTextStyle(x => x.SemiBold())
  70. .BorderBottom(1)
  71. .BorderColor(Colors.Grey.Darken2)
  72. .Padding(5);
  73. }
  74. });
  75. table.Footer(footer =>
  76. {
  77. footer
  78. .Cell().ColumnSpan(5)
  79. .AlignRight()
  80. .Text($"Subtotal: {total}$", TextStyle.Default.Size(14).SemiBold());
  81. });
  82. foreach (var index in Enumerable.Range(State.ShownItemsCount, itemsToDisplay))
  83. {
  84. var item = Items[index];
  85. table.Cell().Element(Style).Text(index + 1);
  86. table.Cell().Element(Style).Text(item.ItemName);
  87. table.Cell().Element(Style).AlignRight().Text(item.Count);
  88. table.Cell().Element(Style).AlignRight().Text($"{item.Price}$");
  89. table.Cell().Element(Style).AlignRight().Text($"{item.Count*item.Price}$");
  90. IContainer Style(IContainer container)
  91. {
  92. return container
  93. .BorderBottom(1)
  94. .BorderColor(Colors.Grey.Lighten2)
  95. .Padding(5);
  96. }
  97. }
  98. });
  99. });
  100. }
  101. }
  102. public static class DynamicSimpleTableExample
  103. {
  104. [Test]
  105. public static void Dynamic()
  106. {
  107. RenderingTest
  108. .Create()
  109. .PageSize(PageSizes.A5)
  110. .ShowResults()
  111. .Render(container =>
  112. {
  113. var items = Enumerable.Range(0, 25).Select(x => new OrderItem()).ToList();
  114. container
  115. .Background(Colors.White)
  116. .Padding(25)
  117. .Decoration(decoration =>
  118. {
  119. decoration
  120. .Header()
  121. .PaddingBottom(5)
  122. .Text(text =>
  123. {
  124. text.DefaultTextStyle(TextStyle.Default.SemiBold().FontColor(Colors.Blue.Darken2).FontSize(16));
  125. text.Span("Page ");
  126. text.CurrentPageNumber();
  127. text.Span(" / ");
  128. text.TotalPages();
  129. });
  130. decoration
  131. .Content()
  132. .Dynamic(new OrdersTable(items));
  133. });
  134. });
  135. }
  136. }
  137. }