TableOfContentsTests.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using QuestPDF.ConformanceTests.TestEngine;
  2. using QuestPDF.Drawing;
  3. using QuestPDF.Fluent;
  4. using QuestPDF.Helpers;
  5. using QuestPDF.Infrastructure;
  6. namespace QuestPDF.ConformanceTests;
  7. internal class TableOfContentsTests : ConformanceTestBase
  8. {
  9. protected override Document GetDocumentUnderTest()
  10. {
  11. return Document
  12. .Create(document =>
  13. {
  14. document.Page(page =>
  15. {
  16. page.Margin(60);
  17. page.Content()
  18. .PaddingVertical(30)
  19. .Column(column =>
  20. {
  21. column.Item()
  22. .ExtendVertical()
  23. .AlignMiddle()
  24. .SemanticHeader1()
  25. .Text("Conformance Test:\nTable of Contents")
  26. .FontSize(36)
  27. .Bold()
  28. .FontColor(Colors.Blue.Darken2);
  29. column.Item().PageBreak();
  30. column.Item().Element(GenerateTableOfContentsSection);
  31. column.Item().PageBreak();
  32. column.Item().Element(GeneratePlaceholderContentSection);
  33. });
  34. });
  35. });
  36. static void GenerateTableOfContentsSection(IContainer container)
  37. {
  38. container
  39. .SemanticSection()
  40. .Column(column =>
  41. {
  42. column.Spacing(15);
  43. column
  44. .Item()
  45. .Text("Table of Contents")
  46. .Bold()
  47. .FontSize(20)
  48. .FontColor(Colors.Blue.Medium);
  49. column.Item()
  50. .SemanticTableOfContents()
  51. .Column(column =>
  52. {
  53. column.Spacing(5);
  54. foreach (var i in Enumerable.Range(1, 10))
  55. {
  56. column.Item()
  57. .SemanticTableOfContentsItem()
  58. .SemanticLink($"Link to section {i}")
  59. .SectionLink($"section-{i}")
  60. .Row(row =>
  61. {
  62. row.ConstantItem(25).Text($"{i}.");
  63. row.AutoItem().Text(Placeholders.Label());
  64. row.RelativeItem().PaddingHorizontal(2).TranslateY(11).LineHorizontal(1).LineDashPattern([1, 3]);
  65. row.AutoItem().Text(text => text.BeginPageNumberOfSection($"section-{i}"));
  66. });
  67. }
  68. });
  69. });
  70. }
  71. static void GeneratePlaceholderContentSection(IContainer container)
  72. {
  73. container
  74. .Column(column =>
  75. {
  76. foreach (var i in Enumerable.Range(1, 10))
  77. {
  78. column.Item()
  79. .SemanticSection()
  80. .Section($"section-{i}")
  81. .Column(column =>
  82. {
  83. column.Spacing(15);
  84. column.Item()
  85. .SemanticHeader2()
  86. .Text($"Section {i}")
  87. .Bold()
  88. .FontSize(20)
  89. .FontColor(Colors.Blue.Medium);
  90. column.Item().Text(Placeholders.Paragraph());
  91. foreach (var j in Enumerable.Range(1, i))
  92. {
  93. column.Item()
  94. .SemanticIgnore()
  95. .Width(200)
  96. .Height(150)
  97. .CornerRadius(10)
  98. .Background(Placeholders.BackgroundColor());
  99. }
  100. });
  101. if (i < 10)
  102. column.Item().PageBreak();
  103. }
  104. });
  105. }
  106. }
  107. protected override SemanticTreeNode? GetExpectedSemanticTree()
  108. {
  109. return ExpectedSemanticTree.DocumentRoot(root =>
  110. {
  111. root.Child("H1", h1 => h1.Alt("Conformance Test:\nTable of Contents"));
  112. // Table of Contents Section
  113. root.Child("Sect", sect =>
  114. {
  115. sect.Child("P");
  116. sect.Child("TOC", toc =>
  117. {
  118. foreach (var i in Enumerable.Range(1, 10))
  119. {
  120. toc.Child("TOCI", toci =>
  121. {
  122. toci.Child("Link", link =>
  123. {
  124. link.Alt($"Link to section {i}");
  125. link.Child("P"); // Number
  126. link.Child("P"); // Label
  127. link.Child("P"); // Page number
  128. });
  129. });
  130. }
  131. });
  132. });
  133. // Content Sections
  134. foreach (var i in Enumerable.Range(1, 10))
  135. {
  136. root.Child("Sect", sect =>
  137. {
  138. sect.Child("H2", h2 => h2.Alt($"Section {i}"));
  139. sect.Child("P");
  140. });
  141. }
  142. });
  143. }
  144. }