DynamicSimpleTableExample.cs 6.2 KB

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