SemanticExamples.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. [Test]
  10. public void HeaderAndFooter()
  11. {
  12. Document
  13. .Create(document =>
  14. {
  15. document.Page(page =>
  16. {
  17. page.MinSize(new PageSize(0, 0));
  18. page.MaxSize(new PageSize(600, 250));
  19. page.DefaultTextStyle(x => x.FontSize(16));
  20. page.Margin(25);
  21. page.Content()
  22. .Border(1)
  23. .BorderColor(Colors.Grey.Lighten1)
  24. .SemanticTable()
  25. .Table(table =>
  26. {
  27. var pageSizes = new List<(string name, double width, double height)>()
  28. {
  29. ("Letter (ANSI A)", 8.5f, 11),
  30. ("Legal", 8.5f, 14),
  31. ("Ledger (ANSI B)", 11, 17),
  32. ("Tabloid (ANSI B)", 17, 11),
  33. ("ANSI C", 22, 17),
  34. ("ANSI D", 34, 22),
  35. ("ANSI E", 44, 34)
  36. };
  37. const int inchesToPoints = 72;
  38. IContainer DefaultCellStyle(IContainer container, string backgroundColor)
  39. {
  40. return container
  41. .Border(1)
  42. .BorderColor(Colors.Grey.Lighten1)
  43. .Background(backgroundColor)
  44. .PaddingVertical(5)
  45. .PaddingHorizontal(10)
  46. .AlignCenter()
  47. .AlignMiddle();
  48. }
  49. table.ColumnsDefinition(columns =>
  50. {
  51. columns.RelativeColumn();
  52. columns.ConstantColumn(80);
  53. columns.ConstantColumn(80);
  54. columns.ConstantColumn(80);
  55. columns.ConstantColumn(80);
  56. });
  57. table.Header(header =>
  58. {
  59. // please be sure to call the 'header' handler!
  60. header.Cell().RowSpan(2).Element(CellStyle).ExtendHorizontal().AlignLeft()
  61. .Text("Document type").Bold();
  62. header.Cell().ColumnSpan(2).Element(CellStyle).Text("Inches").Bold();
  63. header.Cell().ColumnSpan(2).Element(CellStyle).Text("Points").Bold();
  64. header.Cell().Element(CellStyle).Text("Width");
  65. header.Cell().Element(CellStyle).Text("Height");
  66. header.Cell().Element(CellStyle).Text("Width");
  67. header.Cell().Element(CellStyle).Text("Height");
  68. // you can extend existing styles by creating additional methods
  69. IContainer CellStyle(IContainer container) =>
  70. DefaultCellStyle(container, Colors.Grey.Lighten3);
  71. });
  72. foreach (var page in pageSizes)
  73. {
  74. table.Cell().Element(CellStyle).ExtendHorizontal().AlignLeft().Text(page.name);
  75. // inches
  76. table.Cell().Element(CellStyle).Text(page.width);
  77. table.Cell().Element(CellStyle).Text(page.height);
  78. // points
  79. table.Cell().Element(CellStyle).Text(page.width * inchesToPoints);
  80. table.Cell().Element(CellStyle).Text(page.height * inchesToPoints);
  81. IContainer CellStyle(IContainer container) =>
  82. DefaultCellStyle(container, Colors.White).ShowOnce();
  83. }
  84. });
  85. });
  86. })
  87. .GeneratePdf();
  88. }
  89. public class BookTermModel
  90. {
  91. public string Term { get; set; }
  92. public string Description { get; set; }
  93. public string FirstLevelCategory { get; set; }
  94. public string SecondLevelCategory { get; set; }
  95. public string ThirdLevelCategory { get; set; }
  96. }
  97. [Test]
  98. public async Task GenerateBook()
  99. {
  100. QuestPDF.Settings.EnableCaching = false;
  101. QuestPDF.Settings.EnableDebugging = false;
  102. var serializerSettings = new JsonSerializerOptions
  103. {
  104. PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
  105. DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
  106. WriteIndented = true
  107. };
  108. var bookData = await File.ReadAllTextAsync("Resources/semantic-book-content.json");
  109. var terms = JsonSerializer.Deserialize<ICollection<BookTermModel>>(bookData, serializerSettings);
  110. var categories = terms
  111. .GroupBy(x => x.FirstLevelCategory)
  112. .Select(x => new
  113. {
  114. Category = x.Key,
  115. Terms = x
  116. .GroupBy(y => y.SecondLevelCategory)
  117. .Select(y => new
  118. {
  119. Category = y.Key,
  120. Terms = y
  121. .GroupBy(z => z.ThirdLevelCategory)
  122. .Select(z => new
  123. {
  124. Category = z.Key,
  125. Terms = z.ToList()
  126. })
  127. .ToList()
  128. })
  129. .ToList()
  130. })
  131. .ToList();
  132. Document
  133. .Create(document =>
  134. {
  135. document.Page(page =>
  136. {
  137. page.Size(PageSizes.A4);
  138. page.DefaultTextStyle(x => x.FontSize(20));
  139. page.Margin(50);
  140. page.PageColor(Colors.White);
  141. page.Header()
  142. .Text("Programming Terms")
  143. .Bold()
  144. .FontSize(36);
  145. page.Content()
  146. .PaddingVertical(24)
  147. .Column(column =>
  148. {
  149. foreach (var category1 in categories)
  150. {
  151. column.Item()
  152. .SemanticSection()
  153. .EnsureSpace(100)
  154. .Column(column =>
  155. {
  156. column.Spacing(24);
  157. column.Item()
  158. .PaddingBottom(8)
  159. .SemanticHeader1()
  160. .Text(category1.Category)
  161. .FontSize(24)
  162. .FontColor(Colors.Blue.Darken4)
  163. .Bold();
  164. foreach (var category2 in category1.Terms)
  165. {
  166. column.Item()
  167. .SemanticSection()
  168. .EnsureSpace(100)
  169. .Column(column =>
  170. {
  171. column.Spacing(8);
  172. column.Item()
  173. .PaddingBottom(8)
  174. .SemanticHeader2()
  175. .Text(category2.Category)
  176. .FontSize(20)
  177. .FontColor(Colors.Blue.Darken2)
  178. .Bold();
  179. foreach (var category3 in category2.Terms)
  180. {
  181. column.Item()
  182. .SemanticSection()
  183. .EnsureSpace(100)
  184. .Column(column =>
  185. {
  186. column.Spacing(8);
  187. column.Item()
  188. .PaddingBottom(8)
  189. .SemanticHeader3()
  190. .Text(category3.Category)
  191. .FontSize(16)
  192. .FontColor(Colors.Blue.Medium)
  193. .Bold();
  194. foreach (var term in category3.Terms)
  195. {
  196. column.Item()
  197. .SemanticParagraph()
  198. .Text(text =>
  199. {
  200. text.Span(term.Term).Bold();
  201. text.Span(" - ");
  202. text.Span(term.Description);
  203. });
  204. }
  205. });
  206. }
  207. });
  208. }
  209. });
  210. column.Item().PageBreak();
  211. }
  212. });
  213. page.Footer()
  214. .AlignCenter()
  215. .Text(text =>
  216. {
  217. text.Span("Page ");
  218. text.CurrentPageNumber();
  219. text.Span(" of ");
  220. text.TotalPages();
  221. });
  222. });
  223. })
  224. .WithMetadata(new DocumentMetadata()
  225. {
  226. Title = "Programming Terms",
  227. Language = "en-US"
  228. })
  229. .GeneratePdf();
  230. }
  231. }