DynamicProgressHeader.cs 2.3 KB

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