SectionTemplate.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Linq;
  3. using QuestPDF.Fluent;
  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. .MinHeight(100)
  18. .Section(section =>
  19. {
  20. section
  21. .Header()
  22. .PaddingBottom(5)
  23. .Text(Model.Title, Typography.Headline);
  24. section.Content().PageableStack(stack =>
  25. {
  26. foreach (var part in Model.Parts)
  27. {
  28. stack.Element().Row(row =>
  29. {
  30. row.ConstantColumn(150).DarkCell().Text(part.Label, Typography.Normal);
  31. var frame = row.RelativeColumn().LightCell();
  32. if (part is ReportSectionText text)
  33. frame.Text(text.Text, Typography.Normal);
  34. if (part is ReportSectionMap map)
  35. frame.Element(container => MapElement(container, map));
  36. if (part is ReportSectionPhotos photos)
  37. frame.Element(container => PhotosElement(container, photos));
  38. });
  39. }
  40. });
  41. });
  42. }
  43. void MapElement(IContainer container, ReportSectionMap model)
  44. {
  45. if (model.ImageSource == null || model.Location == null)
  46. {
  47. container.Text("No location provided", Typography.Normal);
  48. return;
  49. }
  50. container.Stack(stack =>
  51. {
  52. stack.Spacing(5);
  53. stack.Element().Component(new ImageTemplate(model.ImageSource));
  54. stack.Element().Text(model.Location.Format(), Typography.Normal);
  55. });
  56. }
  57. void PhotosElement(IContainer container, ReportSectionPhotos model)
  58. {
  59. if (model.Photos.Count == 0)
  60. {
  61. container.Text("No photos", Typography.Normal);
  62. return;
  63. }
  64. var rowCount = (int) Math.Ceiling(model.Photos.Count / 3f);
  65. container.Padding(-2).Stack(stack =>
  66. {
  67. foreach (var rowId in Enumerable.Range(0, rowCount))
  68. {
  69. stack.Element().Row(row =>
  70. {
  71. foreach (var id in Enumerable.Range(0, 3))
  72. {
  73. var data = model.Photos.ElementAtOrDefault(rowId + id);
  74. row.RelativeColumn().Padding(2).Component(new ImageTemplate(data));
  75. }
  76. });
  77. }
  78. });
  79. }
  80. }
  81. }