TextExamples.cs 18 KB

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