ElementExamples.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. using SkiaSharp;
  8. namespace QuestPDF.Examples
  9. {
  10. [TestFixture]
  11. public class ElementExamples
  12. {
  13. [Test]
  14. public void Placeholder()
  15. {
  16. RenderingTest
  17. .Create()
  18. .PageSize(200, 150)
  19. .Render(container =>
  20. {
  21. container
  22. .Background("#FFF")
  23. .Padding(25)
  24. .Placeholder();
  25. });
  26. }
  27. [Test]
  28. public void Decoration()
  29. {
  30. RenderingTest
  31. .Create()
  32. .PageSize(300, 300)
  33. .Render(container =>
  34. {
  35. container
  36. .Background("#FFF")
  37. .Padding(25)
  38. .Decoration(decoration =>
  39. {
  40. decoration
  41. .Before()
  42. .Background(Colors.Grey.Medium)
  43. .Padding(10)
  44. .Text("Notes")
  45. .FontSize(16)
  46. .FontColor("#FFF");
  47. decoration
  48. .Content()
  49. .Background(Colors.Grey.Lighten3)
  50. .Padding(10)
  51. .ExtendVertical()
  52. .Text(Helpers.Placeholders.LoremIpsum());
  53. });
  54. });
  55. }
  56. [Test]
  57. public void Row()
  58. {
  59. RenderingTest
  60. .Create()
  61. .PageSize(740, 200)
  62. .Render(container =>
  63. {
  64. container
  65. .Background("#FFF")
  66. .Padding(20)
  67. .Column(column =>
  68. {
  69. column.Item()
  70. .PaddingBottom(10)
  71. .AlignCenter()
  72. .Text("This Row element is 700pt wide");
  73. column.Item().Row(row =>
  74. {
  75. row.ConstantItem(100)
  76. .Background(Colors.Grey.Lighten1)
  77. .Padding(10)
  78. .ExtendVertical()
  79. .Text("This column is 100 pt wide");
  80. row.RelativeItem()
  81. .Background(Colors.Grey.Lighten2)
  82. .Padding(10)
  83. .Text("This column takes 1/3 of the available space (200pt)");
  84. row.RelativeItem(2)
  85. .Background(Colors.Grey.Lighten3)
  86. .Padding(10)
  87. .Text("This column takes 2/3 of the available space (400pt)");
  88. });
  89. });
  90. });
  91. }
  92. [Test]
  93. public void RowSpacing()
  94. {
  95. RenderingTest
  96. .Create()
  97. .PageSize(740, 200)
  98. .Render(container =>
  99. {
  100. container
  101. .Background("#FFF")
  102. .Padding(20)
  103. .Row(row =>
  104. {
  105. row.Spacing(20);
  106. row.RelativeItem(2).Border(1).Background(Colors.Grey.Lighten1);
  107. row.RelativeItem(3).Border(1).Background(Colors.Grey.Lighten2);
  108. row.RelativeItem(4).Border(1).Background(Colors.Grey.Lighten3);
  109. });
  110. });
  111. }
  112. [Test]
  113. public void column()
  114. {
  115. RenderingTest
  116. .Create()
  117. .PageSize(500, 360)
  118. .Render(container =>
  119. {
  120. container
  121. .Background("#FFF")
  122. .Padding(15)
  123. .Column(column =>
  124. {
  125. column.Spacing(15);
  126. column.Item().Background(Colors.Grey.Medium).Height(50);
  127. column.Item().Background(Colors.Grey.Lighten1).Height(100);
  128. column.Item().Background(Colors.Grey.Lighten2).Height(150);
  129. });
  130. });
  131. }
  132. [Test]
  133. public void Debug()
  134. {
  135. RenderingTest
  136. .Create()
  137. .PageSize(210, 210)
  138. .Render(container =>
  139. {
  140. container
  141. .Padding(25)
  142. .DebugArea("Grid example", Colors.Blue.Medium)
  143. .Grid(grid =>
  144. {
  145. grid.Columns(3);
  146. grid.Spacing(5);
  147. foreach (var _ in Enumerable.Range(0, 8))
  148. grid.Item().Height(50).Placeholder();
  149. });
  150. });
  151. }
  152. [Test]
  153. public void ElementEnd()
  154. {
  155. RenderingTest
  156. .Create()
  157. .PageSize(300, 200)
  158. .Render(container =>
  159. {
  160. var text = "";
  161. container
  162. .Padding(10)
  163. .Element(x =>
  164. {
  165. if (string.IsNullOrWhiteSpace(text))
  166. x.Height(10).Width(50).Background("#DDD");
  167. else
  168. x.Text(text);
  169. });
  170. });
  171. }
  172. [Test]
  173. public void GridExample()
  174. {
  175. RenderingTest
  176. .Create()
  177. .PageSize(400, 230)
  178. .Render(container =>
  179. {
  180. var textStyle = TextStyle.Default.Size(14);
  181. container
  182. .Padding(15)
  183. .AlignRight()
  184. .Grid(grid =>
  185. {
  186. grid.VerticalSpacing(10);
  187. grid.HorizontalSpacing(10);
  188. grid.AlignCenter();
  189. grid.Columns(10); // 12 by default
  190. grid.Item(6).Background(Colors.Blue.Lighten1).Height(50);
  191. grid.Item(4).Background(Colors.Blue.Lighten3).Height(50);
  192. grid.Item(2).Background(Colors.Teal.Lighten1).Height(70);
  193. grid.Item(3).Background(Colors.Teal.Lighten2).Height(70);
  194. grid.Item(5).Background(Colors.Teal.Lighten3).Height(70);
  195. grid.Item(2).Background(Colors.Green.Lighten1).Height(50);
  196. grid.Item(2).Background(Colors.Green.Lighten2).Height(50);
  197. grid.Item(2).Background(Colors.Green.Lighten3).Height(50);
  198. });
  199. });
  200. }
  201. [Test]
  202. public void Canvas()
  203. {
  204. RenderingTest
  205. .Create()
  206. .PageSize(300, 200)
  207. .Render(container =>
  208. {
  209. container
  210. .Background("#FFF")
  211. .Padding(25)
  212. .Canvas((canvas, size) =>
  213. {
  214. using var paint = new SKPaint
  215. {
  216. Color = SKColors.Red,
  217. StrokeWidth = 10,
  218. IsStroke = true
  219. };
  220. // move origin to the center of the available space
  221. canvas.Translate(size.Width / 2, size.Height / 2);
  222. // draw a circle
  223. canvas.DrawCircle(0, 0, 50, paint);
  224. });
  225. });
  226. }
  227. [Test]
  228. public void LayersExample()
  229. {
  230. RenderingTest
  231. .Create()
  232. .PageSize(400, 250)
  233. .Render(container =>
  234. {
  235. container
  236. .Padding(25)
  237. .Layers(layers =>
  238. {
  239. // layer below main content
  240. layers
  241. .Layer()
  242. .Height(100)
  243. .Width(100)
  244. .Background(Colors.Grey.Lighten3);
  245. layers
  246. .PrimaryLayer()
  247. .Padding(25)
  248. .Column(column =>
  249. {
  250. column.Spacing(5);
  251. foreach (var _ in Enumerable.Range(0, 7))
  252. column.Item().Text(Placeholders.Sentence());
  253. });
  254. // layer above the main content
  255. layers
  256. .Layer()
  257. .AlignCenter()
  258. .AlignMiddle()
  259. .Text("Watermark")
  260. .FontSize(48)
  261. .Bold()
  262. .FontColor(Colors.Green.Lighten3);
  263. layers
  264. .Layer()
  265. .AlignBottom()
  266. .Text(text => text.CurrentPageNumber().FontSize(16).FontColor(Colors.Green.Medium));
  267. });
  268. });
  269. }
  270. // [Test]
  271. // public void EnsureSpace()
  272. // {
  273. // RenderingTest
  274. // .Create()
  275. // .PageSize(300, 400)
  276. // .Render(container =>
  277. // {
  278. // container
  279. // .Padding(50)
  280. // .Page(page =>
  281. // {
  282. // page.Header().PageNumber("Page {pdf:currentPage}");
  283. //
  284. // page.Content().Height(300).column(content =>
  285. // {
  286. // content.Item().Height(200).Background(Colors.Grey.Lighten2);
  287. //
  288. // content.Item().EnsureSpace(100).column(column =>
  289. // {
  290. // column.Spacing(10);
  291. //
  292. // foreach (var _ in Enumerable.Range(0, 4))
  293. // column.Item().Height(50).Background(Colors.Green.Lighten1);
  294. // });
  295. // });
  296. // });
  297. // });
  298. // }
  299. [Test]
  300. public void RandomColorMatrix()
  301. {
  302. RenderingTest
  303. .Create()
  304. .PageSize(300, 300)
  305. .Render(container =>
  306. {
  307. container
  308. .Padding(25)
  309. .Grid(grid =>
  310. {
  311. grid.Columns(5);
  312. Enumerable
  313. .Range(0, 25)
  314. .Select(x => Placeholders.BackgroundColor())
  315. .ToList()
  316. .ForEach(x => grid.Item().Height(50).Background(x));
  317. });
  318. });
  319. }
  320. [Test]
  321. public void DefinedColors()
  322. {
  323. var colors = new[]
  324. {
  325. Colors.Green.Darken4,
  326. Colors.Green.Darken3,
  327. Colors.Green.Darken2,
  328. Colors.Green.Darken1,
  329. Colors.Green.Medium,
  330. Colors.Green.Lighten1,
  331. Colors.Green.Lighten2,
  332. Colors.Green.Lighten3,
  333. Colors.Green.Lighten4,
  334. Colors.Green.Lighten5,
  335. Colors.Green.Accent1,
  336. Colors.Green.Accent2,
  337. Colors.Green.Accent3,
  338. Colors.Green.Accent4,
  339. };
  340. RenderingTest
  341. .Create()
  342. .PageSize(450, 150)
  343. .Render(container =>
  344. {
  345. container
  346. .Padding(25)
  347. .Height(100)
  348. .Row(row =>
  349. {
  350. foreach (var color in colors)
  351. row.RelativeItem().Background(color);
  352. });
  353. });
  354. }
  355. [Test]
  356. public void DefinedFonts()
  357. {
  358. var fonts = new[]
  359. {
  360. Fonts.Calibri,
  361. Fonts.Candara,
  362. Fonts.Arial,
  363. Fonts.TimesNewRoman,
  364. Fonts.Consolas,
  365. Fonts.Tahoma,
  366. Fonts.Impact,
  367. Fonts.Trebuchet,
  368. Fonts.ComicSans
  369. };
  370. RenderingTest
  371. .Create()
  372. .PageSize(500, 175)
  373. .Render(container =>
  374. {
  375. container
  376. .Padding(25)
  377. .Grid(grid =>
  378. {
  379. grid.Columns(3);
  380. foreach (var font in fonts)
  381. {
  382. grid.Item()
  383. .Border(1)
  384. .BorderColor(Colors.Grey.Medium)
  385. .Padding(10)
  386. .Text(font)
  387. .FontFamily(font)
  388. .FontSize(16);
  389. }
  390. });
  391. });
  392. }
  393. [Test]
  394. public void Layers()
  395. {
  396. RenderingTest
  397. .Create()
  398. .PageSize(300, 300)
  399. .Render(container =>
  400. {
  401. container
  402. .Background("#FFF")
  403. .Padding(25)
  404. .Layers(layers =>
  405. {
  406. layers.Layer().Text("Something else");
  407. layers.PrimaryLayer().Column(column =>
  408. {
  409. column.Item().PaddingTop(20).Text("Text 1");
  410. column.Item().PaddingTop(40).Text("Text 2");
  411. });
  412. layers.Layer().Canvas((canvas, size) =>
  413. {
  414. using var paint = new SKPaint
  415. {
  416. Color = SKColors.Red,
  417. StrokeWidth = 5
  418. };
  419. canvas.Translate(size.Width / 2, size.Height / 2);
  420. canvas.DrawCircle(0, 0, 50, paint);
  421. });
  422. layers.Layer().Background("#8F00").Extend();
  423. layers.Layer().PaddingTop(40).Text("It works!").FontSize(24);
  424. });
  425. });
  426. }
  427. [Test]
  428. public void Box()
  429. {
  430. RenderingTest
  431. .Create()
  432. .PageSize(500, 200)
  433. .ProduceImages()
  434. .ShowResults()
  435. .Render(container =>
  436. {
  437. container.Padding(20).Row(row =>
  438. {
  439. row.RelativeItem().ContentFromLeftToRight().Element(Content);
  440. row.RelativeItem().ContentFromRightToLeft().Element(Content);
  441. });
  442. void Content(IContainer container)
  443. {
  444. container
  445. .ExtendVertical()
  446. .Padding(15)
  447. .Border(4)
  448. .BorderColor(Colors.Blue.Medium)
  449. .MinimalBox()
  450. .Background(Colors.Grey.Lighten2)
  451. .Padding(15)
  452. .Text("Test of the \nbox element").FontSize(20);
  453. }
  454. });
  455. }
  456. [Test]
  457. public void Scale()
  458. {
  459. RenderingTest
  460. .Create()
  461. .PageSize(300, 175)
  462. .Render(container =>
  463. {
  464. container
  465. .Background(Colors.White)
  466. .Padding(10)
  467. .Decoration(decoration =>
  468. {
  469. var headerFontStyle = TextStyle
  470. .Default
  471. .Size(20)
  472. .Color(Colors.Blue.Darken2)
  473. .SemiBold();
  474. decoration
  475. .Before()
  476. .PaddingBottom(10)
  477. .Text("Example: scale component")
  478. .Style(headerFontStyle);
  479. decoration
  480. .Content()
  481. .Column(column =>
  482. {
  483. var scales = new[] { 0.8f, 0.9f, 1.1f, 1.2f };
  484. foreach (var scale in scales)
  485. {
  486. var fontColor = scale <= 1f
  487. ? Colors.Red.Lighten4
  488. : Colors.Green.Lighten4;
  489. var fontStyle = TextStyle.Default.Size(16);
  490. column
  491. .Item()
  492. .Border(1)
  493. .Background(fontColor)
  494. .Scale(scale)
  495. .Padding(5)
  496. .Text($"Content with {scale} scale.")
  497. .Style(fontStyle);
  498. }
  499. });
  500. });
  501. });
  502. }
  503. [Test]
  504. public void Translate()
  505. {
  506. RenderingTest
  507. .Create()
  508. .PageSize(300, 200)
  509. .Render(container =>
  510. {
  511. container
  512. .Background("#FFF")
  513. .MinimalBox()
  514. .Padding(25)
  515. .Background(Colors.Green.Lighten3)
  516. .TranslateX(15)
  517. .TranslateY(15)
  518. .Border(2)
  519. .BorderColor(Colors.Green.Darken1)
  520. .Padding(50)
  521. .Text("Moved text")
  522. .FontSize(25);
  523. });
  524. }
  525. [Test]
  526. public void ConstrainedRotate()
  527. {
  528. RenderingTest
  529. .Create()
  530. .PageSize(650, 450)
  531. .Render(container =>
  532. {
  533. container
  534. .Padding(20)
  535. .Grid(grid =>
  536. {
  537. grid.Columns(2);
  538. grid.Spacing(10);
  539. foreach (var turns in Enumerable.Range(0, 4))
  540. {
  541. grid.Item()
  542. .Width(300)
  543. .Height(200)
  544. .Background(Colors.Grey.Lighten2)
  545. .Padding(10)
  546. .Element(element =>
  547. {
  548. foreach (var x in Enumerable.Range(0, turns))
  549. element = element.RotateRight();
  550. return element;
  551. })
  552. .MinimalBox()
  553. .Background(Colors.White)
  554. .Padding(10)
  555. .Text($"Rotated {turns * 90}°")
  556. .FontSize(16);
  557. }
  558. });
  559. });
  560. }
  561. [Test]
  562. public void FreeRotate()
  563. {
  564. RenderingTest
  565. .Create()
  566. .PageSize(300, 300)
  567. .Render(container =>
  568. {
  569. container
  570. .Padding(25)
  571. .Background(Colors.Grey.Lighten2)
  572. .AlignCenter()
  573. .AlignMiddle()
  574. .Background(Colors.White)
  575. .Rotate(30)
  576. .Width(100)
  577. .Height(100)
  578. .Background(Colors.Blue.Medium);
  579. });
  580. }
  581. [Test]
  582. public void FreeRotateCenter()
  583. {
  584. RenderingTest
  585. .Create()
  586. .PageSize(300, 300)
  587. .Render(container =>
  588. {
  589. container
  590. .Padding(25)
  591. .Background(Colors.Grey.Lighten2)
  592. .AlignCenter()
  593. .AlignMiddle()
  594. .Background(Colors.White)
  595. .TranslateX(50)
  596. .TranslateY(50)
  597. .Rotate(30)
  598. .TranslateX(-50)
  599. .TranslateY(-50)
  600. .Width(100)
  601. .Height(100)
  602. .Background(Colors.Blue.Medium);
  603. });
  604. }
  605. [Test]
  606. public void Flip()
  607. {
  608. RenderingTest
  609. .Create()
  610. .PageSize(350, 350)
  611. .Render(container =>
  612. {
  613. container
  614. .Padding(20)
  615. .Grid(grid =>
  616. {
  617. grid.Columns(2);
  618. grid.Spacing(10);
  619. foreach (var turns in Enumerable.Range(0, 4))
  620. {
  621. grid.Item()
  622. .Width(150)
  623. .Height(150)
  624. .Background(Colors.Grey.Lighten3)
  625. .Padding(10)
  626. .Element(element =>
  627. {
  628. if (turns == 1 || turns == 2)
  629. element = element.FlipHorizontal();
  630. if (turns == 2 || turns == 3)
  631. element = element.FlipVertical();
  632. return element;
  633. })
  634. .MinimalBox()
  635. .Background(Colors.White)
  636. .Padding(10)
  637. .Text($"Flipped {turns}")
  638. .FontSize(16);
  639. }
  640. });
  641. });
  642. }
  643. [Test]
  644. public void RotateInTable()
  645. {
  646. RenderingTest
  647. .Create()
  648. .PageSize(200, 200)
  649. .Render(container =>
  650. {
  651. container
  652. .Padding(10)
  653. .Border(2)
  654. .Row(row =>
  655. {
  656. row.ConstantItem(25)
  657. .Border(1)
  658. .RotateLeft()
  659. .AlignCenter()
  660. .AlignMiddle()
  661. .Text("Sample text");
  662. row.RelativeItem().Border(1).Padding(5).Text(Placeholders.Paragraph());
  663. });
  664. });
  665. }
  666. [Test]
  667. public void Unconstrained()
  668. {
  669. RenderingTest
  670. .Create()
  671. .PageSize(400, 350)
  672. .Render(container =>
  673. {
  674. container
  675. .Padding(25)
  676. .PaddingLeft(50)
  677. .Column(column =>
  678. {
  679. column.Item().Width(300).Height(150).Background(Colors.Blue.Lighten4);
  680. column
  681. .Item()
  682. // creates an infinite space for its child
  683. .Unconstrained()
  684. // moves the child up and left
  685. .TranslateX(-50)
  686. .TranslateY(-50)
  687. // limits the space for the child
  688. .Width(100)
  689. .Height(100)
  690. .Background(Colors.Blue.Darken1);
  691. column.Item().Width(300).Height(150).Background(Colors.Blue.Lighten3);
  692. });
  693. });
  694. }
  695. [Test]
  696. public void ComplexLayout()
  697. {
  698. RenderingTest
  699. .Create()
  700. .PageSize(500, 225)
  701. .Render(container =>
  702. {
  703. container
  704. .Padding(25)
  705. .Column(column =>
  706. {
  707. column.Item().Row(row =>
  708. {
  709. row.RelativeItem().LabelCell("Label 1");
  710. row.RelativeItem(3).Grid(grid =>
  711. {
  712. grid.Columns(3);
  713. grid.Item(2).LabelCell("Label 2");
  714. grid.Item().LabelCell("Label 3");
  715. grid.Item(2).ValueCell().Text("Value 2");
  716. grid.Item().ValueCell().Text("Value 3");
  717. });
  718. });
  719. column.Item().Row(row =>
  720. {
  721. row.RelativeItem().ValueCell().Text("Value 1");
  722. row.RelativeItem(3).Grid(grid =>
  723. {
  724. grid.Columns(3);
  725. grid.Item().LabelCell("Label 4");
  726. grid.Item(2).LabelCell("Label 5");
  727. grid.Item().ValueCell().Text("Value 4");
  728. grid.Item(2).ValueCell().Text("Value 5");
  729. });
  730. });
  731. column.Item().Row(row =>
  732. {
  733. row.RelativeItem().LabelCell("Label 6");
  734. row.RelativeItem().ValueCell().Text("Value 6");
  735. });
  736. });
  737. });
  738. }
  739. [Test]
  740. public void DomainSpecificLanguage()
  741. {
  742. RenderingTest
  743. .Create()
  744. .PageSize(600, 310)
  745. .Render(container =>
  746. {
  747. container
  748. .Padding(25)
  749. .Grid(grid =>
  750. {
  751. grid.Columns(10);
  752. for(var i=1; i<=4; i++)
  753. {
  754. grid.Item(2).LabelCell(Placeholders.Label());
  755. grid.Item(3).ValueCell().Image(Placeholders.Image(200, 150));
  756. }
  757. });
  758. });
  759. }
  760. }
  761. }