DynamicSimpleTableExample.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. .PaddingTop(10)
  81. .Text($"Subtotal: {total}$", TextStyle.Default.Bold());
  82. });
  83. foreach (var index in Enumerable.Range(State.ShownItemsCount, itemsToDisplay))
  84. {
  85. var item = Items[index];
  86. table.Cell().Element(Style).Text(index + 1);
  87. table.Cell().Element(Style).Text(item.ItemName);
  88. table.Cell().Element(Style).AlignRight().Text(item.Count);
  89. table.Cell().Element(Style).AlignRight().Text($"{item.Price}$");
  90. table.Cell().Element(Style).AlignRight().Text($"{item.Count*item.Price}$");
  91. IContainer Style(IContainer container)
  92. {
  93. return container
  94. .BorderBottom(1)
  95. .BorderColor(Colors.Grey.Lighten2)
  96. .Padding(5);
  97. }
  98. }
  99. });
  100. });
  101. }
  102. }
  103. public static class DynamicSimpleTableExample
  104. {
  105. [Test]
  106. public static void Dynamic()
  107. {
  108. RenderingTest
  109. .Create()
  110. .PageSize(PageSizes.A5)
  111. .ShowResults()
  112. .ProduceImages()
  113. .Render(container =>
  114. {
  115. var items = Enumerable.Range(0, 15).Select(x => new OrderItem()).ToList();
  116. container
  117. .Background(Colors.White)
  118. .Padding(25)
  119. .DefaultTextStyle(x => x.FontSize(16))
  120. .Decoration(decoration =>
  121. {
  122. decoration
  123. .Header()
  124. .PaddingBottom(5)
  125. .Text(text =>
  126. {
  127. text.DefaultTextStyle(TextStyle.Default.SemiBold().FontColor(Colors.Blue.Darken2));
  128. text.Span("Page ");
  129. text.CurrentPageNumber();
  130. text.Span(" / ");
  131. text.TotalPages();
  132. });
  133. decoration
  134. .Content()
  135. .Dynamic(new OrdersTable(items));
  136. });
  137. });
  138. }
  139. }
  140. }