ElementExamples.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System.Linq;
  2. using QuestPDF.Examples.Engine;
  3. using QuestPDF.Fluent;
  4. using QuestPDF.Helpers;
  5. using QuestPDF.Infrastructure;
  6. using SkiaSharp;
  7. namespace QuestPDF.Examples
  8. {
  9. public class ElementExamples : ExampleTestBase
  10. {
  11. //[ShowResult]
  12. [ImageSize(200, 150)]
  13. public void Placeholder(IContainer container)
  14. {
  15. container
  16. .Background("#FFF")
  17. .Padding(25)
  18. .Placeholder();
  19. }
  20. //[ShowResult]
  21. [ImageSize(300, 300)]
  22. public void Decoration(IContainer container)
  23. {
  24. container
  25. .Background("#FFF")
  26. .Padding(25)
  27. .Decoration(decoration =>
  28. {
  29. decoration
  30. .Header()
  31. .Background("#888")
  32. .Padding(10)
  33. .Text("Notes", TextStyle.Default.Size(16).Color("#FFF"));
  34. decoration
  35. .Content()
  36. .Background("#DDD")
  37. .Padding(10)
  38. .ExtendVertical()
  39. .Text(Helpers.Placeholders.LoremIpsum());
  40. });
  41. }
  42. //[ShowResult]
  43. [ImageSize(298, 421)]
  44. public void Page(IContainer container)
  45. {
  46. container
  47. .Background("#FFF")
  48. .Padding(15)
  49. .Page(page =>
  50. {
  51. page.Header()
  52. .Height(60)
  53. .Background("#BBB")
  54. .AlignCenter()
  55. .AlignMiddle()
  56. .Text("Header");
  57. page.Content()
  58. .Background("#DDD")
  59. .AlignCenter()
  60. .AlignMiddle()
  61. .Text("Content");
  62. page.Footer()
  63. .Height(30)
  64. .Background("#BBB")
  65. .AlignCenter()
  66. .AlignMiddle()
  67. .Text("Footer");
  68. });
  69. }
  70. //[ShowResult]
  71. [ImageSize(740, 200)]
  72. public void Row(IContainer container)
  73. {
  74. container
  75. .Background("#FFF")
  76. .Padding(20)
  77. .Stack(stack =>
  78. {
  79. stack.Item()
  80. .PaddingBottom(10)
  81. .AlignCenter()
  82. .Text("This Row element is 700pt wide");
  83. stack.Item().Row(row =>
  84. {
  85. row.ConstantColumn(100)
  86. .Background("#DDD")
  87. .Padding(10)
  88. .ExtendVertical()
  89. .Text("This column is 100 pt wide");
  90. row.RelativeColumn()
  91. .Background("#BBB")
  92. .Padding(10)
  93. .Text("This column takes 1/3 of the available space (200pt)");
  94. row.RelativeColumn(2)
  95. .Background("#DDD")
  96. .Padding(10)
  97. .Text("This column takes 2/3 of the available space (400pt)");
  98. });
  99. });
  100. }
  101. //[ShowResult]
  102. [ImageSize(500, 350)]
  103. public void Column(IContainer container)
  104. {
  105. container
  106. .Background("#FFF")
  107. .Padding(15)
  108. .Stack(column =>
  109. {
  110. column.Spacing(10);
  111. column
  112. .Item()
  113. .Background("#999")
  114. .Height(50);
  115. column
  116. .Item()
  117. .Background("#BBB")
  118. .Height(100);
  119. column
  120. .Item()
  121. .Background("#DDD")
  122. .Height(150);
  123. });
  124. }
  125. [ShowResult]
  126. [ImageSize(210, 210)]
  127. public void Debug(IContainer container)
  128. {
  129. container
  130. .Padding(25)
  131. .Debug("Grid example", Colors.Blue.Medium)
  132. .Grid(grid =>
  133. {
  134. grid.Columns(3);
  135. grid.Spacing(5);
  136. foreach (var _ in Enumerable.Range(0, 8))
  137. grid.Item().Height(50).Placeholder();
  138. });
  139. }
  140. //[ShowResult]
  141. [ImageSize(300, 200)]
  142. public void ElementEnd(IContainer container)
  143. {
  144. var text = "";
  145. container
  146. .Padding(10)
  147. .Element(x =>
  148. {
  149. if (string.IsNullOrWhiteSpace(text))
  150. x.Height(10).Width(50).Background("#DDD");
  151. else
  152. x.Text(text);
  153. });
  154. }
  155. //[ShowResult]
  156. [ImageSize(300, 200)]
  157. public void GridExample(IContainer container)
  158. {
  159. var textStyle = TextStyle.Default.Size(14);
  160. container
  161. .Padding(20)
  162. .AlignRight()
  163. .Grid(grid =>
  164. {
  165. grid.VerticalSpacing(20);
  166. grid.HorizontalSpacing(10);
  167. grid.Columns(12);
  168. grid.Item(8).Background("#DDD").Height(50).Padding(5).Text("This is a short text", textStyle);
  169. grid.Item(4).Background("#BBB").Padding(5).Text("Showing how to...", textStyle);
  170. grid.Item(2).Background("#999").Height(50);
  171. grid.Item(4).Background("#AAA").Border(2).BorderColor("#666").Padding(5).Text("...generate", textStyle);
  172. grid.Item(6).Background("#CCC").Padding(5).Text("simple grids", textStyle.Size(18).Bold());
  173. grid.Item(8).Background("#DDD").Height(50);
  174. });
  175. }
  176. //[ShowResult]
  177. [ImageSize(300, 300)]
  178. public void RandomColorMatrix(IContainer container)
  179. {
  180. container
  181. .Padding(25)
  182. .Grid(grid =>
  183. {
  184. grid.Columns(5);
  185. Enumerable
  186. .Range(0, 25)
  187. .Select(x => Placeholders.BackgroundColor())
  188. .ToList()
  189. .ForEach(x => grid.Item().Height(50).Background(x));
  190. });
  191. }
  192. //[ShowResult]
  193. [ImageSize(450, 150)]
  194. public void DefinedColors(IContainer container)
  195. {
  196. var colors = new[]
  197. {
  198. Colors.Green.Darken4,
  199. Colors.Green.Darken3,
  200. Colors.Green.Darken2,
  201. Colors.Green.Darken1,
  202. Colors.Green.Medium,
  203. Colors.Green.Lighten1,
  204. Colors.Green.Lighten2,
  205. Colors.Green.Lighten3,
  206. Colors.Green.Lighten4,
  207. Colors.Green.Lighten5,
  208. Colors.Green.Accent1,
  209. Colors.Green.Accent2,
  210. Colors.Green.Accent3,
  211. Colors.Green.Accent4,
  212. };
  213. container
  214. .Padding(25)
  215. .Height(100)
  216. .Row(row =>
  217. {
  218. foreach (var color in colors)
  219. row.RelativeColumn().Background(color);
  220. });
  221. }
  222. //[ShowResult]
  223. [ImageSize(500, 175)]
  224. public void DefinedFonts(IContainer container)
  225. {
  226. var fonts = new[]
  227. {
  228. Fonts.Calibri,
  229. Fonts.Candara,
  230. Fonts.Arial,
  231. Fonts.TimesNewRoman,
  232. Fonts.Consolas,
  233. Fonts.Tahoma,
  234. Fonts.Impact,
  235. Fonts.Trebuchet,
  236. Fonts.ComicSans
  237. };
  238. container
  239. .Padding(25)
  240. .Grid(grid =>
  241. {
  242. grid.Columns(3);
  243. foreach (var font in fonts)
  244. {
  245. grid.Item()
  246. .Border(1)
  247. .BorderColor(Colors.Grey.Medium)
  248. .Padding(10)
  249. .Text(font, TextStyle.Default.FontType(font).Size(16));
  250. }
  251. });
  252. }
  253. //[ShowResult]
  254. [ImageSize(300, 300)]
  255. public void Layers(IContainer container)
  256. {
  257. container
  258. .Background("#FFF")
  259. .Padding(25)
  260. .Layers(layers =>
  261. {
  262. layers.Layer().Text("Something else");
  263. layers.PrimaryLayer().Stack(stack =>
  264. {
  265. stack.Item().PaddingTop(20).Text("Text 1");
  266. stack.Item().PaddingTop(40).Text("Text 2");
  267. });
  268. layers.Layer().Canvas((canvas, size) =>
  269. {
  270. using var paint = new SKPaint
  271. {
  272. Color = SKColors.Red,
  273. StrokeWidth = 5
  274. };
  275. canvas.Translate(size.Width / 2, size.Height / 2);
  276. canvas.DrawCircle(0, 0, 50, paint);
  277. });
  278. layers.Layer().Background("#8F00").Extend();
  279. layers.Layer().PaddingTop(40).Text("It works!", TextStyle.Default.Size(24));
  280. });
  281. }
  282. }
  283. }