SectionTemplate.cs 2.9 KB

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