TableOfContentsTemplate.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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", Typography.Headline);
  24. decoration.Content().Column(column =>
  25. {
  26. column.Spacing(5);
  27. for (var i = 0; i < Sections.Count; i++)
  28. column.Item().Element(c => DrawLink(c, i+1, Sections[i].Title));
  29. column.Item().Element(c => DrawLink(c, Sections.Count+1, "Photos"));
  30. });
  31. });
  32. }
  33. private void DrawLink(IContainer container, int number, string locationName)
  34. {
  35. container
  36. .InternalLink(locationName)
  37. .Row(row =>
  38. {
  39. row.ConstantItem(25).Text($"{number}.");
  40. row.RelativeItem().Text(locationName);
  41. row.ConstantItem(150).AlignRight().Text(text =>
  42. {
  43. text.BeginPageNumberOfSection(locationName);
  44. text.Span(" - ");
  45. text.EndPageNumberOfSection(locationName);
  46. var lengthStyle = TextStyle.Default.Color(Colors.Grey.Medium);
  47. text.Span(" (", lengthStyle);
  48. text.TotalPagesWithinSection(locationName, lengthStyle, x => x == 1 ? "1 page long" : $"{x} pages long");
  49. text.Span(")", lengthStyle);
  50. });
  51. });
  52. }
  53. }
  54. }