2
0

TextExamples.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 SuperscriptSubscript()
  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.Span("E=mc");
  55. text.Span("2").Superscript();
  56. text.EmptyLine();
  57. text.Span("H");
  58. text.Span("2").Subscript();
  59. text.Span("O");
  60. text.EmptyLine();
  61. text.ParagraphSpacing(2);
  62. var style = TextStyle.Default.Underline();
  63. text.Span("Subscript").Style(style).Subscript().BackgroundColor(Colors.Green.Medium);
  64. text.Span("Normal").Style(style).NormalPosition().BackgroundColor(Colors.Blue.Medium);
  65. text.Line("Superscript").Style(style).Superscript().BackgroundColor(Colors.Red.Medium);
  66. text.Line("Superscript").Style(style).Superscript().BackgroundColor(Colors.Green.Medium);
  67. text.Line("Normal").Style(style).NormalPosition().BackgroundColor(Colors.Blue.Medium);
  68. text.Line("Subscript").Style(style).Subscript().BackgroundColor(Colors.Red.Medium);
  69. });
  70. });
  71. }
  72. [Test]
  73. public void ParagraphSpacing()
  74. {
  75. RenderingTest
  76. .Create()
  77. .PageSize(500, 300)
  78. .ProduceImages()
  79. .ShowResults()
  80. .Render(container =>
  81. {
  82. container
  83. .Padding(5)
  84. .MinimalBox()
  85. .Border(1)
  86. .Padding(10)
  87. .Text(text =>
  88. {
  89. text.ParagraphSpacing(10);
  90. foreach (var i in Enumerable.Range(1, 3))
  91. {
  92. text.Span($"Paragraph {i}: ").SemiBold();
  93. text.Line(Placeholders.Paragraph());
  94. }
  95. });
  96. });
  97. }
  98. [Test]
  99. public void CustomElement()
  100. {
  101. RenderingTest
  102. .Create()
  103. .PageSize(500, 200)
  104. .ProduceImages()
  105. .ShowResults()
  106. .Render(container =>
  107. {
  108. container
  109. .Padding(5)
  110. .MinimalBox()
  111. .Border(1)
  112. .Padding(10)
  113. .Text(text =>
  114. {
  115. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  116. text.Span("This is a random image aligned to the baseline: ");
  117. text.Element()
  118. .PaddingBottom(-6)
  119. .Height(24)
  120. .Width(48)
  121. .Image(Placeholders.Image);
  122. text.Span(".");
  123. });
  124. });
  125. }
  126. [Test]
  127. public void TextElements()
  128. {
  129. RenderingTest
  130. .Create()
  131. .PageSize(PageSizes.A4)
  132. .ProducePdf()
  133. .ShowResults()
  134. .Render(container =>
  135. {
  136. container
  137. .Padding(20)
  138. .Padding(10)
  139. .MinimalBox()
  140. .Border(1)
  141. .Padding(5)
  142. .Padding(10)
  143. .Text(text =>
  144. {
  145. text.DefaultTextStyle(TextStyle.Default);
  146. text.AlignLeft();
  147. text.ParagraphSpacing(10);
  148. text.Line(Placeholders.LoremIpsum());
  149. 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();
  150. });
  151. });
  152. }
  153. [Test]
  154. public void Textcolumn()
  155. {
  156. RenderingTest
  157. .Create()
  158. .PageSize(PageSizes.A4)
  159. .ProducePdf()
  160. .ShowResults()
  161. .Render(container =>
  162. {
  163. container
  164. .Padding(20)
  165. .Padding(10)
  166. .MinimalBox()
  167. .Border(1)
  168. .Padding(5)
  169. .Padding(10)
  170. .Text(text =>
  171. {
  172. text.DefaultTextStyle(TextStyle.Default);
  173. text.AlignLeft();
  174. text.ParagraphSpacing(10);
  175. foreach (var i in Enumerable.Range(1, 100))
  176. text.Line($"{i}: {Placeholders.Paragraph()}");
  177. });
  178. });
  179. }
  180. [Test]
  181. public void SpaceIssue()
  182. {
  183. RenderingTest
  184. .Create()
  185. .PageSize(PageSizes.A4)
  186. .ProducePdf()
  187. .ShowResults()
  188. .Render(container =>
  189. {
  190. container
  191. .Padding(20)
  192. .Padding(10)
  193. .MinimalBox()
  194. .Border(1)
  195. .Padding(5)
  196. .Padding(10)
  197. .Text(text =>
  198. {
  199. text.DefaultTextStyle(x => x.Bold());
  200. text.DefaultTextStyle(TextStyle.Default);
  201. text.AlignLeft();
  202. text.ParagraphSpacing(10);
  203. text.Span(Placeholders.LoremIpsum());
  204. text.EmptyLine();
  205. text.Span("This text is a normal text, ");
  206. text.Span("this is a bold text, ").Bold();
  207. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  208. text.Span("and this is slightly bigger text.").FontSize(16);
  209. text.EmptyLine();
  210. text.Span("The new text element also supports injecting custom content between words: ");
  211. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  212. text.Span(".");
  213. text.EmptyLine();
  214. text.Span("This is page number ");
  215. text.CurrentPageNumber();
  216. text.Span(" out of ");
  217. text.TotalPages();
  218. text.EmptyLine();
  219. text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
  220. text.EmptyLine();
  221. text.Span(Placeholders.Paragraphs());
  222. text.EmptyLine();
  223. text.Span(Placeholders.Paragraphs()).Italic();
  224. text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
  225. });
  226. });
  227. }
  228. [Test]
  229. public void HugeList()
  230. {
  231. RenderingTest
  232. .Create()
  233. .PageSize(PageSizes.A4)
  234. .ProducePdf()
  235. .ShowResults()
  236. .Render(container =>
  237. {
  238. container
  239. .Padding(20)
  240. .Padding(10)
  241. .MinimalBox()
  242. .Border(1)
  243. .Padding(5)
  244. .Padding(10)
  245. .Text(text =>
  246. {
  247. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  248. text.AlignLeft();
  249. text.ParagraphSpacing(10);
  250. text.Span("This text is a normal text, ");
  251. text.Span("this is a bold text, ").Bold();
  252. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  253. text.Span("and this is slightly bigger text.").FontSize(16);
  254. text.Span("The new text element also supports injecting custom content between words: ");
  255. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  256. text.Span(".");
  257. text.EmptyLine();
  258. foreach (var i in Enumerable.Range(1, 100))
  259. {
  260. text.Line($"{i}: {Placeholders.Paragraph()}");
  261. text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
  262. text.Span("This is page number ");
  263. text.CurrentPageNumber();
  264. text.Span(" out of ");
  265. text.TotalPages();
  266. text.EmptyLine();
  267. }
  268. });
  269. });
  270. }
  271. [Test]
  272. public void MeasureIssueWhenSpaceAtLineEnd()
  273. {
  274. // issue 135
  275. RenderingTest
  276. .Create()
  277. .ProduceImages()
  278. .ShowResults()
  279. .RenderDocument(container =>
  280. {
  281. container.Page(page =>
  282. {
  283. page.Margin(50);
  284. page.PageColor(Colors.White);
  285. page.Size(PageSizes.A4);
  286. 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);
  287. });
  288. });
  289. }
  290. [Test]
  291. public void EmptyText()
  292. {
  293. // issue 135
  294. RenderingTest
  295. .Create()
  296. .ProduceImages()
  297. .ShowResults()
  298. .RenderDocument(container =>
  299. {
  300. container.Page(page =>
  301. {
  302. page.Margin(50);
  303. page.PageColor(Colors.White);
  304. page.Size(PageSizes.A4);
  305. page.Content().Text(" ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  306. });
  307. });
  308. }
  309. [Test]
  310. public void Whitespaces()
  311. {
  312. // issue 135
  313. RenderingTest
  314. .Create()
  315. .ProduceImages()
  316. .ShowResults()
  317. .RenderDocument(container =>
  318. {
  319. container.Page(page =>
  320. {
  321. page.Margin(50);
  322. page.PageColor(Colors.White);
  323. page.Size(PageSizes.A4);
  324. page.Content().Text(" x ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  325. });
  326. });
  327. }
  328. [Test]
  329. public void DrawingNullTextShouldNotThrowException()
  330. {
  331. RenderingTest
  332. .Create()
  333. .ProduceImages()
  334. .ShowResults()
  335. .RenderDocument(container =>
  336. {
  337. container.Page(page =>
  338. {
  339. page.Margin(50);
  340. page.PageColor(Colors.White);
  341. page.Size(PageSizes.A4);
  342. page.Content().Column(column =>
  343. {
  344. column.Item().Text(null);
  345. column.Item().Text(text =>
  346. {
  347. text.Span(null);
  348. text.Line(null);
  349. text.Hyperlink(null, "http://www.questpdf.com");
  350. text.TotalPages().Format(x => null);
  351. });
  352. });
  353. });
  354. });
  355. }
  356. [Test]
  357. public void BreakingLongWord()
  358. {
  359. RenderingTest
  360. .Create()
  361. .ProduceImages()
  362. .ShowResults()
  363. .RenderDocument(container =>
  364. {
  365. container.Page(page =>
  366. {
  367. page.Margin(50);
  368. page.PageColor(Colors.White);
  369. page.Size(PageSizes.A4);
  370. page.Content().Column(column =>
  371. {
  372. column.Item().Text(null);
  373. column.Item().Text(text =>
  374. {
  375. text.DefaultTextStyle(x => x.BackgroundColor(Colors.Red.Lighten3).FontSize(24));
  376. text.Span(" " + Placeholders.LoremIpsum());
  377. text.Span(" 0123456789012345678901234567890123456789012345678901234567890123456789 ").WrapAnywhere();
  378. });
  379. });
  380. });
  381. });
  382. }
  383. }
  384. }