TextExamples.cs 20 KB

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