DataSource.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using QuestPDF.Helpers;
  5. using QuestPDF.ReportSample.Layouts;
  6. namespace QuestPDF.ReportSample
  7. {
  8. public static class DataSource
  9. {
  10. public static int SectionCounter { get; set; }
  11. public static int FieldCounter { get; set; }
  12. public static ReportModel GetReport()
  13. {
  14. return new ReportModel
  15. {
  16. Title = "Sample Report Document",
  17. HeaderFields = HeaderFields(),
  18. LogoData = Helpers.GetImage("logo.png"),
  19. Sections = Enumerable.Range(0, 10).Select(x => GenerateSection()).ToList(),
  20. Photos = Enumerable.Range(0, 10).Select(x => GetReportPhotos()).ToList()
  21. };
  22. List<ReportHeaderField> HeaderFields()
  23. {
  24. return new List<ReportHeaderField>
  25. {
  26. new ReportHeaderField()
  27. {
  28. Label = "Scope",
  29. Value = "Internal activities"
  30. },
  31. new ReportHeaderField()
  32. {
  33. Label = "Author",
  34. Value = "Marcin Ziąbek"
  35. },
  36. new ReportHeaderField()
  37. {
  38. Label = "Date",
  39. Value = DateTime.Now.ToString("g")
  40. }
  41. };
  42. }
  43. ReportSection GenerateSection()
  44. {
  45. var sectionLength = Helpers.Random.NextDouble() > 0.75
  46. ? Helpers.Random.Next(10, 20)
  47. : Helpers.Random.Next(3, 11);
  48. return new ReportSection
  49. {
  50. Title = TextPlaceholder.Label(),
  51. Parts = Enumerable.Range(0, sectionLength).Select(x => GetRandomElement()).ToList()
  52. };
  53. }
  54. ReportSectionElement GetRandomElement()
  55. {
  56. var random = Helpers.Random.NextDouble();
  57. if (random < 0.8f)
  58. return GetTextElement();
  59. if (random < 0.9f)
  60. return GetMapElement();
  61. return GetPhotosElement();
  62. }
  63. ReportSectionText GetTextElement()
  64. {
  65. return new ReportSectionText
  66. {
  67. Label = TextPlaceholder.Label(),
  68. Text = TextPlaceholder.Paragraph()
  69. };
  70. }
  71. ReportSectionMap GetMapElement()
  72. {
  73. var rnd = Helpers.Random.Next(0, 64);
  74. return new ReportSectionMap
  75. {
  76. Label = "Location",
  77. ImageSource = x => Helpers.GetDocumentMap($"{rnd}.jpg"),
  78. Location = Helpers.RandomLocation()
  79. };
  80. }
  81. ReportSectionPhotos GetPhotosElement()
  82. {
  83. return new ReportSectionPhotos
  84. {
  85. Label = "Photos",
  86. Photos = Enumerable
  87. .Range(0, Helpers.Random.Next(1, 10))
  88. .Select(x => Helpers.Random.Next(0, 128))
  89. .Select(x => Helpers.GetPhoto($"{x}.jpg"))
  90. .ToList()
  91. };
  92. }
  93. ReportPhoto GetReportPhotos()
  94. {
  95. var photoId = Helpers.Random.Next(0, 128);
  96. var mapId = Helpers.Random.Next(0, 64);
  97. return new ReportPhoto()
  98. {
  99. PhotoData = Helpers.GetPhoto($"{photoId}.jpg"),
  100. Comments = TextPlaceholder.Paragraph(),
  101. Date = DateTime.Now - TimeSpan.FromDays(Helpers.Random.NextDouble() * 100),
  102. Location = Helpers.RandomLocation(),
  103. MapContextSource = x => Helpers.GetContextMap($"{mapId}.jpg"),
  104. MapDetailsSource = x => Helpers.GetDetailsMap($"{mapId}.jpg")
  105. };
  106. }
  107. }
  108. }
  109. }