TextExamples.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 SimpleText()
  15. {
  16. RenderingTest
  17. .Create()
  18. .PageSize(500, 100)
  19. .ProduceImages()
  20. .ShowResults()
  21. .Render(container =>
  22. {
  23. container
  24. .Padding(5)
  25. .MinimalBox()
  26. .Border(1)
  27. .Padding(10)
  28. .Text(Placeholders.Paragraph());
  29. });
  30. }
  31. [Test]
  32. public void SimpleTextBlock()
  33. {
  34. RenderingTest
  35. .Create()
  36. .PageSize(600, 300)
  37. .ProduceImages()
  38. .ShowResults()
  39. .Render(container =>
  40. {
  41. container
  42. .Padding(5)
  43. .MinimalBox()
  44. .Border(1)
  45. .MaxWidth(300)
  46. .Padding(10)
  47. .Text(text =>
  48. {
  49. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  50. text.Span("This is a normal text, followed by an ");
  51. text.Span("underlined red text").FontColor(Colors.Red.Medium).Underline();
  52. text.Span(".");
  53. });
  54. });
  55. }
  56. [Test]
  57. public void TextWeight()
  58. {
  59. RenderingTest
  60. .Create()
  61. .PageSize(500, 500)
  62. .ProduceImages()
  63. .ShowResults()
  64. .Render(container =>
  65. {
  66. container
  67. .Padding(20)
  68. .MinimalBox()
  69. .Border(1)
  70. .Padding(20)
  71. .Text(text =>
  72. {
  73. text.DefaultTextStyle(x => x.FontFamily(Fonts.Calibri).FontSize(20));
  74. text.Line("Thin").Thin();
  75. text.Line("ExtraLight").ExtraLight();
  76. text.Line("Light").Light();
  77. text.Line("NormalWeight").NormalWeight();
  78. text.Line("Medium").Medium();
  79. text.Line("SemiBold").SemiBold();
  80. text.Line("Bold").Bold();
  81. text.Line("ExtraBold").ExtraBold();
  82. text.Line("Black").Black();
  83. text.Line("ExtraBlack").ExtraBlack();
  84. });
  85. });
  86. }
  87. [Test]
  88. public void LineHeight()
  89. {
  90. RenderingTest
  91. .Create()
  92. .PageSize(500, 700)
  93. .ProduceImages()
  94. .ShowResults()
  95. .Render(container =>
  96. {
  97. container
  98. .Padding(20)
  99. .Column(column =>
  100. {
  101. var lineHeights = new[] { 0.8f, 1f, 1.5f };
  102. var paragraph = Placeholders.Paragraph();
  103. foreach (var lineHeight in lineHeights)
  104. {
  105. column
  106. .Item()
  107. .Border(1)
  108. .Padding(10)
  109. .Text(paragraph)
  110. .FontSize(16)
  111. .LineHeight(lineHeight);
  112. }
  113. });
  114. });
  115. }
  116. [Test]
  117. public void SuperscriptSubscript_Simple()
  118. {
  119. RenderingTest
  120. .Create()
  121. .PageSize(500, 500)
  122. .ProduceImages()
  123. .ShowResults()
  124. .Render(container =>
  125. {
  126. container
  127. .Padding(20)
  128. .MinimalBox()
  129. .Border(1)
  130. .Padding(20)
  131. .Text(text =>
  132. {
  133. text.DefaultTextStyle(x => x.FontSize(20));
  134. text.ParagraphSpacing(10);
  135. var highlight = TextStyle.Default.BackgroundColor(Colors.Green.Lighten3);
  136. text.Span("E=mc").Style(highlight);
  137. text.Span("2").Superscript().Style(highlight);
  138. text.Span(" is the equation of mass–energy equivalence.");
  139. text.EmptyLine();
  140. text.Span("H").Style(highlight);
  141. text.Span("2").Subscript().Style(highlight);
  142. text.Span("O").Style(highlight);
  143. text.Span(" is the chemical formula for water.");
  144. });
  145. });
  146. }
  147. [Test]
  148. public void SuperscriptSubscript_Effects()
  149. {
  150. RenderingTest
  151. .Create()
  152. .PageSize(800, 400)
  153. .ProduceImages()
  154. .ShowResults()
  155. .Render(container =>
  156. {
  157. container
  158. .Padding(25)
  159. .DefaultTextStyle(x => x.FontSize(30))
  160. .Column(column =>
  161. {
  162. column.Spacing(25);
  163. column.Item().Text(text =>
  164. {
  165. text.DefaultTextStyle(x => x.Underline());
  166. text.Span("Underline of the superscript (E = mc");
  167. text.Span("2").Superscript();
  168. text.Span(") should be at the same height as for normal text.");
  169. });
  170. column.Item().Text(text =>
  171. {
  172. text.DefaultTextStyle(x => x.Underline());
  173. text.Span("Underline of the subscript(H");
  174. text.Span("2").Subscript();
  175. text.Span("O) should be slightly lower than a normal text.");
  176. });
  177. column.Item().Text(text =>
  178. {
  179. text.DefaultTextStyle(x => x.Strikethrough());
  180. text.Span("Strikethrough of both superscript (E=mc");
  181. text.Span("2").Superscript();
  182. text.Span(") and subscript(H");
  183. text.Span("2").Subscript();
  184. text.Span("O) should be visible in the middle of the text.");
  185. });
  186. });
  187. });
  188. }
  189. [Test]
  190. public void ParagraphSpacing()
  191. {
  192. RenderingTest
  193. .Create()
  194. .PageSize(500, 500)
  195. .ProduceImages()
  196. .ShowResults()
  197. .Render(container =>
  198. {
  199. container
  200. .Padding(5)
  201. .MinimalBox()
  202. .Border(1)
  203. .Padding(10)
  204. .Text(text =>
  205. {
  206. text.Line(Placeholders.Paragraph());
  207. });
  208. });
  209. }
  210. [Test]
  211. public void CustomElement()
  212. {
  213. RenderingTest
  214. .Create()
  215. .PageSize(500, 200)
  216. .ProduceImages()
  217. .ShowResults()
  218. .Render(container =>
  219. {
  220. container
  221. .Padding(5)
  222. .MinimalBox()
  223. .Border(1)
  224. .Padding(10)
  225. .Text(text =>
  226. {
  227. text.DefaultTextStyle(TextStyle.Default.FontSize(20));
  228. text.Span("This is a random image aligned to the baseline: ");
  229. text.Element()
  230. .PaddingBottom(-6)
  231. .Height(24)
  232. .Width(48)
  233. .Image(Placeholders.Image);
  234. text.Span(".");
  235. });
  236. });
  237. }
  238. [Test]
  239. public void TextElements()
  240. {
  241. RenderingTest
  242. .Create()
  243. .PageSize(PageSizes.A4)
  244. .ProducePdf()
  245. .ShowResults()
  246. .Render(container =>
  247. {
  248. container
  249. .Padding(20)
  250. .Padding(10)
  251. .MinimalBox()
  252. .Border(1)
  253. .Padding(5)
  254. .Padding(10)
  255. .Text(text =>
  256. {
  257. text.DefaultTextStyle(TextStyle.Default);
  258. text.AlignLeft();
  259. text.ParagraphSpacing(10);
  260. text.Line(Placeholders.LoremIpsum());
  261. 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();
  262. });
  263. });
  264. }
  265. [Test]
  266. public void Textcolumn()
  267. {
  268. RenderingTest
  269. .Create()
  270. .PageSize(PageSizes.A4)
  271. .ProducePdf()
  272. .ShowResults()
  273. .Render(container =>
  274. {
  275. container
  276. .Padding(20)
  277. .Padding(10)
  278. .MinimalBox()
  279. .Border(1)
  280. .Padding(5)
  281. .Padding(10)
  282. .Text(text =>
  283. {
  284. text.DefaultTextStyle(TextStyle.Default);
  285. text.AlignLeft();
  286. text.ParagraphSpacing(10);
  287. foreach (var i in Enumerable.Range(1, 100))
  288. text.Line($"{i}: {Placeholders.Paragraph()}");
  289. });
  290. });
  291. }
  292. [Test]
  293. public void SpaceIssue()
  294. {
  295. RenderingTest
  296. .Create()
  297. .PageSize(PageSizes.A4)
  298. .ProducePdf()
  299. .ShowResults()
  300. .Render(container =>
  301. {
  302. container
  303. .Padding(20)
  304. .Padding(10)
  305. .MinimalBox()
  306. .Border(1)
  307. .Padding(5)
  308. .Padding(10)
  309. .Text(text =>
  310. {
  311. text.DefaultTextStyle(x => x.Bold());
  312. text.DefaultTextStyle(TextStyle.Default);
  313. text.AlignLeft();
  314. text.ParagraphSpacing(10);
  315. text.Span(Placeholders.LoremIpsum());
  316. text.EmptyLine();
  317. text.Span("This text is a normal text, ");
  318. text.Span("this is a bold text, ").Bold();
  319. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  320. text.Span("and this is slightly bigger text.").FontSize(16);
  321. text.EmptyLine();
  322. text.Span("The new text element also supports injecting custom content between words: ");
  323. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  324. text.Span(".");
  325. text.EmptyLine();
  326. text.Span("This is page number ");
  327. text.CurrentPageNumber();
  328. text.Span(" out of ");
  329. text.TotalPages();
  330. text.EmptyLine();
  331. text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
  332. text.EmptyLine();
  333. text.Span(Placeholders.Paragraphs());
  334. text.EmptyLine();
  335. text.Span(Placeholders.Paragraphs()).Italic();
  336. text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
  337. });
  338. });
  339. }
  340. [Test]
  341. public void HugeList()
  342. {
  343. RenderingTest
  344. .Create()
  345. .PageSize(PageSizes.A4)
  346. .ProducePdf()
  347. .ShowResults()
  348. .Render(container =>
  349. {
  350. container
  351. .Padding(20)
  352. .Padding(10)
  353. .MinimalBox()
  354. .Border(1)
  355. .Padding(5)
  356. .Padding(10)
  357. .Text(text =>
  358. {
  359. text.DefaultTextStyle(TextStyle.Default.FontSize(20).BackgroundColor(Colors.Red.Lighten4));
  360. text.AlignLeft();
  361. text.ParagraphSpacing(10);
  362. text.Span("This text is a normal text, ");
  363. text.Span("this is a bold text, ").Bold();
  364. text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
  365. text.Span("and this is slightly bigger text.").FontSize(16);
  366. text.Span("The new text element also supports injecting custom content between words: ");
  367. text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
  368. text.Span(".");
  369. text.EmptyLine();
  370. foreach (var i in Enumerable.Range(1, 100))
  371. {
  372. text.Line($"{i}: {Placeholders.Paragraph()}");
  373. text.Hyperlink("Please visit QuestPDF website. ", "https://www.questpdf.com");
  374. text.Span("This is page number ");
  375. text.CurrentPageNumber();
  376. text.Span(" out of ");
  377. text.TotalPages();
  378. text.EmptyLine();
  379. }
  380. });
  381. });
  382. }
  383. [Test]
  384. public void MeasureIssueWhenSpaceAtLineEnd()
  385. {
  386. // issue 135
  387. RenderingTest
  388. .Create()
  389. .ProduceImages()
  390. .ShowResults()
  391. .RenderDocument(container =>
  392. {
  393. container.Page(page =>
  394. {
  395. page.Margin(50);
  396. page.PageColor(Colors.White);
  397. page.Size(PageSizes.A4);
  398. 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);
  399. });
  400. });
  401. }
  402. [Test]
  403. public void EmptyText()
  404. {
  405. // issue 135
  406. RenderingTest
  407. .Create()
  408. .ProduceImages()
  409. .ShowResults()
  410. .RenderDocument(container =>
  411. {
  412. container.Page(page =>
  413. {
  414. page.Margin(50);
  415. page.PageColor(Colors.White);
  416. page.Size(PageSizes.A4);
  417. page.Content().Text(" ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  418. });
  419. });
  420. }
  421. [Test]
  422. public void Whitespaces()
  423. {
  424. // issue 135
  425. RenderingTest
  426. .Create()
  427. .ProduceImages()
  428. .ShowResults()
  429. .RenderDocument(container =>
  430. {
  431. container.Page(page =>
  432. {
  433. page.Margin(50);
  434. page.PageColor(Colors.White);
  435. page.Size(PageSizes.A4);
  436. page.Content().Text(" x ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
  437. });
  438. });
  439. }
  440. [Test]
  441. public void DrawingNullTextShouldNotThrowException()
  442. {
  443. RenderingTest
  444. .Create()
  445. .ProduceImages()
  446. .ShowResults()
  447. .RenderDocument(container =>
  448. {
  449. container.Page(page =>
  450. {
  451. page.Margin(50);
  452. page.PageColor(Colors.White);
  453. page.Size(PageSizes.A4);
  454. page.Content().Column(column =>
  455. {
  456. column.Item().Text(null);
  457. column.Item().Text(text =>
  458. {
  459. text.Span(null);
  460. text.Line(null);
  461. text.Hyperlink(null, "http://www.questpdf.com");
  462. text.TotalPages().Format(x => null);
  463. });
  464. });
  465. });
  466. });
  467. }
  468. [Test]
  469. public void BreakingLongWord()
  470. {
  471. RenderingTest
  472. .Create()
  473. .ProduceImages()
  474. .ShowResults()
  475. .RenderDocument(container =>
  476. {
  477. container.Page(page =>
  478. {
  479. page.Margin(50);
  480. page.PageColor(Colors.White);
  481. page.Size(PageSizes.A4);
  482. page.Content().Column(column =>
  483. {
  484. column.Item().Text(null);
  485. column.Item().Text(text =>
  486. {
  487. text.DefaultTextStyle(x => x.BackgroundColor(Colors.Red.Lighten3).FontSize(24));
  488. text.Span(" " + Placeholders.LoremIpsum());
  489. text.Span(" 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 ").WrapAnywhere();
  490. });
  491. });
  492. });
  493. });
  494. }
  495. }
  496. }