DynamicOptimizedExample.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System.Collections.Generic;
  2. using System.Globalization;
  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 OrderItem
  13. {
  14. public string ItemName { get; set; } = Placeholders.Label();
  15. public int Price { get; set; } = Placeholders.Random.Next(1, 11) * 10;
  16. public int Count { get; set; } = Placeholders.Random.Next(1, 11);
  17. }
  18. public struct OrdersTableState
  19. {
  20. public int ShownItemsCount { get; set; }
  21. }
  22. public class OptimizedOrdersTable : IDynamicComponent<OrdersTableState>
  23. {
  24. private ICollection<OrderItem> Items { get; }
  25. public OrdersTableState State { get; set; }
  26. public OptimizedOrdersTable(ICollection<OrderItem> items)
  27. {
  28. Items = items;
  29. State = new OrdersTableState
  30. {
  31. ShownItemsCount = 0
  32. };
  33. }
  34. public DynamicComponentComposeResult Compose(DynamicContext context)
  35. {
  36. var header = ComposeHeader(context);
  37. var sampleFooter = ComposeFooter(context, Enumerable.Empty<OrderItem>());
  38. var decorationHeight = header.Size.Height + sampleFooter.Size.Height;
  39. var rows = GetItemsForPage(context, decorationHeight).ToList();
  40. var footer = ComposeFooter(context, rows.Select(x => x.Item));
  41. var content = context.CreateElement(container =>
  42. {
  43. container.MinimalBox().Decoration(decoration =>
  44. {
  45. decoration.Header().Element(header);
  46. decoration.Content().Box().Stack(stack =>
  47. {
  48. foreach (var row in rows)
  49. stack.Item().Element(row.Element);
  50. });
  51. decoration.Footer().Element(footer);
  52. });
  53. });
  54. State = new OrdersTableState
  55. {
  56. ShownItemsCount = State.ShownItemsCount + rows.Count
  57. };
  58. return new DynamicComponentComposeResult
  59. {
  60. Content = content,
  61. HasMoreContent = State.ShownItemsCount < Items.Count
  62. };
  63. }
  64. private IDynamicElement ComposeHeader(DynamicContext context)
  65. {
  66. return context.CreateElement(element =>
  67. {
  68. element
  69. .Width(context.AvailableSize.Width)
  70. .BorderBottom(1)
  71. .BorderColor(Colors.Grey.Darken2)
  72. .Padding(5)
  73. .Row(row =>
  74. {
  75. var textStyle = TextStyle.Default.SemiBold();
  76. row.ConstantItem(30).Text("#", textStyle);
  77. row.RelativeItem().Text("Item name", textStyle);
  78. row.ConstantItem(50).AlignRight().Text("Count", textStyle);
  79. row.ConstantItem(50).AlignRight().Text("Price", textStyle);
  80. row.ConstantItem(50).AlignRight().Text("Total", textStyle);
  81. });
  82. });
  83. }
  84. private IDynamicElement ComposeFooter(DynamicContext context, IEnumerable<OrderItem> items)
  85. {
  86. var total = items.Sum(x => x.Count * x.Price);
  87. return context.CreateElement(element =>
  88. {
  89. element
  90. .Width(context.AvailableSize.Width)
  91. .Padding(5)
  92. .AlignRight()
  93. .Text($"Subtotal: {total}$", TextStyle.Default.Size(14).SemiBold());
  94. });
  95. }
  96. private IEnumerable<(OrderItem Item, IDynamicElement Element)> GetItemsForPage(DynamicContext context, float decorationHeight)
  97. {
  98. var totalHeight = decorationHeight;
  99. foreach (var index in Enumerable.Range(State.ShownItemsCount, Items.Count - State.ShownItemsCount))
  100. {
  101. var item = Items.ElementAt(index);
  102. var element = context.CreateElement(content =>
  103. {
  104. content
  105. .Width(context.AvailableSize.Width)
  106. .BorderBottom(1)
  107. .BorderColor(Colors.Grey.Lighten2)
  108. .Padding(5)
  109. .Row(row =>
  110. {
  111. row.ConstantItem(30).Text((index + 1).ToString(CultureInfo.InvariantCulture));
  112. row.RelativeItem().Text(item.ItemName);
  113. row.ConstantItem(50).AlignRight().Text(item.Count.ToString(CultureInfo.InvariantCulture));
  114. row.ConstantItem(50).AlignRight().Text($"{item.Price}$");
  115. row.ConstantItem(50).AlignRight().Text($"{item.Count*item.Price}$");
  116. });
  117. });
  118. var elementHeight = element.Size.Height;
  119. if (totalHeight + elementHeight > context.AvailableSize.Height)
  120. break;
  121. totalHeight += elementHeight;
  122. yield return (item, element);
  123. }
  124. }
  125. }
  126. public static class DynamicOptimizedExamples
  127. {
  128. [Test]
  129. public static void Dynamic()
  130. {
  131. RenderingTest
  132. .Create()
  133. .PageSize(PageSizes.A5)
  134. .ShowResults()
  135. .Render(container =>
  136. {
  137. var items = Enumerable.Range(0, 25).Select(x => new OrderItem()).ToList();
  138. container
  139. .Background(Colors.White)
  140. .Padding(25)
  141. .Decoration(decoration =>
  142. {
  143. decoration
  144. .Header()
  145. .PaddingBottom(5)
  146. .Text(text =>
  147. {
  148. text.DefaultTextStyle(TextStyle.Default.SemiBold().FontColor(Colors.Blue.Darken2).FontSize(16));
  149. text.Span("Page ");
  150. text.CurrentPageNumber();
  151. text.Span(" of ");
  152. text.TotalPages();
  153. });
  154. decoration
  155. .Content()
  156. .Dynamic(new OptimizedOrdersTable(items));
  157. });
  158. });
  159. }
  160. }
  161. }