ElementExamples.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using QuestPDF.Examples.Engine;
  5. using QuestPDF.Fluent;
  6. using QuestPDF.Helpers;
  7. using QuestPDF.Infrastructure;
  8. using SkiaSharp;
  9. namespace QuestPDF.Examples
  10. {
  11. [TestFixture]
  12. public class ElementExamples
  13. {
  14. [Test]
  15. public void Placeholder()
  16. {
  17. RenderingTest
  18. .Create()
  19. .PageSize(200, 150)
  20. .Render(container =>
  21. {
  22. container
  23. .Background("#FFF")
  24. .Padding(25)
  25. .Placeholder();
  26. });
  27. }
  28. [Test]
  29. public void Decoration()
  30. {
  31. RenderingTest
  32. .Create()
  33. .PageSize(300, 300)
  34. .Render(container =>
  35. {
  36. container
  37. .Background("#FFF")
  38. .Padding(25)
  39. .Decoration(decoration =>
  40. {
  41. decoration
  42. .Header()
  43. .Background(Colors.Grey.Medium)
  44. .Padding(10)
  45. .Text("Notes", TextStyle.Default.Size(16).Color("#FFF"));
  46. decoration
  47. .Content()
  48. .Background(Colors.Grey.Lighten3)
  49. .Padding(10)
  50. .ExtendVertical()
  51. .Text(Helpers.Placeholders.LoremIpsum());
  52. });
  53. });
  54. }
  55. [Test]
  56. public void Page()
  57. {
  58. RenderingTest
  59. .Create()
  60. .PageSize(298, 421)
  61. .Render(container =>
  62. {
  63. container
  64. .Background("#FFF")
  65. .Padding(15)
  66. .Page(page =>
  67. {
  68. page.Header()
  69. .Height(60)
  70. .Background(Colors.Grey.Lighten1)
  71. .AlignCenter()
  72. .AlignMiddle()
  73. .Text("Header");
  74. page.Content()
  75. .Background(Colors.Grey.Lighten2)
  76. .AlignCenter()
  77. .AlignMiddle()
  78. .Text("Content");
  79. page.Footer()
  80. .Height(30)
  81. .Background(Colors.Grey.Lighten1)
  82. .AlignCenter()
  83. .AlignMiddle()
  84. .Text("Footer");
  85. });
  86. });
  87. }
  88. [Test]
  89. public void Row()
  90. {
  91. RenderingTest
  92. .Create()
  93. .PageSize(740, 200)
  94. .Render(container =>
  95. {
  96. container
  97. .Background("#FFF")
  98. .Padding(20)
  99. .Stack(stack =>
  100. {
  101. stack.Item()
  102. .PaddingBottom(10)
  103. .AlignCenter()
  104. .Text("This Row element is 700pt wide");
  105. stack.Item().Row(row =>
  106. {
  107. row.ConstantColumn(100)
  108. .Background(Colors.Grey.Lighten1)
  109. .Padding(10)
  110. .ExtendVertical()
  111. .Text("This column is 100 pt wide");
  112. row.RelativeColumn()
  113. .Background(Colors.Grey.Lighten2)
  114. .Padding(10)
  115. .Text("This column takes 1/3 of the available space (200pt)");
  116. row.RelativeColumn(2)
  117. .Background(Colors.Grey.Lighten3)
  118. .Padding(10)
  119. .Text("This column takes 2/3 of the available space (400pt)");
  120. });
  121. });
  122. });
  123. }
  124. [Test]
  125. public void RowSpacing()
  126. {
  127. RenderingTest
  128. .Create()
  129. .PageSize(740, 200)
  130. .Render(container =>
  131. {
  132. container
  133. .Background("#FFF")
  134. .Padding(20)
  135. .Row(row =>
  136. {
  137. row.Spacing(20);
  138. row.RelativeColumn(2).Border(1).Background(Colors.Grey.Lighten1);
  139. row.RelativeColumn(3).Border(1).Background(Colors.Grey.Lighten2);
  140. row.RelativeColumn(4).Border(1).Background(Colors.Grey.Lighten3);
  141. });
  142. });
  143. }
  144. [Test]
  145. public void Stack()
  146. {
  147. RenderingTest
  148. .Create()
  149. .PageSize(500, 360)
  150. .Render(container =>
  151. {
  152. container
  153. .Background("#FFF")
  154. .Padding(15)
  155. .Stack(stack =>
  156. {
  157. stack.Spacing(15);
  158. stack.Item().Background(Colors.Grey.Medium).Height(50);
  159. stack.Item().Background(Colors.Grey.Lighten1).Height(100);
  160. stack.Item().Background(Colors.Grey.Lighten2).Height(150);
  161. });
  162. });
  163. }
  164. [Test]
  165. public void Debug()
  166. {
  167. RenderingTest
  168. .Create()
  169. .PageSize(210, 210)
  170. .Render(container =>
  171. {
  172. container
  173. .Padding(25)
  174. .Debug("Grid example", Colors.Blue.Medium)
  175. .Grid(grid =>
  176. {
  177. grid.Columns(3);
  178. grid.Spacing(5);
  179. foreach (var _ in Enumerable.Range(0, 8))
  180. grid.Item().Height(50).Placeholder();
  181. });
  182. });
  183. }
  184. [Test]
  185. public void ElementEnd()
  186. {
  187. RenderingTest
  188. .Create()
  189. .PageSize(300, 200)
  190. .Render(container =>
  191. {
  192. var text = "";
  193. container
  194. .Padding(10)
  195. .Element(x =>
  196. {
  197. if (string.IsNullOrWhiteSpace(text))
  198. x.Height(10).Width(50).Background("#DDD");
  199. else
  200. x.Text(text);
  201. });
  202. });
  203. }
  204. [Test]
  205. public void GridExample()
  206. {
  207. RenderingTest
  208. .Create()
  209. .PageSize(400, 230)
  210. .Render(container =>
  211. {
  212. var textStyle = TextStyle.Default.Size(14);
  213. container
  214. .Padding(15)
  215. .AlignRight()
  216. .Grid(grid =>
  217. {
  218. grid.VerticalSpacing(15);
  219. grid.HorizontalSpacing(15);
  220. grid.AlignCenter();
  221. grid.Columns(10); // 12 by default
  222. grid.Item(6).Background(Colors.Blue.Lighten1).Height(50);
  223. grid.Item(4).Background(Colors.Blue.Lighten3).Height(50);
  224. grid.Item(2).Background(Colors.Teal.Lighten1).Height(70);
  225. grid.Item(3).Background(Colors.Teal.Lighten2).Height(70);
  226. grid.Item(5).Background(Colors.Teal.Lighten3).Height(70);
  227. grid.Item(2).Background(Colors.Green.Lighten1).Height(50);
  228. grid.Item(2).Background(Colors.Green.Lighten2).Height(50);
  229. grid.Item(2).Background(Colors.Green.Lighten3).Height(50);
  230. });
  231. });
  232. }
  233. [Test]
  234. public void Canvas()
  235. {
  236. RenderingTest
  237. .Create()
  238. .PageSize(300, 200)
  239. .Render(container =>
  240. {
  241. container
  242. .Background("#FFF")
  243. .Padding(25)
  244. .Canvas((canvas, size) =>
  245. {
  246. using var paint = new SKPaint
  247. {
  248. Color = SKColors.Red,
  249. StrokeWidth = 10,
  250. IsStroke = true
  251. };
  252. // move origin to the center of the available space
  253. canvas.Translate(size.Width / 2, size.Height / 2);
  254. // draw a circle
  255. canvas.DrawCircle(0, 0, 50, paint);
  256. });
  257. });
  258. }
  259. [Test]
  260. public void LayersExample()
  261. {
  262. RenderingTest
  263. .Create()
  264. .PageSize(400, 250)
  265. .Render(container =>
  266. {
  267. container
  268. .Padding(25)
  269. .Layers(layers =>
  270. {
  271. // layer below main content
  272. layers
  273. .Layer()
  274. .Height(100)
  275. .Width(100)
  276. .Background(Colors.Grey.Lighten3);
  277. layers
  278. .PrimaryLayer()
  279. .Padding(25)
  280. .Stack(stack =>
  281. {
  282. stack.Spacing(5);
  283. foreach (var _ in Enumerable.Range(0, 7))
  284. stack.Item().Text(Placeholders.Sentence());
  285. });
  286. // layer above the main content
  287. layers
  288. .Layer()
  289. .AlignCenter()
  290. .AlignMiddle()
  291. .Text("Watermark", TextStyle.Default.Size(48).Bold().Color(Colors.Green.Lighten3));
  292. layers
  293. .Layer()
  294. .AlignBottom()
  295. .PageNumber("Page {number}", TextStyle.Default.Size(16).Color(Colors.Green.Medium));
  296. });
  297. });
  298. }
  299. [Test]
  300. public void EnsureSpace()
  301. {
  302. RenderingTest
  303. .Create()
  304. .PageSize(300, 400)
  305. .Render(container =>
  306. {
  307. container
  308. .Padding(50)
  309. .Page(page =>
  310. {
  311. page.Header().PageNumber("Page {number}");
  312. page.Content().Height(300).Stack(content =>
  313. {
  314. content.Item().Height(200).Background(Colors.Grey.Lighten2);
  315. content.Item().EnsureSpace(100).Stack(stack =>
  316. {
  317. stack.Spacing(10);
  318. foreach (var _ in Enumerable.Range(0, 4))
  319. stack.Item().Height(50).Background(Colors.Green.Lighten1);
  320. });
  321. });
  322. });
  323. });
  324. }
  325. [Test]
  326. public void RandomColorMatrix()
  327. {
  328. RenderingTest
  329. .Create()
  330. .PageSize(300, 300)
  331. .Render(container =>
  332. {
  333. container
  334. .Padding(25)
  335. .Grid(grid =>
  336. {
  337. grid.Columns(5);
  338. Enumerable
  339. .Range(0, 25)
  340. .Select(x => Placeholders.BackgroundColor())
  341. .ToList()
  342. .ForEach(x => grid.Item().Height(50).Background(x));
  343. });
  344. });
  345. }
  346. [Test]
  347. public void DefinedColors()
  348. {
  349. var colors = new[]
  350. {
  351. Colors.Green.Darken4,
  352. Colors.Green.Darken3,
  353. Colors.Green.Darken2,
  354. Colors.Green.Darken1,
  355. Colors.Green.Medium,
  356. Colors.Green.Lighten1,
  357. Colors.Green.Lighten2,
  358. Colors.Green.Lighten3,
  359. Colors.Green.Lighten4,
  360. Colors.Green.Lighten5,
  361. Colors.Green.Accent1,
  362. Colors.Green.Accent2,
  363. Colors.Green.Accent3,
  364. Colors.Green.Accent4,
  365. };
  366. RenderingTest
  367. .Create()
  368. .PageSize(450, 150)
  369. .Render(container =>
  370. {
  371. container
  372. .Padding(25)
  373. .Height(100)
  374. .Row(row =>
  375. {
  376. foreach (var color in colors)
  377. row.RelativeColumn().Background(color);
  378. });
  379. });
  380. }
  381. [Test]
  382. public void DefinedFonts()
  383. {
  384. var fonts = new[]
  385. {
  386. Fonts.Calibri,
  387. Fonts.Candara,
  388. Fonts.Arial,
  389. Fonts.TimesNewRoman,
  390. Fonts.Consolas,
  391. Fonts.Tahoma,
  392. Fonts.Impact,
  393. Fonts.Trebuchet,
  394. Fonts.ComicSans
  395. };
  396. RenderingTest
  397. .Create()
  398. .PageSize(500, 175)
  399. .Render(container =>
  400. {
  401. container
  402. .Padding(25)
  403. .Grid(grid =>
  404. {
  405. grid.Columns(3);
  406. foreach (var font in fonts)
  407. {
  408. grid.Item()
  409. .Border(1)
  410. .BorderColor(Colors.Grey.Medium)
  411. .Padding(10)
  412. .Text(font, TextStyle.Default.FontType(font).Size(16));
  413. }
  414. });
  415. });
  416. }
  417. [Test]
  418. public void Layers()
  419. {
  420. RenderingTest
  421. .Create()
  422. .PageSize(300, 300)
  423. .Render(container =>
  424. {
  425. container
  426. .Background("#FFF")
  427. .Padding(25)
  428. .Layers(layers =>
  429. {
  430. layers.Layer().Text("Something else");
  431. layers.PrimaryLayer().Stack(stack =>
  432. {
  433. stack.Item().PaddingTop(20).Text("Text 1");
  434. stack.Item().PaddingTop(40).Text("Text 2");
  435. });
  436. layers.Layer().Canvas((canvas, size) =>
  437. {
  438. using var paint = new SKPaint
  439. {
  440. Color = SKColors.Red,
  441. StrokeWidth = 5
  442. };
  443. canvas.Translate(size.Width / 2, size.Height / 2);
  444. canvas.DrawCircle(0, 0, 50, paint);
  445. });
  446. layers.Layer().Background("#8F00").Extend();
  447. layers.Layer().PaddingTop(40).Text("It works!", TextStyle.Default.Size(24));
  448. });
  449. });
  450. }
  451. [Test]
  452. public void Box()
  453. {
  454. RenderingTest
  455. .Create()
  456. .PageSize(300, 150)
  457. .Render(container =>
  458. {
  459. container
  460. .Background("#FFF")
  461. .Padding(15)
  462. .Border(4)
  463. .BorderColor(Colors.Blue.Medium)
  464. //.Box()
  465. .Background(Colors.Grey.Lighten2)
  466. .Padding(15)
  467. .Text("Test of the \n box element", TextStyle.Default.Size(20));
  468. });
  469. }
  470. [Test]
  471. public void Scale()
  472. {
  473. RenderingTest
  474. .Create()
  475. .PageSize(400, 400)
  476. .Render(container =>
  477. {
  478. var style = TextStyle.Default.Size(20);
  479. container
  480. .Background("#FFF")
  481. .Padding(25)
  482. .Stack(stack =>
  483. {
  484. stack.Spacing(5);
  485. stack.Item().Background(Placeholders.BackgroundColor()).Scale(0.5f).Text("Smaller text", style);
  486. stack.Item().Background(Placeholders.BackgroundColor()).Text("Normal text", style);
  487. stack.Item().Background(Placeholders.BackgroundColor()).Scale(2f).Text("Bigger text", style);
  488. stack.Item().Background(Placeholders.BackgroundColor()).Scale(-1.5f).Text("Flipped text", style);
  489. });
  490. });
  491. }
  492. [Test]
  493. public void Translate()
  494. {
  495. RenderingTest
  496. .Create()
  497. .PageSize(500, 300)
  498. .Render(container =>
  499. {
  500. container
  501. .Background("#FFF")
  502. .Padding(25)
  503. .Box()
  504. .Background(Colors.Green.Lighten4)
  505. .Padding(5)
  506. .TranslateX(15)
  507. .TranslateY(15)
  508. .Text("Text outside of bounds");
  509. });
  510. }
  511. [Test]
  512. public void Rotate()
  513. {
  514. RenderingTest
  515. .Create()
  516. .PageSize(450, 450)
  517. .Render(container =>
  518. {
  519. container
  520. .Background("#FFF")
  521. .Padding(20)
  522. .Grid(grid =>
  523. {
  524. grid.Columns(2);
  525. grid.Spacing(10);
  526. foreach (var turns in Enumerable.Range(0, 4))
  527. {
  528. grid.Item()
  529. .Width(200)
  530. .Height(200)
  531. .Background(Colors.Grey.Lighten3)
  532. .Padding(10)
  533. //.Box()
  534. .Element(element =>
  535. {
  536. foreach (var x in Enumerable.Range(0, turns))
  537. element = element.RotateRight();
  538. return element;
  539. })
  540. .Text($"Rotated {turns * 90} degrees.", TextStyle.Default.Size(14));
  541. }
  542. });
  543. });
  544. }
  545. [Test]
  546. public void Flip()
  547. {
  548. RenderingTest
  549. .Create()
  550. .PageSize(450, 450)
  551. .Render(container =>
  552. {
  553. container
  554. .Background("#FFF")
  555. .Padding(20)
  556. .Grid(grid =>
  557. {
  558. grid.Columns(2);
  559. grid.Spacing(10);
  560. foreach (var turns in Enumerable.Range(0, 4))
  561. {
  562. grid.Item()
  563. .Width(200)
  564. .Height(200)
  565. .Background(Colors.Grey.Lighten3)
  566. .Padding(10)
  567. .Element(element =>
  568. {
  569. if (turns == 1 || turns == 2)
  570. element = element.FlipX();
  571. if (turns == 2 || turns == 3)
  572. element = element.FlipY();
  573. return element;
  574. })
  575. .Text($"Flipped.", TextStyle.Default.Size(14));
  576. }
  577. });
  578. });
  579. }
  580. }
  581. }