TableOfContentsTemplate.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Collections.Generic;
  2. using System.Drawing;
  3. using QuestPDF.Fluent;
  4. using QuestPDF.Helpers;
  5. using QuestPDF.Infrastructure;
  6. using SkiaSharp;
  7. namespace QuestPDF.ReportSample.Layouts
  8. {
  9. public class TableOfContentsTemplate : IComponent
  10. {
  11. private List<ReportSection> Sections { get; }
  12. public TableOfContentsTemplate(List<ReportSection> sections)
  13. {
  14. Sections = sections;
  15. }
  16. public void Compose(IContainer container)
  17. {
  18. container
  19. .Decoration(decoration =>
  20. {
  21. decoration
  22. .Before()
  23. .PaddingBottom(5)
  24. .Text("Table of contents")
  25. .Style(Typography.Headline);
  26. decoration.Content().Column(column =>
  27. {
  28. column.Spacing(5);
  29. for (var i = 0; i < Sections.Count; i++)
  30. column.Item().Element(c => DrawLink(c, i+1, Sections[i].Title));
  31. column.Item().Element(c => DrawLink(c, Sections.Count+1, "Photos"));
  32. });
  33. });
  34. }
  35. private static void DrawLink(IContainer container, int number, string locationName)
  36. {
  37. container
  38. .SectionLink(locationName)
  39. .Row(row =>
  40. {
  41. row.ConstantItem(20).Text($"{number}.");
  42. row.AutoItem().Text(locationName);
  43. row.RelativeItem().PaddingHorizontal(2).AlignBottom().TranslateY(-3).Height(1).Canvas((canvas, space) =>
  44. {
  45. // best to statically cache
  46. using var paint = new SKPaint
  47. {
  48. StrokeWidth = space.Height,
  49. PathEffect = SKPathEffect.CreateDash(new float[] { 1, 3 }, 0)
  50. };
  51. canvas.DrawLine(0, 0, space.Width, 0, paint);
  52. });
  53. row.AutoItem().Text(text =>
  54. {
  55. text.BeginPageNumberOfSection(locationName);
  56. text.Span(" - ");
  57. text.EndPageNumberOfSection(locationName);
  58. var lengthStyle = TextStyle.Default.FontColor(Colors.Grey.Medium);
  59. text.TotalPagesWithinSection(locationName).Style(lengthStyle).Format(x =>
  60. {
  61. var formatted = x == 1 ? "1 page long" : $"{x} pages long";
  62. return $" ({formatted})";
  63. });
  64. });
  65. });
  66. }
  67. }
  68. }