ElementExamples.cs 24 KB

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