DynamicPageNumberLeftRight.cs 2.1 KB

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