TextExamples.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using NUnit.Framework;
  5. using QuestPDF.Examples.Engine;
  6. using QuestPDF.Fluent;
  7. using QuestPDF.Helpers;
  8. using QuestPDF.Infrastructure;
  9. namespace QuestPDF.Examples
  10. {
  11. public class TextExamples
  12. {
  13. [Test]
  14. public void SimpleTextBlock()
  15. {
  16. RenderingTest
  17. .Create()
  18. .PageSize(500, 300)
  19. .FileName()
  20. .ProduceImages()
  21. .ShowResults()
  22. .Render(container =>
  23. {
  24. container
  25. .Padding(5)
  26. .Box()
  27. .Border(1)
  28. .Padding(10)
  29. .Text(text =>
  30. {
  31. text.DefaultTextStyle(TextStyle.Default.Size(20));
  32. text.Span("This is a normal text, followed by an ");
  33. text.Span("underlined red text.", TextStyle.Default.Size(20).Color(Colors.Red.Medium).Underline());
  34. });
  35. });
  36. }
  37. [Test]
  38. public void ParagraphSpacing()
  39. {
  40. RenderingTest
  41. .Create()
  42. .PageSize(500, 300)
  43. .FileName()
  44. .ProduceImages()
  45. .ShowResults()
  46. .Render(container =>
  47. {
  48. container
  49. .Padding(5)
  50. .Box()
  51. .Border(1)
  52. .Padding(10)
  53. .Text(text =>
  54. {
  55. text.ParagraphSpacing(10);
  56. foreach (var i in Enumerable.Range(1, 3))
  57. {
  58. text.Span($"Paragraph {i}: ", TextStyle.Default.SemiBold());
  59. text.Line(Placeholders.Paragraph());
  60. }
  61. });
  62. });
  63. }
  64. [Test]
  65. public void CustomElement()
  66. {
  67. RenderingTest
  68. .Create()
  69. .PageSize(500, 200)
  70. .FileName()
  71. .ProduceImages()
  72. .ShowResults()
  73. .Render(container =>
  74. {
  75. container
  76. .Padding(5)
  77. .Box()
  78. .Border(1)
  79. .Padding(10)
  80. .Text(text =>
  81. {
  82. text.DefaultTextStyle(TextStyle.Default.Size(20));
  83. text.Span("This is a random image aligned to the baseline: ");
  84. text.Element()
  85. .PaddingBottom(-6)
  86. .Height(24)
  87. .Width(48)
  88. .Image(Placeholders.Image);
  89. text.Span(".");
  90. });
  91. });
  92. }
  93. [Test]
  94. public void TextElements()
  95. {
  96. RenderingTest
  97. .Create()
  98. .PageSize(PageSizes.A4)
  99. .FileName()
  100. .ProducePdf()
  101. .ShowResults()
  102. .Render(container =>
  103. {
  104. container
  105. .Padding(20)
  106. .Padding(10)
  107. .Box()
  108. .Border(1)
  109. .Padding(5)
  110. .Padding(10)
  111. .Text(text =>
  112. {
  113. text.DefaultTextStyle(TextStyle.Default);
  114. text.AlignLeft();
  115. text.ParagraphSpacing(10);
  116. text.Line(Placeholders.LoremIpsum());
  117. text.Span($"This is target text that should show up. {DateTime.UtcNow:T} > This is a short sentence that will be wrapped into second line hopefully, right? <", TextStyle.Default.Underline());
  118. });
  119. });
  120. }
  121. [Test]
  122. public void TextStack()
  123. {
  124. RenderingTest
  125. .Create()
  126. .PageSize(PageSizes.A4)
  127. .FileName()
  128. .ProducePdf()
  129. .ShowResults()
  130. .Render(container =>
  131. {
  132. container
  133. .Padding(20)
  134. .Padding(10)
  135. .Box()
  136. .Border(1)
  137. .Padding(5)
  138. .Padding(10)
  139. .Text(text =>
  140. {
  141. text.DefaultTextStyle(TextStyle.Default);
  142. text.AlignLeft();
  143. text.ParagraphSpacing(10);
  144. foreach (var i in Enumerable.Range(1, 100))
  145. text.Line($"{i}: {Placeholders.Paragraph()}");
  146. });
  147. });
  148. }
  149. [Test]
  150. public void SpaceIssue()
  151. {
  152. RenderingTest
  153. .Create()
  154. .PageSize(PageSizes.A4)
  155. .FileName()
  156. .ProducePdf()
  157. .ShowResults()
  158. .Render(container =>
  159. {
  160. container
  161. .Padding(20)
  162. .Padding(10)
  163. .Box()
  164. .Border(1)
  165. .Padding(5)
  166. .Padding(10)
  167. .Text(text =>
  168. {
  169. text.DefaultTextStyle(TextStyle.Default);
  170. text.AlignLeft();
  171. text.ParagraphSpacing(10);
  172. text.Span(Placeholders.LoremIpsum());
  173. text.EmptyLine();
  174. text.Span("This text is a normal text, ");
  175. text.Span("this is a bold text, ", TextStyle.Default.Bold());
  176. text.Span("this is a red and underlined text, ", TextStyle.Default.Color(Colors.Red.Medium).Underline());
  177. text.Span("and this is slightly bigger text.", TextStyle.Default.Size(16));
  178. text.EmptyLine();
  179. text.Span("The new text element also supports injecting custom content between words: ");
  180. text.Element().PaddingBottom(-10).Height(16).Width(32).Image(Placeholders.Image);
  181. text.Span(".");
  182. text.EmptyLine();
  183. text.Span("This is page number ");
  184. text.CurrentPageNumber();
  185. text.Span(" out of ");
  186. text.TotalPages();
  187. text.EmptyLine();
  188. text.ExternalLocation("Please visit QuestPDF website", "https://www.questpdf.com");
  189. text.EmptyLine();
  190. text.Span(Placeholders.Paragraphs());
  191. text.EmptyLine();
  192. text.Span(Placeholders.Paragraphs(), TextStyle.Default.Italic());
  193. text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
  194. });
  195. });
  196. }
  197. [Test]
  198. public void HugeList()
  199. {
  200. RenderingTest
  201. .Create()
  202. .PageSize(PageSizes.A4)
  203. .FileName()
  204. .ProducePdf()
  205. .ShowResults()
  206. .Render(container =>
  207. {
  208. container
  209. .Padding(20)
  210. .Padding(10)
  211. .Box()
  212. .Border(1)
  213. .Padding(5)
  214. .Padding(10)
  215. .Text(text =>
  216. {
  217. text.DefaultTextStyle(TextStyle.Default);
  218. text.AlignLeft();
  219. text.ParagraphSpacing(10);
  220. text.Span("This text is a normal text, ");
  221. text.Span("this is a bold text, ", TextStyle.Default.Bold());
  222. text.Span("this is a red and underlined text, ", TextStyle.Default.Color(Colors.Red.Medium).Underline());
  223. text.Span("and this is slightly bigger text.", TextStyle.Default.Size(16));
  224. text.Span("The new text element also supports injecting custom content between words: ");
  225. text.Element().PaddingBottom(-10).Height(16).Width(32).Image(Placeholders.Image);
  226. text.Span(".");
  227. text.EmptyLine();
  228. foreach (var i in Enumerable.Range(1, 100))
  229. {
  230. text.Line($"{i}: {Placeholders.Paragraph()}");
  231. text.ExternalLocation("Please visit QuestPDF website", "https://www.questpdf.com");
  232. text.Span("This is page number ");
  233. text.CurrentPageNumber();
  234. text.Span(" out of ");
  235. text.TotalPages();
  236. text.EmptyLine();
  237. }
  238. });
  239. });
  240. }
  241. }
  242. }