ElementExamples.cs 30 KB

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