TextExamples.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. .ProduceImages()
  20. .ShowResults()
  21. .Render(container =>
  22. {
  23. container
  24. .Padding(5)
  25. .MinimalBox()
  26. .Border(1)
  27. .Padding(10)
  28. .Text(text =>
  29. {
  30. text.DefaultTextStyle(TextStyle.Default.Size(20));
  31. text.Span("This is a normal text, followed by an ");
  32. text.Span("underlined red text", TextStyle.Default.Color(Colors.Red.Medium).Underline());
  33. text.Span(".");
  34. });
  35. });
  36. }
  37. [Test]
  38. public void ParagraphSpacing()
  39. {
  40. RenderingTest
  41. .Create()
  42. .PageSize(500, 300)
  43. .ProduceImages()
  44. .ShowResults()
  45. .Render(container =>
  46. {
  47. container
  48. .Padding(5)
  49. .MinimalBox()
  50. .Border(1)
  51. .Padding(10)
  52. .Text(text =>
  53. {
  54. text.ParagraphSpacing(10);
  55. foreach (var i in Enumerable.Range(1, 3))
  56. {
  57. text.Span($"Paragraph {i}: ", TextStyle.Default.SemiBold());
  58. text.Line(Placeholders.Paragraph());
  59. }
  60. });
  61. });
  62. }
  63. [Test]
  64. public void CustomElement()
  65. {
  66. RenderingTest
  67. .Create()
  68. .PageSize(500, 200)
  69. .ProduceImages()
  70. .ShowResults()
  71. .Render(container =>
  72. {
  73. container
  74. .Padding(5)
  75. .MinimalBox()
  76. .Border(1)
  77. .Padding(10)
  78. .Text(text =>
  79. {
  80. text.DefaultTextStyle(TextStyle.Default.Size(20));
  81. text.Span("This is a random image aligned to the baseline: ");
  82. text.Element()
  83. .PaddingBottom(-6)
  84. .Height(24)
  85. .Width(48)
  86. .Image(Placeholders.Image);
  87. text.Span(".");
  88. });
  89. });
  90. }
  91. [Test]
  92. public void TextElements()
  93. {
  94. RenderingTest
  95. .Create()
  96. .PageSize(PageSizes.A4)
  97. .ProducePdf()
  98. .ShowResults()
  99. .Render(container =>
  100. {
  101. container
  102. .Padding(20)
  103. .Padding(10)
  104. .MinimalBox()
  105. .Border(1)
  106. .Padding(5)
  107. .Padding(10)
  108. .Text(text =>
  109. {
  110. text.DefaultTextStyle(TextStyle.Default);
  111. text.AlignLeft();
  112. text.ParagraphSpacing(10);
  113. text.Line(Placeholders.LoremIpsum());
  114. 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());
  115. });
  116. });
  117. }
  118. [Test]
  119. public void Textcolumn()
  120. {
  121. RenderingTest
  122. .Create()
  123. .PageSize(PageSizes.A4)
  124. .ProducePdf()
  125. .ShowResults()
  126. .Render(container =>
  127. {
  128. container
  129. .Padding(20)
  130. .Padding(10)
  131. .MinimalBox()
  132. .Border(1)
  133. .Padding(5)
  134. .Padding(10)
  135. .Text(text =>
  136. {
  137. text.DefaultTextStyle(TextStyle.Default);
  138. text.AlignLeft();
  139. text.ParagraphSpacing(10);
  140. foreach (var i in Enumerable.Range(1, 100))
  141. text.Line($"{i}: {Placeholders.Paragraph()}");
  142. });
  143. });
  144. }
  145. [Test]
  146. public void SpaceIssue()
  147. {
  148. RenderingTest
  149. .Create()
  150. .PageSize(PageSizes.A4)
  151. .ProducePdf()
  152. .ShowResults()
  153. .Render(container =>
  154. {
  155. container
  156. .Padding(20)
  157. .Padding(10)
  158. .MinimalBox()
  159. .Border(1)
  160. .Padding(5)
  161. .Padding(10)
  162. .Text(text =>
  163. {
  164. text.DefaultTextStyle(TextStyle.Default);
  165. text.AlignLeft();
  166. text.ParagraphSpacing(10);
  167. text.Span(Placeholders.LoremIpsum());
  168. text.EmptyLine();
  169. text.Span("This text is a normal text, ");
  170. text.Span("this is a bold text, ", TextStyle.Default.Bold());
  171. text.Span("this is a red and underlined text, ", TextStyle.Default.Color(Colors.Red.Medium).Underline());
  172. text.Span("and this is slightly bigger text.", TextStyle.Default.Size(16));
  173. text.EmptyLine();
  174. text.Span("The new text element also supports injecting custom content between words: ");
  175. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  176. text.Span(".");
  177. text.EmptyLine();
  178. text.Span("This is page number ");
  179. text.CurrentPageNumber();
  180. text.Span(" out of ");
  181. text.TotalPages();
  182. text.EmptyLine();
  183. text.ExternalLocation("Please visit QuestPDF website", "https://www.questpdf.com");
  184. text.EmptyLine();
  185. text.Span(Placeholders.Paragraphs());
  186. text.EmptyLine();
  187. text.Span(Placeholders.Paragraphs(), TextStyle.Default.Italic());
  188. text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
  189. });
  190. });
  191. }
  192. [Test]
  193. public void HugeList()
  194. {
  195. RenderingTest
  196. .Create()
  197. .PageSize(PageSizes.A4)
  198. .ProducePdf()
  199. .ShowResults()
  200. .Render(container =>
  201. {
  202. container
  203. .Padding(20)
  204. .Padding(10)
  205. .MinimalBox()
  206. .Border(1)
  207. .Padding(5)
  208. .Padding(10)
  209. .Text(text =>
  210. {
  211. text.DefaultTextStyle(TextStyle.Default);
  212. text.AlignLeft();
  213. text.ParagraphSpacing(10);
  214. text.Span("This text is a normal text, ");
  215. text.Span("this is a bold text, ", TextStyle.Default.Bold());
  216. text.Span("this is a red and underlined text, ", TextStyle.Default.Color(Colors.Red.Medium).Underline());
  217. text.Span("and this is slightly bigger text.", TextStyle.Default.Size(16));
  218. text.Span("The new text element also supports injecting custom content between words: ");
  219. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  220. text.Span(".");
  221. text.EmptyLine();
  222. foreach (var i in Enumerable.Range(1, 100))
  223. {
  224. text.Line($"{i}: {Placeholders.Paragraph()}");
  225. text.ExternalLocation("Please visit QuestPDF website", "https://www.questpdf.com");
  226. text.Span("This is page number ");
  227. text.CurrentPageNumber();
  228. text.Span(" out of ");
  229. text.TotalPages();
  230. text.EmptyLine();
  231. }
  232. });
  233. });
  234. }
  235. [Test]
  236. public void MeasureIssueWhenSpaceAtLineEnd()
  237. {
  238. // issue 135
  239. RenderingTest
  240. .Create()
  241. .ProduceImages()
  242. .ShowResults()
  243. .RenderDocument(container =>
  244. {
  245. container.Page(page =>
  246. {
  247. page.Margin(50);
  248. page.Background(Colors.White);
  249. page.Size(PageSizes.A4);
  250. page.Content().Text(
  251. "This is a specially crafted sentence with a specially chosen length for demonstration of the bug that occurs ;;;;;. ",
  252. TextStyle.Default.Size(11).BackgroundColor(Colors.Red.Lighten3));
  253. });
  254. });
  255. }
  256. [Test]
  257. public void EmptyText()
  258. {
  259. // issue 135
  260. RenderingTest
  261. .Create()
  262. .ProduceImages()
  263. .ShowResults()
  264. .RenderDocument(container =>
  265. {
  266. container.Page(page =>
  267. {
  268. page.Margin(50);
  269. page.Background(Colors.White);
  270. page.Size(PageSizes.A4);
  271. page.Content().Text(
  272. " ",
  273. TextStyle.Default.Size(11).BackgroundColor(Colors.Red.Lighten3));
  274. });
  275. });
  276. }
  277. [Test]
  278. public void Whitespaces()
  279. {
  280. // issue 135
  281. RenderingTest
  282. .Create()
  283. .ProduceImages()
  284. .ShowResults()
  285. .RenderDocument(container =>
  286. {
  287. container.Page(page =>
  288. {
  289. page.Margin(50);
  290. page.Background(Colors.White);
  291. page.Size(PageSizes.A4);
  292. page.Content().Text(
  293. " x ",
  294. TextStyle.Default.Size(11).BackgroundColor(Colors.Red.Lighten3));
  295. });
  296. });
  297. }
  298. }
  299. }