ElementExamples.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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(300, 175)
  461. .FileName()
  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. .Header()
  476. .PaddingBottom(10)
  477. .Text("Example: scale component", headerFontStyle);
  478. decoration
  479. .Content()
  480. .Stack(stack =>
  481. {
  482. var scales = new[] { 0.8f, 0.9f, 1.1f, 1.2f };
  483. foreach (var scale in scales)
  484. {
  485. var fontColor = scale <= 1f
  486. ? Colors.Red.Lighten4
  487. : Colors.Green.Lighten4;
  488. var fontStyle = TextStyle.Default.Size(16);
  489. stack
  490. .Item()
  491. .Border(1)
  492. .Background(fontColor)
  493. .Scale(scale)
  494. .Padding(5)
  495. .Text($"Content with {scale} scale.", fontStyle);
  496. }
  497. });
  498. });
  499. });
  500. }
  501. [Test]
  502. public void Translate()
  503. {
  504. RenderingTest
  505. .Create()
  506. .PageSize(300, 200)
  507. .FileName()
  508. .Render(container =>
  509. {
  510. container
  511. .Background("#FFF")
  512. .Box()
  513. .Padding(25)
  514. .Background(Colors.Green.Lighten3)
  515. .TranslateX(15)
  516. .TranslateY(15)
  517. .Border(2)
  518. .BorderColor(Colors.Green.Darken1)
  519. .Padding(50)
  520. .Text("Moved text", TextStyle.Default.Size(25));
  521. });
  522. }
  523. [Test]
  524. public void ConstrainedRotate()
  525. {
  526. RenderingTest
  527. .Create()
  528. .PageSize(350, 350)
  529. .FileName()
  530. .Render(container =>
  531. {
  532. container
  533. .Padding(20)
  534. .Grid(grid =>
  535. {
  536. grid.Columns(2);
  537. grid.Spacing(10);
  538. foreach (var turns in Enumerable.Range(0, 4))
  539. {
  540. grid.Item()
  541. .Width(150)
  542. .Height(150)
  543. .Background(Colors.Grey.Lighten2)
  544. .Padding(10)
  545. .Element(element =>
  546. {
  547. foreach (var x in Enumerable.Range(0, turns))
  548. element = element.RotateRight();
  549. return element;
  550. })
  551. .Box()
  552. .Background(Colors.White)
  553. .Padding(10)
  554. .Text($"Rotated {turns * 90}°", TextStyle.Default.Size(16));
  555. }
  556. });
  557. });
  558. }
  559. [Test]
  560. public void FreeRotate()
  561. {
  562. RenderingTest
  563. .Create()
  564. .PageSize(300, 300)
  565. .FileName()
  566. .Render(container =>
  567. {
  568. container
  569. .Padding(25)
  570. .Background(Colors.Grey.Lighten2)
  571. .AlignCenter()
  572. .AlignMiddle()
  573. .Background(Colors.White)
  574. .Rotate(30)
  575. .Width(100)
  576. .Height(100)
  577. .Background(Colors.Blue.Medium);
  578. });
  579. }
  580. [Test]
  581. public void FreeRotateCenter()
  582. {
  583. RenderingTest
  584. .Create()
  585. .PageSize(300, 300)
  586. .FileName()
  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. .FileName()
  612. .Render(container =>
  613. {
  614. container
  615. .Padding(20)
  616. .Grid(grid =>
  617. {
  618. grid.Columns(2);
  619. grid.Spacing(10);
  620. foreach (var turns in Enumerable.Range(0, 4))
  621. {
  622. grid.Item()
  623. .Width(150)
  624. .Height(150)
  625. .Background(Colors.Grey.Lighten3)
  626. .Padding(10)
  627. .Element(element =>
  628. {
  629. if (turns == 1 || turns == 2)
  630. element = element.FlipHorizontal();
  631. if (turns == 2 || turns == 3)
  632. element = element.FlipVertical();
  633. return element;
  634. })
  635. .Box()
  636. .Background(Colors.White)
  637. .Padding(10)
  638. .Text($"Flipped {turns}", TextStyle.Default.Size(16));
  639. }
  640. });
  641. });
  642. }
  643. [Test]
  644. public void RotateInTable()
  645. {
  646. RenderingTest
  647. .Create()
  648. .PageSize(200, 200)
  649. .FileName()
  650. .Render(container =>
  651. {
  652. container
  653. .Padding(10)
  654. .Border(2)
  655. .Row(row =>
  656. {
  657. row.ConstantColumn(25)
  658. .Border(1)
  659. .RotateLeft()
  660. .AlignCenter()
  661. .AlignMiddle()
  662. .Text("Sample text");
  663. row.RelativeColumn().Border(1).Padding(5).Text(Placeholders.Paragraph());
  664. });
  665. });
  666. }
  667. [Test]
  668. public void Unconstrained()
  669. {
  670. RenderingTest
  671. .Create()
  672. .PageSize(400, 350)
  673. .FileName()
  674. .Render(container =>
  675. {
  676. container
  677. .Padding(25)
  678. .PaddingLeft(75)
  679. .Stack(stack =>
  680. {
  681. stack.Item().Width(300).Height(150).Background(Colors.Blue.Lighten4);
  682. stack
  683. .Item()
  684. // creates an infinite space for its child
  685. .Unconstrained()
  686. // moves the child up and left
  687. .TranslateX(-50)
  688. .TranslateY(-50)
  689. // limits the space for the child
  690. .Width(100)
  691. .Height(100)
  692. .Background(Colors.Blue.Darken1);
  693. stack.Item().Width(300).Height(150).Background(Colors.Blue.Lighten3);
  694. });
  695. });
  696. }
  697. }
  698. }