TableOfContentsTemplate.cs 3.0 KB

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