ElementExamples.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using QuestPDF.Examples.Engine;
  2. using QuestPDF.Fluent;
  3. using QuestPDF.Infrastructure;
  4. using SkiaSharp;
  5. namespace QuestPDF.Examples
  6. {
  7. public class ElementExamples : ExampleTestBase
  8. {
  9. //[ShowResult]
  10. [ImageSize(200, 150)]
  11. public void Placeholder(IContainer container)
  12. {
  13. container
  14. .Background("#FFF")
  15. .Padding(25)
  16. .Placeholder();
  17. }
  18. //[ShowResult]
  19. [ImageSize(300, 300)]
  20. public void Section(IContainer container)
  21. {
  22. container
  23. .Background("#FFF")
  24. .Padding(25)
  25. .Section(section =>
  26. {
  27. section
  28. .Header()
  29. .Background("#888")
  30. .Padding(10)
  31. .Text("Notes", TextStyle.Default.Size(16).Color("#FFF"));
  32. section
  33. .Content()
  34. .Background("#DDD")
  35. .Padding(10)
  36. .ExtendVertical()
  37. .Text(Helpers.Placeholders.LoremIpsum());
  38. });
  39. }
  40. //[ShowResult]
  41. [ImageSize(298, 421)]
  42. public void Page(IContainer container)
  43. {
  44. container
  45. .Background("#FFF")
  46. .Padding(15)
  47. .Page(page =>
  48. {
  49. page.Header()
  50. .Height(60)
  51. .Background("#BBB")
  52. .AlignCenter()
  53. .AlignMiddle()
  54. .Text("Header");
  55. page.Content()
  56. .Background("#DDD")
  57. .AlignCenter()
  58. .AlignMiddle()
  59. .Text("Content");
  60. page.Footer()
  61. .Height(30)
  62. .Background("#BBB")
  63. .AlignCenter()
  64. .AlignMiddle()
  65. .Text("Footer");
  66. });
  67. }
  68. //[ShowResult]
  69. [ImageSize(740, 200)]
  70. public void Row(IContainer container)
  71. {
  72. container
  73. .Background("#FFF")
  74. .Padding(20)
  75. .Stack(stack =>
  76. {
  77. stack.Element()
  78. .PaddingBottom(10)
  79. .AlignCenter()
  80. .Text("This Row element is 700pt wide");
  81. stack.Element().Row(row =>
  82. {
  83. row.ConstantColumn(100)
  84. .Background("#DDD")
  85. .Padding(10)
  86. .ExtendVertical()
  87. .Text("This column is 100 pt wide");
  88. row.RelativeColumn()
  89. .Background("#BBB")
  90. .Padding(10)
  91. .Text("This column takes 1/3 of the available space (200pt)");
  92. row.RelativeColumn(2)
  93. .Background("#DDD")
  94. .Padding(10)
  95. .Text("This column takes 2/3 of the available space (400pt)");
  96. });
  97. });
  98. }
  99. //[ShowResult]
  100. [ImageSize(500, 350)]
  101. public void Column(IContainer container)
  102. {
  103. container
  104. .Background("#FFF")
  105. .Padding(15)
  106. .Stack(column =>
  107. {
  108. column.Spacing(10);
  109. column
  110. .Element()
  111. .Background("#999")
  112. .Height(50);
  113. column
  114. .Element()
  115. .Background("#BBB")
  116. .Height(100);
  117. column
  118. .Element()
  119. .Background("#DDD")
  120. .Height(150);
  121. });
  122. }
  123. //[ShowResult]
  124. [ImageSize(300, 200)]
  125. public void Debug(IContainer container)
  126. {
  127. container
  128. .Padding(25)
  129. .Debug()
  130. .Padding(-5)
  131. .Row(row =>
  132. {
  133. row.RelativeColumn().Padding(5).Extend().Placeholder();
  134. row.RelativeColumn().Padding(5).Extend().Placeholder();
  135. });
  136. }
  137. //[ShowResult]
  138. [ImageSize(300, 200)]
  139. public void ElementEnd(IContainer container)
  140. {
  141. var text = "";
  142. container
  143. .Padding(10)
  144. .Element(x =>
  145. {
  146. if (string.IsNullOrWhiteSpace(text))
  147. x.Height(10).Width(50).Background("#DDD");
  148. else
  149. x.Text(text);
  150. });
  151. }
  152. //[ShowResult]
  153. [ImageSize(300, 200)]
  154. public void GridExample(IContainer container)
  155. {
  156. var textStyle = TextStyle.Default.Size(14);
  157. container
  158. .Padding(20)
  159. .AlignRight()
  160. .Grid(grid =>
  161. {
  162. grid.Spacing(5);
  163. grid.Columns(12);
  164. grid.Element(8).Background("#DDD").Height(50).Padding(5).Text("This is a short text", textStyle);
  165. grid.Element(4).Background("#BBB").Padding(5).Text("Showing how to...", textStyle);
  166. grid.Element(2).Background("#999").Height(50);
  167. grid.Element(4).Background("#AAA").Border(2).BorderColor("#666").Padding(5).Text("...generate", textStyle);
  168. grid.Element(6).Background("#CCC").Padding(5).Text("simple grids", textStyle.Size(18).Bold());
  169. grid.Element(8).Background("#DDD").Height(50);
  170. });
  171. }
  172. [ShowResult]
  173. [ImageSize(300, 300)]
  174. public void Layers(IContainer container)
  175. {
  176. container
  177. .Background("#FFF")
  178. .Padding(25)
  179. .Layers(layers =>
  180. {
  181. layers.Layer().Text("Something else");
  182. layers.PrimaryLayer().Stack(stack =>
  183. {
  184. stack.Element().PaddingTop(20).Text("Text 1");
  185. stack.Element().PaddingTop(40).Text("Text 2");
  186. });
  187. layers.Layer().Canvas((canvas, size) =>
  188. {
  189. using var paint = new SKPaint
  190. {
  191. Color = SKColors.Red,
  192. StrokeWidth = 5
  193. };
  194. canvas.Translate(size.Width / 2, size.Height / 2);
  195. canvas.DrawCircle(0, 0, 50, paint);
  196. });
  197. layers.Layer().Background("#8F00").Extend();
  198. layers.Layer().PaddingTop(40).Text("It works!", TextStyle.Default.Size(24));
  199. });
  200. }
  201. }
  202. }