2
0

DynamicTests.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using QuestPDF.ConformanceTests.TestEngine;
  2. using QuestPDF.Drawing;
  3. using QuestPDF.Elements;
  4. using QuestPDF.Fluent;
  5. using QuestPDF.Infrastructure;
  6. namespace QuestPDF.ConformanceTests;
  7. internal class DynamicTests : ConformanceTestBase
  8. {
  9. protected override Document GetDocumentUnderTest()
  10. {
  11. var imageData = File.ReadAllBytes("Resources/photo.jpeg");
  12. return Document
  13. .Create(document =>
  14. {
  15. document.Page(page =>
  16. {
  17. page.Margin(60);
  18. page.Content()
  19. .PaddingVertical(30)
  20. .Column(column =>
  21. {
  22. column.Spacing(25);
  23. column.Item().SemanticHeader1().Text("Conformance Test: Lazy");
  24. column.Item().SemanticHeader2().Text("Before lazy");
  25. foreach (var i in Enumerable.Range(0, 10))
  26. column.Item().Dynamic(new DynamicComponent(i));
  27. column.Item().SemanticHeader2().Text("After lazy");
  28. });
  29. });
  30. });
  31. }
  32. protected override SemanticTreeNode? GetExpectedSemanticTree()
  33. {
  34. return ExpectedSemanticTree.DocumentRoot(root =>
  35. {
  36. root.Child("H1", h1 => h1.Alt("Conformance Test: Lazy"));
  37. root.Child("H2", h2 => h2.Alt("Before lazy"));
  38. foreach (var i in Enumerable.Range(0, 10))
  39. {
  40. root.Child("Art", art =>
  41. {
  42. art.Child("H3", h3 => h3.Alt($"Article {i}"));
  43. foreach (var j in Enumerable.Range(0, 10))
  44. {
  45. art.Child("P");
  46. }
  47. });
  48. }
  49. root.Child("H2", h2 => h2.Alt("After lazy"));
  50. });
  51. }
  52. internal class DynamicComponent(int index) : IDynamicComponent
  53. {
  54. public DynamicComponentComposeResult Compose(DynamicContext context)
  55. {
  56. var result = context.CreateElement(container =>
  57. {
  58. container.SemanticArticle().Column(column =>
  59. {
  60. column.Item().SemanticHeader3().Text($"Article {index}").Bold();
  61. foreach (var j in Enumerable.Range(0, 10))
  62. {
  63. column.Item().Text($"{index} - {j}");
  64. }
  65. });
  66. });
  67. if (result.Size.Height > context.AvailableSize.Height)
  68. {
  69. return new DynamicComponentComposeResult
  70. {
  71. Content = context.CreateElement(container => { }),
  72. HasMoreContent = true
  73. };
  74. }
  75. return new DynamicComponentComposeResult
  76. {
  77. Content = result,
  78. HasMoreContent = false
  79. };
  80. }
  81. }
  82. }