SectionTemplate.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Linq;
  2. using QuestPDF.Fluent;
  3. using QuestPDF.Helpers;
  4. using QuestPDF.Infrastructure;
  5. namespace QuestPDF.ReportSample.Layouts
  6. {
  7. public class SectionTemplate : IComponent
  8. {
  9. public ReportSection Model { get; set; }
  10. public SectionTemplate(ReportSection model)
  11. {
  12. Model = model;
  13. }
  14. public void Compose(IContainer container)
  15. {
  16. container
  17. .EnsureSpace()
  18. .Decoration(decoration =>
  19. {
  20. decoration
  21. .Header()
  22. .PaddingBottom(5)
  23. .Text(Model.Title, Typography.Headline);
  24. decoration.Content().Border(0.75f).BorderColor(Colors.Grey.Medium).Stack(stack =>
  25. {
  26. foreach (var part in Model.Parts)
  27. {
  28. stack.Item().EnsureSpace(25).Row(row =>
  29. {
  30. row.ConstantColumn(150).LabelCell().Text(part.Label);
  31. var frame = row.RelativeColumn().ValueCell();
  32. if (part is ReportSectionText text)
  33. frame.ShowEntire().Text(text.Text);
  34. if (part is ReportSectionMap map)
  35. frame.Element(x => MapElement(x, map));
  36. if (part is ReportSectionPhotos photos)
  37. frame.Element(x => PhotosElement(x, photos));
  38. });
  39. }
  40. });
  41. });
  42. }
  43. void MapElement(IContainer container, ReportSectionMap model)
  44. {
  45. if (model.Location == null)
  46. {
  47. container.Text("No location provided");
  48. return;
  49. }
  50. container.ShowEntire().Stack(stack =>
  51. {
  52. stack.Spacing(5);
  53. stack.Item().MaxWidth(250).AspectRatio(4 / 3f).Component<ImagePlaceholder>();
  54. stack.Item().Text(model.Location.Format());
  55. });
  56. }
  57. void PhotosElement(IContainer container, ReportSectionPhotos model)
  58. {
  59. if (model.PhotoCount == 0)
  60. {
  61. container.Text("No photos", Typography.Normal);
  62. return;
  63. }
  64. container.DebugArea("Photos").Grid(grid =>
  65. {
  66. grid.Spacing(5);
  67. grid.Columns(3);
  68. Enumerable
  69. .Range(0, model.PhotoCount)
  70. .ToList()
  71. .ForEach(x => grid.Item().AspectRatio(4 / 3f).Component<ImagePlaceholder>());
  72. });
  73. }
  74. }
  75. }