SemanticExamples.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. .SemanticDocument()
  68. .Column(column =>
  69. {
  70. foreach (var category1 in categories)
  71. {
  72. column.Item()
  73. .SemanticSection()
  74. .EnsureSpace(100)
  75. .Column(column =>
  76. {
  77. column.Spacing(24);
  78. column.Item()
  79. .PaddingBottom(8)
  80. .SemanticHeader1(category1.Category)
  81. .Text(category1.Category)
  82. .FontSize(24)
  83. .FontColor(Colors.Blue.Darken4)
  84. .Bold();
  85. foreach (var category2 in category1.Terms)
  86. {
  87. column.Item()
  88. .SemanticSection()
  89. .EnsureSpace(100)
  90. .Column(column =>
  91. {
  92. column.Spacing(8);
  93. column.Item()
  94. .PaddingBottom(8)
  95. .SemanticHeader2(category2.Category)
  96. .Text(category2.Category)
  97. .FontSize(20)
  98. .FontColor(Colors.Blue.Darken2)
  99. .Bold();
  100. foreach (var category3 in category2.Terms)
  101. {
  102. column.Item()
  103. .SemanticSection()
  104. .EnsureSpace(100)
  105. .Column(column =>
  106. {
  107. column.Spacing(8);
  108. column.Item()
  109. .PaddingBottom(8)
  110. .SemanticHeader3(category3.Category)
  111. .Text(category3.Category)
  112. .FontSize(16)
  113. .FontColor(Colors.Blue.Medium)
  114. .Bold();
  115. foreach (var term in category3.Terms)
  116. {
  117. column.Item()
  118. .SemanticParagraph()
  119. .Text(text =>
  120. {
  121. text.Span(term.Term).Bold();
  122. text.Span(" - ");
  123. text.Span(term.Description);
  124. });
  125. }
  126. });
  127. }
  128. });
  129. }
  130. });
  131. column.Item().PageBreak();
  132. }
  133. });
  134. page.Footer()
  135. .AlignCenter()
  136. .Text(text =>
  137. {
  138. text.Span("Page ");
  139. text.CurrentPageNumber();
  140. text.Span(" of ");
  141. text.TotalPages();
  142. });
  143. });
  144. })
  145. .WithMetadata(new DocumentMetadata()
  146. {
  147. Title = "Programming Terms",
  148. Language = "en-US"
  149. })
  150. .GeneratePdfAndShow();
  151. }
  152. }