ContentOverflowVisualizationExamples.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System.Linq;
  2. using NUnit.Framework;
  3. using QuestPDF.Examples.Engine;
  4. using QuestPDF.Fluent;
  5. using QuestPDF.Helpers;
  6. using QuestPDF.Infrastructure;
  7. namespace QuestPDF.Examples;
  8. public class ContentOverflowVisualizationExamples
  9. {
  10. [Test]
  11. public void DrawOverflow()
  12. {
  13. RenderingTest
  14. .Create()
  15. .ShowResults()
  16. .ProducePdf()
  17. .EnableDebugging()
  18. .RenderDocument(document =>
  19. {
  20. document.Page(page =>
  21. {
  22. page.Size(PageSizes.A4);
  23. page.Margin(24);
  24. page.DefaultTextStyle(TextStyle.Default.FontSize(16));
  25. page.Header().Text("Document header").FontSize(24).Bold().FontColor(Colors.Blue.Accent2);
  26. page.Content().PaddingVertical(24).Column(column =>
  27. {
  28. column.Spacing(16);
  29. foreach (var size in Enumerable.Range(20, 20))
  30. column.Item().Width(size * 10).Height(40).Background(Colors.Grey.Lighten3);
  31. column.Item().Row(row =>
  32. {
  33. row.RelativeItem().Border(1).Background(Colors.Grey.Lighten3).Padding(5).Text("Will it work?").FontSize(20);
  34. row.RelativeItem().Border(1).Background(Colors.Grey.Lighten3).Padding(5).LayoutOverflowVisualization().Height(100).ShowEntire().Text(Placeholders.LoremIpsum()).FontSize(20);
  35. });
  36. foreach (var size in Enumerable.Range(20, 20))
  37. column.Item().Width(size * 10).Height(40).Background(Colors.Grey.Lighten3);
  38. });
  39. page.Footer().AlignCenter().Text(text =>
  40. {
  41. text.CurrentPageNumber();
  42. text.Span(" / ");
  43. text.TotalPages();
  44. });
  45. });
  46. });
  47. }
  48. [Test]
  49. public void DrawOverflowRealExample()
  50. {
  51. var image = Placeholders.Image(400, 300);
  52. RenderingTest
  53. .Create()
  54. .ShowResults()
  55. .PageSize(PageSizes.A4)
  56. .ProducePdf()
  57. .EnableDebugging()
  58. .Render(container =>
  59. {
  60. container.Column(column =>
  61. {
  62. foreach (var i in Enumerable.Range(0, 50))
  63. column.Item().Height(30).Width(i * 5 + 100).Background(Placeholders.BackgroundColor());
  64. column.Item()
  65. .Padding(24)
  66. // constrain area to square 200 x 200
  67. .Width(200)
  68. .Height(200)
  69. .Background(Colors.Grey.Lighten3)
  70. // draw image that fits height (and therefore will overflow)
  71. .LayoutOverflowVisualization()
  72. .Image(image)
  73. .FitHeight();
  74. foreach (var i in Enumerable.Range(0, 50))
  75. column.Item().Height(30).Width(i * 5 + 100).Background(Placeholders.BackgroundColor());
  76. });
  77. });
  78. }
  79. [Test]
  80. public void DrawOverflowSimpleExample()
  81. {
  82. var image = Placeholders.Image(400, 300);
  83. RenderingTest
  84. .Create()
  85. .ShowResults()
  86. .PageSize(PageSizes.A4)
  87. .EnableDebugging()
  88. .ProducePdf()
  89. .Render(container =>
  90. {
  91. container
  92. .Padding(24)
  93. // constrain area to square 200 x 200
  94. .Width(200)
  95. .Height(200)
  96. .Background(Colors.Grey.Lighten3)
  97. // draw image that fits height (and therefore will overflow)
  98. .LayoutOverflowVisualization()
  99. .Image(image)
  100. .FitHeight();
  101. });
  102. }
  103. [Test]
  104. public void DrawOverflowCases()
  105. {
  106. RenderingTest
  107. .Create()
  108. .ShowResults()
  109. .PageSize(PageSizes.A4)
  110. .EnableDebugging()
  111. .ProducePdf()
  112. .Render(container =>
  113. {
  114. container.Padding(24).Row(row =>
  115. {
  116. row.Spacing(50);
  117. row.RelativeItem().ContentFromLeftToRight().Element(GenerateOverflowPatterns);
  118. row.RelativeItem().ContentFromRightToLeft().Element(GenerateOverflowPatterns);
  119. });
  120. void GenerateOverflowPatterns(IContainer container)
  121. {
  122. container.Column(column =>
  123. {
  124. column.Spacing(50);
  125. column
  126. .Item()
  127. .Element(DrawTestcaseArea)
  128. .Width(50)
  129. .Height(150)
  130. .Text("Test");
  131. column
  132. .Item()
  133. .Element(DrawTestcaseArea)
  134. .Width(150)
  135. .Height(50)
  136. .Text("Test");
  137. column
  138. .Item()
  139. .Element(DrawTestcaseArea)
  140. .Width(200)
  141. .Height(150)
  142. .Text("Test");
  143. IContainer DrawTestcaseArea(IContainer container)
  144. {
  145. return container
  146. .Height(200)
  147. .Background(Colors.Grey.Lighten4)
  148. .Width(100)
  149. .Height(100)
  150. .LayoutOverflowVisualization()
  151. .Background(Colors.Grey.Lighten1);
  152. }
  153. });
  154. }
  155. });
  156. }
  157. }