TextExamples.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.Size(20));
  31. text.Span("This is a normal text, followed by an ");
  32. text.Span("underlined red text.").Size(20).Color(Colors.Red.Medium).Underline();
  33. });
  34. });
  35. }
  36. [Test]
  37. public void ParagraphSpacing()
  38. {
  39. RenderingTest
  40. .Create()
  41. .PageSize(500, 300)
  42. .ProduceImages()
  43. .ShowResults()
  44. .Render(container =>
  45. {
  46. container
  47. .Padding(5)
  48. .MinimalBox()
  49. .Border(1)
  50. .Padding(10)
  51. .Text(text =>
  52. {
  53. text.ParagraphSpacing(10);
  54. foreach (var i in Enumerable.Range(1, 3))
  55. {
  56. text.Span($"Paragraph {i}: ").SemiBold();
  57. text.Line(Placeholders.Paragraph());
  58. }
  59. });
  60. });
  61. }
  62. [Test]
  63. public void CustomElement()
  64. {
  65. RenderingTest
  66. .Create()
  67. .PageSize(500, 200)
  68. .ProduceImages()
  69. .ShowResults()
  70. .Render(container =>
  71. {
  72. container
  73. .Padding(5)
  74. .MinimalBox()
  75. .Border(1)
  76. .Padding(10)
  77. .Text(text =>
  78. {
  79. text.DefaultTextStyle(TextStyle.Default.Size(20));
  80. text.Span("This is a random image aligned to the baseline: ");
  81. text.Element()
  82. .PaddingBottom(-6)
  83. .Height(24)
  84. .Width(48)
  85. .Image(Placeholders.Image);
  86. text.Span(".");
  87. });
  88. });
  89. }
  90. [Test]
  91. public void TextElements()
  92. {
  93. RenderingTest
  94. .Create()
  95. .PageSize(PageSizes.A4)
  96. .ProducePdf()
  97. .ShowResults()
  98. .Render(container =>
  99. {
  100. container
  101. .Padding(20)
  102. .Padding(10)
  103. .MinimalBox()
  104. .Border(1)
  105. .Padding(5)
  106. .Padding(10)
  107. .Text(text =>
  108. {
  109. text.DefaultTextStyle(TextStyle.Default);
  110. text.AlignLeft();
  111. text.ParagraphSpacing(10);
  112. text.Line(Placeholders.LoremIpsum());
  113. 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();
  114. });
  115. });
  116. }
  117. [Test]
  118. public void Textcolumn()
  119. {
  120. RenderingTest
  121. .Create()
  122. .PageSize(PageSizes.A4)
  123. .ProducePdf()
  124. .ShowResults()
  125. .Render(container =>
  126. {
  127. container
  128. .Padding(20)
  129. .Padding(10)
  130. .MinimalBox()
  131. .Border(1)
  132. .Padding(5)
  133. .Padding(10)
  134. .Text(text =>
  135. {
  136. text.DefaultTextStyle(TextStyle.Default);
  137. text.AlignLeft();
  138. text.ParagraphSpacing(10);
  139. foreach (var i in Enumerable.Range(1, 100))
  140. text.Line($"{i}: {Placeholders.Paragraph()}");
  141. });
  142. });
  143. }
  144. [Test]
  145. public void SpaceIssue()
  146. {
  147. RenderingTest
  148. .Create()
  149. .PageSize(PageSizes.A4)
  150. .ProducePdf()
  151. .ShowResults()
  152. .Render(container =>
  153. {
  154. container
  155. .Padding(20)
  156. .Padding(10)
  157. .MinimalBox()
  158. .Border(1)
  159. .Padding(5)
  160. .Padding(10)
  161. .Text(text =>
  162. {
  163. text.DefaultTextStyle(x => x.Bold());
  164. text.DefaultTextStyle(TextStyle.Default);
  165. text.AlignLeft();
  166. text.ParagraphSpacing(10);
  167. text.Span(Placeholders.LoremIpsum());
  168. text.EmptyLine();
  169. text.Span("This text is a normal text, ");
  170. text.Span("this is a bold text, ").Bold();
  171. text.Span("this is a red and underlined text, ").Color(Colors.Red.Medium).Underline();
  172. text.Span("and this is slightly bigger text.").Size(16);
  173. text.EmptyLine();
  174. text.Span("The new text element also supports injecting custom content between words: ");
  175. text.Element().PaddingBottom(-10).Height(16).Width(32).Image(Placeholders.Image);
  176. text.Span(".");
  177. text.EmptyLine();
  178. text.Span("This is page number ");
  179. text.CurrentPageNumber();
  180. text.Span(" out of ");
  181. text.TotalPages();
  182. text.EmptyLine();
  183. text.ExternalLocation("Please visit QuestPDF website", "https://www.questpdf.com");
  184. text.EmptyLine();
  185. text.Span(Placeholders.Paragraphs());
  186. text.EmptyLine();
  187. text.Span(Placeholders.Paragraphs()).Italic();
  188. text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
  189. });
  190. });
  191. }
  192. [Test]
  193. public void HugeList()
  194. {
  195. RenderingTest
  196. .Create()
  197. .PageSize(PageSizes.A4)
  198. .ProducePdf()
  199. .ShowResults()
  200. .Render(container =>
  201. {
  202. container
  203. .Padding(20)
  204. .Padding(10)
  205. .MinimalBox()
  206. .Border(1)
  207. .Padding(5)
  208. .Padding(10)
  209. .Text(text =>
  210. {
  211. text.DefaultTextStyle(TextStyle.Default);
  212. text.AlignLeft();
  213. text.ParagraphSpacing(10);
  214. text.Span("This text is a normal text, ");
  215. text.Span("this is a bold text, ").Bold();
  216. text.Span("this is a red and underlined text, ").Color(Colors.Red.Medium).Underline();
  217. text.Span("and this is slightly bigger text.").Size(16);
  218. text.Span("The new text element also supports injecting custom content between words: ");
  219. text.Element().PaddingBottom(-10).Height(16).Width(32).Image(Placeholders.Image);
  220. text.Span(".");
  221. text.EmptyLine();
  222. foreach (var i in Enumerable.Range(1, 100))
  223. {
  224. text.Line($"{i}: {Placeholders.Paragraph()}");
  225. text.ExternalLocation("Please visit QuestPDF website", "https://www.questpdf.com");
  226. text.Span("This is page number ");
  227. text.CurrentPageNumber();
  228. text.Span(" out of ");
  229. text.TotalPages();
  230. text.EmptyLine();
  231. }
  232. });
  233. });
  234. }
  235. }
  236. }