DynamicPositionCapture.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Globalization;
  5. using System.Linq;
  6. using NUnit.Framework;
  7. using QuestPDF.Elements;
  8. using QuestPDF.Examples.Engine;
  9. using QuestPDF.Fluent;
  10. using QuestPDF.Helpers;
  11. using QuestPDF.Infrastructure;
  12. namespace QuestPDF.Examples
  13. {
  14. public class DynamicPositionCaptureExample : IDynamicComponent
  15. {
  16. public DynamicComponentComposeResult Compose(DynamicContext context)
  17. {
  18. var positions = Enumerable
  19. .Range(0, 20)
  20. .SelectMany(x => context.GetElementCapturedLocations($"capture_{x}"))
  21. .ToList();
  22. var visibleCount = positions.Count(x => x.PageNumber == context.PageNumber);
  23. return new DynamicComponentComposeResult
  24. {
  25. Content = context.CreateElement(container => container.Text(visibleCount.ToString())),
  26. HasMoreContent = positions.Any(x => x.PageNumber > context.PageNumber + 1)
  27. };
  28. }
  29. }
  30. public static class DynamicPositionCapture
  31. {
  32. [Test]
  33. public static void Dynamic()
  34. {
  35. RenderingTest
  36. .Create()
  37. .PageSize(PageSizes.A4)
  38. .ShowResults()
  39. .ProducePdf()
  40. .Render(container =>
  41. {
  42. container
  43. .Background(Colors.White)
  44. .Padding(25)
  45. .Row(row =>
  46. {
  47. row.Spacing(25);
  48. row.RelativeItem().Border(1).Column(column =>
  49. {
  50. column.Spacing(25);
  51. foreach (var i in Enumerable.Range(0, 20))
  52. {
  53. column.Item()
  54. .CaptureLocation($"capture_{i}")
  55. .Width(Random.Shared.Next(25, 125))
  56. .Height(Random.Shared.Next(25, 125))
  57. .Background(Placeholders.BackgroundColor());
  58. }
  59. });
  60. row.RelativeItem().Dynamic(new DynamicPositionCaptureExample());
  61. });
  62. });
  63. }
  64. }
  65. }