DynamicProgressHeader.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. .ProduceImages()
  42. .RenderDocument(container =>
  43. {
  44. container.Page(page =>
  45. {
  46. page.Size(PageSizes.A6);
  47. page.Margin(1, Unit.Centimetre);
  48. page.PageColor(Colors.White);
  49. page.DefaultTextStyle(x => x.FontSize(20));
  50. page.Header().Dynamic(new ProgressHeader());
  51. page.Content().Column(column =>
  52. {
  53. foreach (var i in Enumerable.Range(0, 100))
  54. column.Item().PaddingTop(25).Background(Colors.Grey.Lighten2).Height(50);
  55. });
  56. page.Footer().AlignCenter().Text(text =>
  57. {
  58. text.DefaultTextStyle(x => x.FontSize(20));
  59. text.CurrentPageNumber();
  60. text.Span(" / ");
  61. text.TotalPages();
  62. });
  63. });
  64. });
  65. }
  66. }
  67. }