SectionTemplate.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. .Before()
  22. .PaddingBottom(5)
  23. .Text(Model.Title)
  24. .Style(Typography.Headline);
  25. decoration.Content().Border(0.75f).BorderColor(Colors.Grey.Medium).Column(column =>
  26. {
  27. foreach (var part in Model.Parts)
  28. {
  29. column.Item().EnsureSpace(25).Row(row =>
  30. {
  31. row.ConstantItem(150).LabelCell().Text(part.Label);
  32. var frame = row.RelativeItem().ValueCell();
  33. if (part is ReportSectionText text)
  34. frame.ShowEntire().Text(text.Text);
  35. if (part is ReportSectionMap map)
  36. frame.Element(x => MapElement(x, map));
  37. if (part is ReportSectionPhotos photos)
  38. frame.Element(x => PhotosElement(x, photos));
  39. });
  40. }
  41. });
  42. });
  43. }
  44. void MapElement(IContainer container, ReportSectionMap model)
  45. {
  46. if (model.Location == null)
  47. {
  48. container.Text("No location provided");
  49. return;
  50. }
  51. container.ShowEntire().Column(column =>
  52. {
  53. column.Spacing(5);
  54. column.Item().MaxWidth(250).AspectRatio(4 / 3f).Component<ImagePlaceholder>();
  55. column.Item().Text(model.Location.Format());
  56. });
  57. }
  58. void PhotosElement(IContainer container, ReportSectionPhotos model)
  59. {
  60. if (model.PhotoCount == 0)
  61. {
  62. container.Text("No photos").Style(Typography.Normal);
  63. return;
  64. }
  65. container.DebugArea("Photos").Grid(grid =>
  66. {
  67. grid.Spacing(5);
  68. grid.Columns(3);
  69. Enumerable
  70. .Range(0, model.PhotoCount)
  71. .ToList()
  72. .ForEach(x => grid.Item().AspectRatio(4 / 3f).Component<ImagePlaceholder>());
  73. });
  74. }
  75. }
  76. }