TableOfContentsTemplate.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. .Height(3)
  49. .SkiaSharpRasterized((canvas, size) =>
  50. {
  51. using var paint = new SKPaint
  52. {
  53. StrokeWidth = 1,
  54. PathEffect = SKPathEffect.CreateDash(new float[] { 1, 3 }, 0),
  55. };
  56. canvas.Translate(0, 1);
  57. canvas.DrawLine(0, 0, size.Width, 0, paint);
  58. });
  59. row.AutoItem().Text(text =>
  60. {
  61. text.BeginPageNumberOfSection(locationName);
  62. text.Span(" - ");
  63. text.EndPageNumberOfSection(locationName);
  64. var lengthStyle = TextStyle.Default.FontColor(Colors.Grey.Medium);
  65. text.TotalPagesWithinSection(locationName).Style(lengthStyle).Format(x =>
  66. {
  67. var formatted = x == 1 ? "1 page long" : $"{x} pages long";
  68. return $" ({formatted})";
  69. });
  70. });
  71. });
  72. }
  73. }
  74. }