ElementExamples.cs 29 KB

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