ContentDirectionExamples.cs 15 KB

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