ContentDirectionExamples.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using QuestPDF.Drawing.Exceptions;
  5. using QuestPDF.Elements;
  6. using QuestPDF.Examples.Engine;
  7. using QuestPDF.Fluent;
  8. using QuestPDF.Helpers;
  9. using QuestPDF.Infrastructure;
  10. namespace QuestPDF.Examples
  11. {
  12. public class ContentDirectionExamples
  13. {
  14. private Action<IContainer> ContentDirectionTemplate(Action<IContainer> content)
  15. {
  16. return container =>
  17. {
  18. container
  19. .Padding(20)
  20. .MinimalBox()
  21. .Border(1)
  22. .ExtendHorizontal()
  23. .Table(table =>
  24. {
  25. table.ColumnsDefinition(columns =>
  26. {
  27. columns.RelativeColumn();
  28. columns.RelativeColumn();
  29. });
  30. table.Header(header =>
  31. {
  32. header.Cell().ContentFromLeftToRight().Element(HeaderCell("Left-to-right"));
  33. header.Cell().ContentFromRightToLeft().Element(HeaderCell("Right-to-left"));
  34. static Action<IContainer> HeaderCell(string label)
  35. {
  36. return container => container
  37. .Border(1)
  38. .BorderColor(Colors.Grey.Medium)
  39. .Background(Colors.Grey.Lighten3)
  40. .PaddingHorizontal(10)
  41. .PaddingVertical(5)
  42. .Text(label)
  43. .FontSize(18)
  44. .SemiBold();
  45. }
  46. });
  47. table.Cell().Element(TestCell).ContentFromLeftToRight().Element(content);
  48. table.Cell()
  49. .Element(TestCell)
  50. .ContentFromRightToLeft()
  51. .Element(content);
  52. static IContainer TestCell(IContainer container)
  53. {
  54. return container.Border(1).BorderColor(Colors.Grey.Medium).Padding(10);
  55. }
  56. });
  57. };
  58. }
  59. [Test]
  60. public void Page()
  61. {
  62. RenderingTest
  63. .Create()
  64. .ProduceImages()
  65. .ShowResults()
  66. .EnableDebugging()
  67. .RenderDocument(document =>
  68. {
  69. document.Page(page =>
  70. {
  71. page.Size(PageSizes.A5);
  72. page.Margin(20);
  73. page.PageColor(Colors.White);
  74. //page.ContentFromRightToLeft();
  75. page.Content().Column(column =>
  76. {
  77. column.Spacing(20);
  78. column.Item().Row(row =>
  79. {
  80. row.Spacing(10);
  81. row.AutoItem().AlignMiddle().Width(20).Height(20).Image(Placeholders.Image);
  82. row.RelativeItem()
  83. .Text("Document title")
  84. .FontSize(24).FontColor(Colors.Blue.Accent1).SemiBold();
  85. });
  86. column.Item().Table(table =>
  87. {
  88. table.ColumnsDefinition(columns =>
  89. {
  90. columns.RelativeColumn();
  91. columns.RelativeColumn();
  92. columns.RelativeColumn();
  93. columns.RelativeColumn();
  94. });
  95. foreach (var i in Enumerable.Range(0, 9))
  96. {
  97. var width = (i % 4 == 0) ? 2 : 1;
  98. table
  99. .Cell()
  100. .ColumnSpan((uint)width)
  101. .Background(i % 4 == 0 ? Colors.Grey.Lighten1 : Colors.Grey.Lighten2)
  102. .Padding(5)
  103. .AlignCenter()
  104. .Text(i)
  105. .FontSize(20);
  106. }
  107. });
  108. });
  109. });
  110. });
  111. }
  112. [Test]
  113. public void Column()
  114. {
  115. RenderingTest
  116. .Create()
  117. .PageSize(PageSizes.A4)
  118. .ProduceImages()
  119. .ShowResults()
  120. .EnableDebugging()
  121. .Render(ContentDirectionTemplate(Content));
  122. void Content(IContainer container)
  123. {
  124. container.Column(column =>
  125. {
  126. column.Spacing(5);
  127. column.Item().Height(50).Width(50).Background(Colors.Red.Lighten1);
  128. column.Item().Height(50).Width(100).Background(Colors.Green.Lighten1);
  129. column.Item().Height(50).Width(150).Background(Colors.Blue.Lighten1);
  130. });
  131. }
  132. }
  133. [Test]
  134. public void Row()
  135. {
  136. RenderingTest
  137. .Create()
  138. .PageSize(PageSizes.A4)
  139. .ProduceImages()
  140. .ShowResults()
  141. .EnableDebugging()
  142. .Render(ContentDirectionTemplate(Content));
  143. void Content(IContainer container)
  144. {
  145. container.Row(row =>
  146. {
  147. row.Spacing(5);
  148. row.AutoItem().Height(50).Width(50).Background(Colors.Red.Lighten1);
  149. row.AutoItem().Height(50).Width(50).Background(Colors.Green.Lighten1);
  150. row.AutoItem().Height(50).Width(75).Background(Colors.Blue.Lighten1);
  151. });
  152. }
  153. }
  154. [Test]
  155. public void Table()
  156. {
  157. RenderingTest
  158. .Create()
  159. .PageSize(PageSizes.A4)
  160. .ProduceImages()
  161. .ShowResults()
  162. .EnableDebugging()
  163. .Render(ContentDirectionTemplate(Content));
  164. void Content(IContainer container)
  165. {
  166. container.Table(table =>
  167. {
  168. table.ColumnsDefinition(columns =>
  169. {
  170. columns.RelativeColumn();
  171. columns.RelativeColumn();
  172. columns.RelativeColumn();
  173. });
  174. table.Cell().Height(50).Background(Colors.Red.Lighten1);
  175. table.Cell().Height(50).Background(Colors.Green.Lighten1);
  176. table.Cell().Height(50).Background(Colors.Blue.Lighten1);
  177. table.Cell().ColumnSpan(2).Height(50).Background(Colors.Orange.Lighten1);
  178. });
  179. }
  180. }
  181. [Test]
  182. public void Constrained()
  183. {
  184. RenderingTest
  185. .Create()
  186. .PageSize(PageSizes.A4)
  187. .ProduceImages()
  188. .ShowResults()
  189. .EnableDebugging()
  190. .Render(ContentDirectionTemplate(Content));
  191. void Content(IContainer container)
  192. {
  193. container.Width(50).Height(50).Background(Colors.Red.Lighten1);
  194. }
  195. }
  196. [Test]
  197. public void Unconstrained()
  198. {
  199. RenderingTest
  200. .Create()
  201. .PageSize(PageSizes.A4)
  202. .ProduceImages()
  203. .ShowResults()
  204. .EnableDebugging()
  205. .Render(ContentDirectionTemplate(Content));
  206. void Content(IContainer container)
  207. {
  208. container
  209. .Width(100)
  210. .Height(100)
  211. .Background(Colors.Grey.Lighten3)
  212. .AlignCenter()
  213. .AlignMiddle()
  214. .Unconstrained()
  215. .Width(50)
  216. .Height(50)
  217. .Background(Colors.Red.Lighten1);
  218. }
  219. }
  220. [Test]
  221. public void Inlined()
  222. {
  223. RenderingTest
  224. .Create()
  225. .PageSize(PageSizes.A4)
  226. .ProduceImages()
  227. .ShowResults()
  228. .EnableDebugging()
  229. .Render(ContentDirectionTemplate(Content));
  230. void Content(IContainer container)
  231. {
  232. container.Column(column =>
  233. {
  234. column.Spacing(10);
  235. column.Item().Background(Colors.Grey.Lighten3).Text("Default alignment").FontSize(13);
  236. column.Item().Element(ContentWithAlignment(null));
  237. column.Item().Background(Colors.Grey.Lighten3).Text("Left alignment").FontSize(14);
  238. column.Item().Element(ContentWithAlignment(InlinedAlignment.Left));
  239. column.Item().Background(Colors.Grey.Lighten3).Text("Center alignment").FontSize(14);
  240. column.Item().Element(ContentWithAlignment(InlinedAlignment.Center));
  241. column.Item().Background(Colors.Grey.Lighten3).Text("Right alignment").FontSize(14);
  242. column.Item().Element(ContentWithAlignment(InlinedAlignment.Right));
  243. });
  244. static Action<IContainer> ContentWithAlignment(InlinedAlignment? alignment)
  245. {
  246. return container =>
  247. {
  248. container.Inlined(inlined =>
  249. {
  250. inlined.Spacing(5);
  251. inlined.Alignment(alignment);
  252. inlined.Item().Height(50).Width(50).Background(Colors.Red.Lighten1);
  253. inlined.Item().Height(50).Width(75).Background(Colors.Green.Lighten1);
  254. inlined.Item().Height(50).Width(100).Background(Colors.Blue.Lighten1);
  255. inlined.Item().Height(50).Width(125).Background(Colors.Orange.Lighten1);
  256. });
  257. };
  258. }
  259. }
  260. }
  261. [Test]
  262. public void Text()
  263. {
  264. RenderingTest
  265. .Create()
  266. .PageSize(PageSizes.A4)
  267. .ProduceImages()
  268. .ShowResults()
  269. .EnableDebugging()
  270. .Render(ContentDirectionTemplate(Content));
  271. void Content(IContainer container)
  272. {
  273. container.Column(column =>
  274. {
  275. column.Spacing(10);
  276. column.Item().Background(Colors.Grey.Lighten3).Text("Default alignment").FontSize(13);
  277. column.Item().Element(ContentWithAlignment(null));
  278. column.Item().Background(Colors.Grey.Lighten3).Text("Left alignment").FontSize(14);
  279. column.Item().Element(ContentWithAlignment(HorizontalAlignment.Left));
  280. column.Item().Background(Colors.Grey.Lighten3).Text("Center alignment").FontSize(14);
  281. column.Item().Element(ContentWithAlignment(HorizontalAlignment.Center));
  282. column.Item().Background(Colors.Grey.Lighten3).Text("Right alignment").FontSize(14);
  283. column.Item().Element(ContentWithAlignment(HorizontalAlignment.Right));
  284. });
  285. static Action<IContainer> ContentWithAlignment(HorizontalAlignment? alignment)
  286. {
  287. return container =>
  288. {
  289. container.Text(text =>
  290. {
  291. text.Alignment = alignment; // internal API
  292. text.Span("Lorem ipsum").Bold().FontColor(Colors.Red.Medium);
  293. text.Element().Width(5);
  294. text.Span(Placeholders.LoremIpsum());
  295. });
  296. };
  297. }
  298. }
  299. }
  300. [Test]
  301. public void Decoration()
  302. {
  303. RenderingTest
  304. .Create()
  305. .PageSize(PageSizes.A4)
  306. .ProduceImages()
  307. .ShowResults()
  308. .EnableDebugging()
  309. .Render(ContentDirectionTemplate(Content));
  310. void Content(IContainer container)
  311. {
  312. container.Decoration(decoration =>
  313. {
  314. decoration.Before().Background(Colors.Green.Lighten1).Padding(5).Text("Before").FontSize(16);
  315. decoration.Content().Background(Colors.Green.Lighten2).Padding(5).Text("Content").FontSize(16);
  316. decoration.After().Background(Colors.Green.Lighten3).Padding(5).Text("After").FontSize(16);
  317. });
  318. }
  319. }
  320. [Test]
  321. public void Dynamic()
  322. {
  323. RenderingTest
  324. .Create()
  325. .PageSize(PageSizes.A4)
  326. .ProduceImages()
  327. .ShowResults()
  328. .EnableDebugging()
  329. .Render(ContentDirectionTemplate(Content));
  330. void Content(IContainer container)
  331. {
  332. container.Dynamic(new SimpleDynamic());
  333. }
  334. }
  335. class SimpleDynamic : IDynamicComponent<int>
  336. {
  337. public int State { get; set; }
  338. public DynamicComponentComposeResult Compose(DynamicContext context)
  339. {
  340. var content = context.CreateElement(container =>
  341. {
  342. container.Row(row =>
  343. {
  344. row.ConstantItem(50).Background(Colors.Red.Lighten2).Height(50);
  345. row.ConstantItem(75).Background(Colors.Green.Lighten2).Height(50);
  346. row.ConstantItem(100).Background(Colors.Blue.Lighten2).Height(50);
  347. });
  348. });
  349. return new DynamicComponentComposeResult
  350. {
  351. Content = content,
  352. HasMoreContent = false
  353. };
  354. }
  355. }
  356. }
  357. }