ContentDirectionExamples.cs 15 KB

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