DynamicOptimizedExample.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.Before().Element(header);
  46. decoration.Content().Column(column =>
  47. {
  48. foreach (var row in rows)
  49. column.Item().Element(row.Element);
  50. });
  51. decoration.After().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 static 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. .DefaultTextStyle(TextStyle.Default.SemiBold())
  74. .Row(row =>
  75. {
  76. row.ConstantItem(30).Text("#");
  77. row.RelativeItem().Text("Item name");
  78. row.ConstantItem(50).AlignRight().Text("Count");
  79. row.ConstantItem(50).AlignRight().Text("Price");
  80. row.ConstantItem(50).AlignRight().Text("Total");
  81. });
  82. });
  83. }
  84. private static 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. .DefaultTextStyle(TextStyle.Default.FontSize(14).SemiBold())
  94. .Text($"Subtotal: {total}$");
  95. });
  96. }
  97. private IEnumerable<(OrderItem Item, IDynamicElement Element)> GetItemsForPage(DynamicContext context, float decorationHeight)
  98. {
  99. var totalHeight = decorationHeight;
  100. foreach (var index in Enumerable.Range(State.ShownItemsCount, Items.Count - State.ShownItemsCount))
  101. {
  102. var item = Items.ElementAt(index);
  103. var element = context.CreateElement(content =>
  104. {
  105. content
  106. .Width(context.AvailableSize.Width)
  107. .BorderBottom(1)
  108. .BorderColor(Colors.Grey.Lighten2)
  109. .Padding(5)
  110. .Row(row =>
  111. {
  112. row.ConstantItem(30).Text((index + 1).ToString(CultureInfo.InvariantCulture));
  113. row.RelativeItem().Text(item.ItemName);
  114. row.ConstantItem(50).AlignRight().Text(item.Count.ToString(CultureInfo.InvariantCulture));
  115. row.ConstantItem(50).AlignRight().Text($"{item.Price}$");
  116. row.ConstantItem(50).AlignRight().Text($"{item.Count*item.Price}$");
  117. });
  118. });
  119. var elementHeight = element.Size.Height;
  120. // it is important to use the Size.Epsilon constant to avoid floating point comparison issues
  121. if (totalHeight + elementHeight > context.AvailableSize.Height + Size.Epsilon)
  122. break;
  123. totalHeight += elementHeight;
  124. yield return (item, element);
  125. }
  126. }
  127. }
  128. public static class DynamicOptimizedExamples
  129. {
  130. [Test]
  131. public static void Dynamic()
  132. {
  133. RenderingTest
  134. .Create()
  135. .PageSize(PageSizes.A5)
  136. .ProducePdf()
  137. .ShowResults()
  138. .Render(container =>
  139. {
  140. var items = Enumerable.Range(0, 25).Select(x => new OrderItem()).ToList();
  141. container
  142. .Background(Colors.White)
  143. .Padding(25)
  144. .Decoration(decoration =>
  145. {
  146. decoration
  147. .Before()
  148. .PaddingBottom(5)
  149. .Text(text =>
  150. {
  151. text.DefaultTextStyle(TextStyle.Default.SemiBold().FontColor(Colors.Blue.Darken2).FontSize(16));
  152. text.Span("Page ");
  153. text.CurrentPageNumber();
  154. text.Span(" of ");
  155. text.TotalPages();
  156. });
  157. decoration
  158. .Content()
  159. .Dynamic(new OptimizedOrdersTable(items));
  160. });
  161. });
  162. }
  163. }
  164. }