TextExamples.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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.FontSize(20));
  31. text.Span("This is a normal text, followed by an ");
  32. text.Span("underlined red text").FontColor(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}: ").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.FontSize(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? <").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(x => x.Bold());
  165. text.DefaultTextStyle(TextStyle.Default);
  166. text.AlignLeft();
  167. text.ParagraphSpacing(10);
  168. text.Span(Placeholders.LoremIpsum());
  169. text.EmptyLine();
  170. text.Span("This text is a normal text, ");
  171. text.Span("this is a bold text, ").Bold();
  172. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  173. text.Span("and this is slightly bigger text.").FontSize(16);
  174. text.EmptyLine();
  175. text.Span("The new text element also supports injecting custom content between words: ");
  176. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  177. text.Span(".");
  178. text.EmptyLine();
  179. text.Span("This is page number ");
  180. text.CurrentPageNumber();
  181. text.Span(" out of ");
  182. text.TotalPages();
  183. text.EmptyLine();
  184. text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
  185. text.EmptyLine();
  186. text.Span(Placeholders.Paragraphs());
  187. text.EmptyLine();
  188. text.Span(Placeholders.Paragraphs()).Italic();
  189. text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
  190. });
  191. });
  192. }
  193. [Test]
  194. public void HugeList()
  195. {
  196. RenderingTest
  197. .Create()
  198. .PageSize(PageSizes.A4)
  199. .ProducePdf()
  200. .ShowResults()
  201. .Render(container =>
  202. {
  203. container
  204. .Padding(20)
  205. .Padding(10)
  206. .MinimalBox()
  207. .Border(1)
  208. .Padding(5)
  209. .Padding(10)
  210. .Text(text =>
  211. {
  212. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  213. text.AlignLeft();
  214. text.ParagraphSpacing(10);
  215. text.Span("This text is a normal text, ");
  216. text.Span("this is a bold text, ").Bold();
  217. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  218. text.Span("and this is slightly bigger text.").FontSize(16);
  219. text.Span("The new text element also supports injecting custom content between words: ");
  220. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  221. text.Span(".");
  222. text.EmptyLine();
  223. foreach (var i in Enumerable.Range(1, 100))
  224. {
  225. text.Line($"{i}: {Placeholders.Paragraph()}");
  226. text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
  227. text.Span("This is page number ");
  228. text.CurrentPageNumber();
  229. text.Span(" out of ");
  230. text.TotalPages();
  231. text.EmptyLine();
  232. }
  233. });
  234. });
  235. }
  236. [Test]
  237. public void MeasureIssueWhenSpaceAtLineEnd()
  238. {
  239. // issue 135
  240. RenderingTest
  241. .Create()
  242. .ProduceImages()
  243. .ShowResults()
  244. .RenderDocument(container =>
  245. {
  246. container.Page(page =>
  247. {
  248. page.Margin(50);
  249. page.PageColor(Colors.White);
  250. page.Size(PageSizes.A4);
  251. page.Content().Text("This is a specially crafted sentence with a specially chosen length for demonstration of the bug that occurs ;;;;;. ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  252. });
  253. });
  254. }
  255. [Test]
  256. public void EmptyText()
  257. {
  258. // issue 135
  259. RenderingTest
  260. .Create()
  261. .ProduceImages()
  262. .ShowResults()
  263. .RenderDocument(container =>
  264. {
  265. container.Page(page =>
  266. {
  267. page.Margin(50);
  268. page.PageColor(Colors.White);
  269. page.Size(PageSizes.A4);
  270. page.Content().Text(" ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  271. });
  272. });
  273. }
  274. [Test]
  275. public void Whitespaces()
  276. {
  277. // issue 135
  278. RenderingTest
  279. .Create()
  280. .ProduceImages()
  281. .ShowResults()
  282. .RenderDocument(container =>
  283. {
  284. container.Page(page =>
  285. {
  286. page.Margin(50);
  287. page.PageColor(Colors.White);
  288. page.Size(PageSizes.A4);
  289. page.Content().Text(" x ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  290. });
  291. });
  292. }
  293. [Test]
  294. public void DrawingNullTextShouldNotThrowException()
  295. {
  296. RenderingTest
  297. .Create()
  298. .ProduceImages()
  299. .ShowResults()
  300. .RenderDocument(container =>
  301. {
  302. container.Page(page =>
  303. {
  304. page.Margin(50);
  305. page.PageColor(Colors.White);
  306. page.Size(PageSizes.A4);
  307. page.Content().Column(column =>
  308. {
  309. column.Item().Text(null);
  310. column.Item().Text(text =>
  311. {
  312. text.Span(null);
  313. text.Line(null);
  314. text.Hyperlink(null, "http://www.questpdf.com");
  315. text.TotalPages().Format(x => null);
  316. });
  317. });
  318. });
  319. });
  320. }
  321. }
  322. }