TableOfContentsTemplate.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Collections.Generic;
  2. using QuestPDF.Fluent;
  3. using QuestPDF.Helpers;
  4. using QuestPDF.Infrastructure;
  5. using SkiaSharp;
  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 static void DrawLink(IContainer container, int number, string locationName)
  35. {
  36. container
  37. .SectionLink(locationName)
  38. .Row(row =>
  39. {
  40. row.ConstantItem(20).Text($"{number}.");
  41. row.AutoItem().Text(locationName);
  42. row.RelativeItem().PaddingHorizontal(2).AlignBottom().TranslateY(-3).Height(1).Canvas((canvas, space) =>
  43. {
  44. // best to statically cache
  45. using var paint = new SKPaint
  46. {
  47. StrokeWidth = space.Height,
  48. PathEffect = SKPathEffect.CreateDash(new float[] { 1, 3 }, 0)
  49. };
  50. canvas.DrawLine(0, 0, space.Width, 0, paint);
  51. });
  52. row.AutoItem().Text(text =>
  53. {
  54. text.BeginPageNumberOfSection(locationName);
  55. text.Span(" - ");
  56. text.EndPageNumberOfSection(locationName);
  57. var lengthStyle = TextStyle.Default.FontColor(Colors.Grey.Medium);
  58. text.TotalPagesWithinSection(locationName).Style(lengthStyle).Format(x =>
  59. {
  60. var formatted = x == 1 ? "1 page long" : $"{x} pages long";
  61. return $" ({formatted})";
  62. });
  63. });
  64. });
  65. }
  66. }
  67. }