DynamicProgressHeader.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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
  12. {
  13. public DynamicComponentComposeResult Compose(DynamicContext context)
  14. {
  15. var content = context.CreateElement(container =>
  16. {
  17. var width = context.AvailableSize.Width * context.PageNumber / context.TotalPages;
  18. container
  19. .Background(Colors.Blue.Lighten2)
  20. .Height(25)
  21. .Width(width)
  22. .Background(Colors.Blue.Darken1);
  23. });
  24. return new DynamicComponentComposeResult
  25. {
  26. Content = content,
  27. HasMoreContent = false
  28. };
  29. }
  30. }
  31. public static class DynamicProgressHeader
  32. {
  33. [Test]
  34. public static void Dynamic()
  35. {
  36. RenderingTest
  37. .Create()
  38. .ShowResults()
  39. .MaxPages(100)
  40. .ProduceImages()
  41. .RenderDocument(container =>
  42. {
  43. container.Page(page =>
  44. {
  45. page.Size(PageSizes.A6);
  46. page.Margin(1, Unit.Centimetre);
  47. page.PageColor(Colors.White);
  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. page.Footer().AlignCenter().Text(text =>
  56. {
  57. text.DefaultTextStyle(x => x.FontSize(20));
  58. text.CurrentPageNumber();
  59. text.Span(" / ");
  60. text.TotalPages();
  61. });
  62. });
  63. });
  64. }
  65. }
  66. }