DynamicProgressHeader.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using QuestPDF.Elements;
  5. using QuestPDF.Examples.Engine;
  6. using QuestPDF.Fluent;
  7. using QuestPDF.Helpers;
  8. using QuestPDF.Infrastructure;
  9. namespace QuestPDF.Examples
  10. {
  11. public class ProgressHeader : IDynamicComponent<int>
  12. {
  13. public int State { get; set; }
  14. public DynamicComponentComposeResult Compose(DynamicContext context)
  15. {
  16. var content = context.CreateElement(container =>
  17. {
  18. var width = context.AvailableSize.Width * context.PageNumber / context.TotalPages;
  19. container
  20. .Background(Colors.Blue.Lighten2)
  21. .Height(25)
  22. .Width(width)
  23. .Background(Colors.Blue.Darken1);
  24. });
  25. return new DynamicComponentComposeResult
  26. {
  27. Content = content,
  28. HasMoreContent = false
  29. };
  30. }
  31. }
  32. public static class DynamicProgressHeader
  33. {
  34. [Test]
  35. public static void Dynamic()
  36. {
  37. RenderingTest
  38. .Create()
  39. .ShowResults()
  40. .MaxPages(100)
  41. .ProducePdf()
  42. .RenderDocument(container =>
  43. {
  44. container.Page(page =>
  45. {
  46. page.Size(PageSizes.A6);
  47. page.Margin(1, Unit.Centimetre);
  48. page.DefaultTextStyle(x => x.FontSize(20));
  49. page.Header().Dynamic(new ProgressHeader());
  50. page.Content().Column(column =>
  51. {
  52. foreach (var i in Enumerable.Range(0, 100))
  53. column.Item().PaddingTop(25).Background(Colors.Grey.Lighten2).Height(50);
  54. });
  55. });
  56. });
  57. }
  58. }
  59. }