CodePatternExtesionMethodExample.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using QuestPDF.Fluent;
  2. using QuestPDF.Helpers;
  3. using QuestPDF.Infrastructure;
  4. namespace QuestPDF.DocumentationExamples.CodePatterns;
  5. public class CodePatternExtensionMethodExample
  6. {
  7. [Test]
  8. public void Example()
  9. {
  10. Document
  11. .Create(document =>
  12. {
  13. document.Page(page =>
  14. {
  15. page.MinSize(new PageSize(600, 0));
  16. page.MaxSize(new PageSize(600, 1000));
  17. page.DefaultTextStyle(x => x.FontSize(14));
  18. page.Margin(25);
  19. page.Content()
  20. .Border(1)
  21. .Table(table =>
  22. {
  23. table.ColumnsDefinition(columns =>
  24. {
  25. columns.RelativeColumn(2);
  26. columns.RelativeColumn(3);
  27. columns.RelativeColumn(2);
  28. columns.RelativeColumn(3);
  29. });
  30. table.Cell().TableLabelCell("Product name");
  31. table.Cell().TableValueCell().Text(Placeholders.Label());
  32. table.Cell().TableLabelCell("Description");
  33. table.Cell().TableValueCell().Text(Placeholders.Sentence());
  34. table.Cell().TableLabelCell("Price");
  35. table.Cell().TableValueCell().Text(Placeholders.Price());
  36. table.Cell().TableLabelCell("Date of production");
  37. table.Cell().TableValueCell().Text(Placeholders.ShortDate());
  38. table.Cell().ColumnSpan(2).TableLabelCell("Photo of the product");
  39. table.Cell().ColumnSpan(2).TableValueCell().AspectRatio(16 / 9f).Image(Placeholders.Image);
  40. });
  41. });
  42. })
  43. .GenerateImages(x => "code-pattern-extension-methods.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.VeryHigh, RasterDpi = 144 });
  44. }
  45. }
  46. public static class TableExtensions
  47. {
  48. private static IContainer TableCellStyle(this IContainer container, string backgroundColor)
  49. {
  50. return container
  51. .Border(1)
  52. .BorderColor(Colors.Black)
  53. .Background(backgroundColor)
  54. .Padding(10);
  55. }
  56. public static void TableLabelCell(this IContainer container, string text)
  57. {
  58. container
  59. .TableCellStyle(Colors.Grey.Lighten3)
  60. .Text(text)
  61. .Bold();
  62. }
  63. public static IContainer TableValueCell(this IContainer container)
  64. {
  65. return container.TableCellStyle(Colors.Transparent);
  66. }
  67. }