DynamicPageNumberLeftRight.cs 2.1 KB

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