TableOfContentsTemplate.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections.Generic;
  2. using System.Drawing;
  3. using QuestPDF.Fluent;
  4. using QuestPDF.Helpers;
  5. using QuestPDF.Infrastructure;
  6. namespace QuestPDF.ReportSample.Layouts
  7. {
  8. public class TableOfContentsTemplate : IComponent
  9. {
  10. private List<ReportSection> Sections { get; }
  11. public TableOfContentsTemplate(List<ReportSection> sections)
  12. {
  13. Sections = sections;
  14. }
  15. public void Compose(IContainer container)
  16. {
  17. container
  18. .Decoration(decoration =>
  19. {
  20. decoration
  21. .Before()
  22. .PaddingBottom(5)
  23. .Text("Table of contents")
  24. .Style(Typography.Headline);
  25. decoration.Content().Column(column =>
  26. {
  27. column.Spacing(5);
  28. for (var i = 0; i < Sections.Count; i++)
  29. column.Item().Element(c => DrawLink(c, i+1, Sections[i].Title));
  30. column.Item().Element(c => DrawLink(c, Sections.Count+1, "Photos"));
  31. });
  32. });
  33. }
  34. private void DrawLink(IContainer container, int number, string locationName)
  35. {
  36. container
  37. .InternalLink(locationName)
  38. .Row(row =>
  39. {
  40. row.ConstantItem(25).Text($"{number}.");
  41. row.RelativeItem().Text(locationName);
  42. row.ConstantItem(150).AlignRight().Text(text =>
  43. {
  44. text.BeginPageNumberOfSection(locationName);
  45. text.Span(" - ");
  46. text.EndPageNumberOfSection(locationName);
  47. var lengthStyle = TextStyle.Default.Color(Colors.Grey.Medium);
  48. text.Span(" (").Style(lengthStyle);
  49. text.TotalPagesWithinSection(locationName).Style(lengthStyle).Format(x => x == 1 ? "1 page long" : $"{x} pages long");
  50. text.Span(")").Style(lengthStyle);
  51. });
  52. });
  53. }
  54. }
  55. }