DynamicPageNumberLeftRight.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 FooterWithAlternatingAlignment : IDynamicComponent<int>
  12. {
  13. public int State { get; set; }
  14. public DynamicComponentComposeResult Compose(DynamicContext context)
  15. {
  16. var content = context.CreateElement(element =>
  17. {
  18. element
  19. .Element(x => context.PageNumber % 2 == 0 ? x.AlignLeft() : x.AlignRight())
  20. .Text(x =>
  21. {
  22. x.CurrentPageNumber();
  23. x.Span(" / ");
  24. x.TotalPages();
  25. });
  26. });
  27. return new DynamicComponentComposeResult()
  28. {
  29. Content = content,
  30. HasMoreContent = false
  31. };
  32. }
  33. }
  34. public static class DynamicPageNumberLeftRightExamples
  35. {
  36. [Test]
  37. public static void Dynamic()
  38. {
  39. RenderingTest
  40. .Create()
  41. .PageSize(PageSizes.A5)
  42. .MaxPages(100)
  43. .ShowResults()
  44. .ProduceImages()
  45. .RenderDocument(container =>
  46. {
  47. container.Page(page =>
  48. {
  49. page.Size(PageSizes.A6);
  50. page.PageColor(Colors.White);
  51. page.Margin(1, Unit.Centimetre);
  52. page.DefaultTextStyle(x => x.FontSize(18));
  53. page.Content().Column(column =>
  54. {
  55. foreach (var i in Enumerable.Range(0, 50))
  56. column.Item().PaddingTop(25).Background(Colors.Grey.Lighten2).Height(50);
  57. });
  58. page.Footer().Dynamic(new FooterWithAlternatingAlignment());
  59. });
  60. });
  61. }
  62. }
  63. }