TextExample.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using QuestPDF.Elements;
  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 TextExample : ExampleTestBase
  12. {
  13. [ShowResult]
  14. [ImageSize(1200, 500)]
  15. public void Test(IContainer container)
  16. {
  17. List<TextElement> Lorem()
  18. {
  19. return new List<TextElement>
  20. {
  21. new TextElement()
  22. {
  23. Style = TextStyle.Default.Size(32).BackgroundColor(Colors.Red.Lighten3),
  24. Text = "Lorem ipsum "
  25. },
  26. new TextElement()
  27. {
  28. Style = TextStyle.Default.Size(24).BackgroundColor(Colors.Orange.Lighten3),
  29. Text = " dolor "
  30. },
  31. new TextElement()
  32. {
  33. Style = TextStyle.Default.Size(64).BackgroundColor(Colors.Yellow.Lighten3),
  34. Text = " sit "
  35. },
  36. new TextElement()
  37. {
  38. Style = TextStyle.Default.Size(32).BackgroundColor(Colors.Green.Lighten3),
  39. Text = " amet"
  40. }
  41. };
  42. }
  43. Func<List<TextElement>> Source = Lorem ;
  44. container
  45. .Padding(50)
  46. .Stack(row =>
  47. {
  48. row.Spacing(50);
  49. row.Element().Box().Border(1).Stack(stack =>
  50. {
  51. stack
  52. .Element()
  53. .Box()
  54. //.Background(Placeholders.BackgroundColor())
  55. .Element(new TextRun()
  56. {
  57. Elements = Source()
  58. });
  59. stack
  60. .Element()
  61. .Box()
  62. //.Background(Placeholders.BackgroundColor())
  63. .Element(new TextRun()
  64. {
  65. Elements = Source()
  66. });
  67. stack
  68. .Element()
  69. .Box()
  70. //.Background(Placeholders.BackgroundColor())
  71. .Element(new TextRun()
  72. {
  73. Elements = Split(Source())
  74. });
  75. });
  76. row.Element().Box().Border(1).Stack(stack =>
  77. {
  78. stack
  79. .Element()
  80. .Box()
  81. .Background(Placeholders.BackgroundColor())
  82. .Element(new TextRun()
  83. {
  84. Elements = Dense(Source())
  85. });
  86. stack
  87. .Element()
  88. .Box()
  89. .Background(Placeholders.BackgroundColor())
  90. .Element(new TextRun()
  91. {
  92. Elements = Dense(Source())
  93. });
  94. stack
  95. .Element()
  96. .Box()
  97. .Background(Placeholders.BackgroundColor())
  98. .Element(new TextRun()
  99. {
  100. Elements = Dense(Split(Source()))
  101. });
  102. });
  103. });
  104. }
  105. List<TextElement> RandomText()
  106. {
  107. return Placeholders
  108. .Sentence()
  109. .Split(" ")
  110. .Select(x => new TextElement
  111. {
  112. Text = $"{x} ",
  113. Style = TextStyle.Default.Size(Placeholders.Random.Next(24, 64))
  114. })
  115. .ToList();
  116. }
  117. List<TextElement> Split(List<TextElement> elements)
  118. {
  119. return elements
  120. .SelectMany(x => x
  121. .Text
  122. .Select(y => new TextElement
  123. {
  124. Style = x.Style,
  125. Text = y.ToString()
  126. }))
  127. .ToList();
  128. }
  129. List<TextElement> Dense(List<TextElement> elements)
  130. {
  131. elements.ForEach(x => x.Style.IsDense = true);
  132. return elements;
  133. }
  134. }
  135. }