ParagraphStyleExamples.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using QuestPDF.Fluent;
  2. using QuestPDF.Helpers;
  3. using QuestPDF.Infrastructure;
  4. namespace QuestPDF.DocumentationExamples.Text;
  5. public class ParagraphStyleExamples
  6. {
  7. [Test]
  8. public void DefaultTextStyle()
  9. {
  10. Document
  11. .Create(document =>
  12. {
  13. document.Page(page =>
  14. {
  15. page.MinSize(new PageSize(0, 0));
  16. page.MaxSize(new PageSize(400, 1000));
  17. page.DefaultTextStyle(x => x.FontSize(20));
  18. page.Margin(25);
  19. page.Content()
  20. .Text(text =>
  21. {
  22. text.DefaultTextStyle(x => x.Light().LetterSpacing(-0.1f).WordSpacing(0.1f));
  23. text.Span("Changing typography settings helps creating ");
  24. text.Span("significant").LetterSpacing(0.2f).Black().BackgroundColor(Colors.Grey.Lighten2);
  25. text.Span(" visual contrast.");
  26. });
  27. });
  28. })
  29. .GenerateImages(x => "text-paragraph-default-style.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.VeryHigh, RasterDpi = 144 });
  30. }
  31. [Test]
  32. public void TextAlignment()
  33. {
  34. Document
  35. .Create(document =>
  36. {
  37. document.Page(page =>
  38. {
  39. page.MinSize(new PageSize(0, 0));
  40. page.MaxSize(new PageSize(400, 1000));
  41. page.DefaultTextStyle(x => x.FontSize(20));
  42. page.Margin(25);
  43. page.Content()
  44. .Column(column =>
  45. {
  46. column.Spacing(20);
  47. column.Item()
  48. .Element(CellStyle)
  49. .Text("This is an example of left-aligned text, showcasing how the text starts from the left margin and continues naturally across the container.")
  50. .AlignLeft();
  51. column.Item()
  52. .Element(CellStyle)
  53. .Text("This text is centered within its container, creating a balanced look, especially for titles or headers.")
  54. .AlignCenter();
  55. column.Item()
  56. .Element(CellStyle)
  57. .Text("This example demonstrates right-aligned text, often used for dates, numbers, or aligning text to the right margin.")
  58. .AlignRight();
  59. column.Item()
  60. .Element(CellStyle)
  61. .Text("Justified text adjusts the spacing between words so that both the left and right edges of the text block are aligned, creating a clean, newspaper-like look.")
  62. .Justify();
  63. static IContainer CellStyle(IContainer container)
  64. => container.Background(Colors.Grey.Lighten3).Padding(10);
  65. });
  66. });
  67. })
  68. .GenerateImages(x => "text-paragraph-alignment.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.VeryHigh, RasterDpi = 144 });
  69. }
  70. [Test]
  71. public void FirstLineIndentation()
  72. {
  73. Document
  74. .Create(document =>
  75. {
  76. document.Page(page =>
  77. {
  78. page.MinSize(new PageSize(0, 0));
  79. page.MaxSize(new PageSize(500, 1200));
  80. page.DefaultTextStyle(x => x.FontSize(20));
  81. page.Margin(25);
  82. page.Content()
  83. .Text(Placeholders.Paragraphs())
  84. .ParagraphFirstLineIndentation(40);
  85. });
  86. })
  87. .GenerateImages(x => "text-paragraph-first-line-indentation.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.High, RasterDpi = 144 });
  88. }
  89. [Test]
  90. public void Spacing()
  91. {
  92. Document
  93. .Create(document =>
  94. {
  95. document.Page(page =>
  96. {
  97. page.MinSize(new PageSize(0, 0));
  98. page.MaxSize(new PageSize(500, 1200));
  99. page.DefaultTextStyle(x => x.FontSize(20));
  100. page.Margin(25);
  101. page.Content()
  102. .Text(Placeholders.Paragraphs())
  103. .ParagraphSpacing(10);
  104. });
  105. })
  106. .GenerateImages(x => "text-paragraph-spacing.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.High, RasterDpi = 144 });
  107. }
  108. [Test]
  109. public void ClampLines()
  110. {
  111. Document
  112. .Create(document =>
  113. {
  114. document.Page(page =>
  115. {
  116. page.MinSize(new PageSize(0, 0));
  117. page.MaxSize(new PageSize(600, 1000));
  118. page.DefaultTextStyle(x => x.FontSize(20));
  119. page.Margin(25);
  120. page.Content()
  121. .Column(column =>
  122. {
  123. column.Spacing(10);
  124. var paragraph = Placeholders.Paragraph();
  125. column.Item()
  126. .Background(Colors.Grey.Lighten3)
  127. .Padding(5)
  128. .Text(paragraph);
  129. column.Item()
  130. .Background(Colors.Grey.Lighten3)
  131. .Padding(5)
  132. .Text(paragraph)
  133. .ClampLines(3);
  134. });
  135. });
  136. })
  137. .GenerateImages(x => "text-paragraph-clamp-lines.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.Best, RasterDpi = 144 });
  138. }
  139. [Test]
  140. public void ClampLinesWithCustomEllipsis()
  141. {
  142. Document
  143. .Create(document =>
  144. {
  145. document.Page(page =>
  146. {
  147. page.MinSize(new PageSize(0, 0));
  148. page.MaxSize(new PageSize(600, 1000));
  149. page.DefaultTextStyle(x => x.FontSize(20));
  150. page.Margin(25);
  151. page.Content()
  152. .Text(Placeholders.Paragraph())
  153. .ClampLines(3, " [...]");
  154. });
  155. })
  156. .GenerateImages(x => "text-paragraph-clamp-lines-custom-ellipsis.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.Best, RasterDpi = 144 });
  157. }
  158. }