LazyExamples.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using QuestPDF.Fluent;
  2. using QuestPDF.Helpers;
  3. using QuestPDF.Infrastructure;
  4. namespace QuestPDF.DocumentationExamples;
  5. public class LazyExamples
  6. {
  7. class SimpleComponent : IComponent
  8. {
  9. public required int Start { get; init; }
  10. public required int End { get; init; }
  11. public void Compose(IContainer container)
  12. {
  13. container.Decoration(decoration =>
  14. {
  15. decoration.Before()
  16. .Text($"Numbers from {Start} to {End}")
  17. .FontSize(20).Bold().FontColor(Colors.Blue.Darken2);
  18. decoration.Content().Column(column =>
  19. {
  20. foreach (var i in Enumerable.Range(Start, End - Start + 1))
  21. column.Item().Text($"Number {i}").FontSize(10);
  22. });
  23. });
  24. }
  25. }
  26. [Test]
  27. public void Disabled()
  28. {
  29. Document
  30. .Create(document =>
  31. {
  32. document.Page(page =>
  33. {
  34. page.Margin(10);
  35. page.Content().Column(column =>
  36. {
  37. const int sectionSize = 1000;
  38. foreach (var i in Enumerable.Range(0, 1000))
  39. {
  40. column.Item().Component(new SimpleComponent
  41. {
  42. Start = i * sectionSize,
  43. End = i * sectionSize + sectionSize - 1
  44. });
  45. }
  46. });
  47. });
  48. })
  49. .GeneratePdf("lazy-disabled.pdf");
  50. }
  51. [Test]
  52. public void Enabled()
  53. {
  54. Document
  55. .Create(document =>
  56. {
  57. document.Page(page =>
  58. {
  59. page.Margin(10);
  60. page.Content().Column(column =>
  61. {
  62. const int sectionSize = 1000;
  63. foreach (var i in Enumerable.Range(0, 1000))
  64. {
  65. var start = i * sectionSize;
  66. var end = start + sectionSize - 1;
  67. column.Item().Lazy(c =>
  68. {
  69. c.Component(new SimpleComponent
  70. {
  71. Start = start,
  72. End = end
  73. });
  74. });
  75. }
  76. });
  77. });
  78. })
  79. .GeneratePdf("lazy-enabled.pdf");
  80. }
  81. [Test]
  82. public void EnabledWithCache()
  83. {
  84. Document
  85. .Create(document =>
  86. {
  87. document.Page(page =>
  88. {
  89. page.Margin(10);
  90. page.Content().Column(column =>
  91. {
  92. const int sectionSize = 1000;
  93. foreach (var i in Enumerable.Range(0, 1000))
  94. {
  95. var start = i * sectionSize;
  96. var end = start + sectionSize - 1;
  97. column.Item().LazyWithCache(c =>
  98. {
  99. c.Component(new SimpleComponent
  100. {
  101. Start = start,
  102. End = end
  103. });
  104. });
  105. }
  106. });
  107. });
  108. })
  109. .GeneratePdf("lazy-enabled-with-cache.pdf");
  110. }
  111. }