ElementExamples.cs 30 KB

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