SemanticExamples.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System.Text.Json;
  2. using System.Text.Json.Serialization;
  3. using QuestPDF.Fluent;
  4. using QuestPDF.Helpers;
  5. using QuestPDF.Infrastructure;
  6. namespace QuestPDF.DocumentationExamples;
  7. public class SemanticExamples
  8. {
  9. public class BookTermModel
  10. {
  11. public string Term { get; set; }
  12. public string Description { get; set; }
  13. public string FirstLevelCategory { get; set; }
  14. public string SecondLevelCategory { get; set; }
  15. public string ThirdLevelCategory { get; set; }
  16. }
  17. [Test]
  18. public async Task GenerateBook()
  19. {
  20. QuestPDF.Settings.EnableCaching = false;
  21. QuestPDF.Settings.EnableDebugging = false;
  22. var serializerSettings = new JsonSerializerOptions
  23. {
  24. PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
  25. DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
  26. WriteIndented = true
  27. };
  28. var bookData = await File.ReadAllTextAsync("Resources/semantic-book-content.json");
  29. var terms = JsonSerializer.Deserialize<ICollection<BookTermModel>>(bookData, serializerSettings);
  30. var categories = terms
  31. .GroupBy(x => x.FirstLevelCategory)
  32. .Select(x => new
  33. {
  34. Category = x.Key,
  35. Terms = x
  36. .GroupBy(y => y.SecondLevelCategory)
  37. .Select(y => new
  38. {
  39. Category = y.Key,
  40. Terms = y
  41. .GroupBy(z => z.ThirdLevelCategory)
  42. .Select(z => new
  43. {
  44. Category = z.Key,
  45. Terms = z.ToList()
  46. })
  47. .ToList()
  48. })
  49. .ToList()
  50. })
  51. .ToList();
  52. Document
  53. .Create(document =>
  54. {
  55. document.Page(page =>
  56. {
  57. page.Size(PageSizes.A4);
  58. page.DefaultTextStyle(x => x.FontSize(20));
  59. page.Margin(50);
  60. page.PageColor(Colors.White);
  61. page.Header()
  62. .Text("Programming Terms")
  63. .Bold()
  64. .FontSize(36);
  65. page.Content()
  66. .PaddingVertical(24)
  67. .Column(column =>
  68. {
  69. foreach (var category1 in categories)
  70. {
  71. column.Item()
  72. .SemanticSection()
  73. .EnsureSpace(100)
  74. .Column(column =>
  75. {
  76. column.Spacing(24);
  77. column.Item()
  78. .PaddingBottom(8)
  79. .SemanticHeader1()
  80. .Text(category1.Category)
  81. .FontSize(24)
  82. .FontColor(Colors.Blue.Darken4)
  83. .Bold();
  84. foreach (var category2 in category1.Terms)
  85. {
  86. column.Item()
  87. .SemanticSection()
  88. .EnsureSpace(100)
  89. .Column(column =>
  90. {
  91. column.Spacing(8);
  92. column.Item()
  93. .PaddingBottom(8)
  94. .SemanticHeader2()
  95. .Text(category2.Category)
  96. .FontSize(20)
  97. .FontColor(Colors.Blue.Darken2)
  98. .Bold();
  99. foreach (var category3 in category2.Terms)
  100. {
  101. column.Item()
  102. .SemanticSection()
  103. .EnsureSpace(100)
  104. .Column(column =>
  105. {
  106. column.Spacing(8);
  107. column.Item()
  108. .PaddingBottom(8)
  109. .SemanticHeader3()
  110. .Text(category3.Category)
  111. .FontSize(16)
  112. .FontColor(Colors.Blue.Medium)
  113. .Bold();
  114. foreach (var term in category3.Terms)
  115. {
  116. column.Item()
  117. .SemanticParagraph()
  118. .Text(text =>
  119. {
  120. text.Span(term.Term).Bold();
  121. text.Span(" - ");
  122. text.Span(term.Description);
  123. });
  124. }
  125. });
  126. }
  127. });
  128. }
  129. });
  130. column.Item().PageBreak();
  131. }
  132. });
  133. page.Footer()
  134. .AlignCenter()
  135. .Text(text =>
  136. {
  137. text.Span("Page ");
  138. text.CurrentPageNumber();
  139. text.Span(" of ");
  140. text.TotalPages();
  141. });
  142. });
  143. })
  144. .WithMetadata(new DocumentMetadata()
  145. {
  146. Title = "Programming Terms",
  147. Language = "en-US"
  148. })
  149. .GeneratePdfAndShow();
  150. }
  151. }