CodePatternConfigurableComponentExample.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using QuestPDF.Fluent;
  2. using QuestPDF.Helpers;
  3. using QuestPDF.Infrastructure;
  4. namespace QuestPDF.DocumentationExamples.CodePatterns;
  5. public class CodePatternConfigurableComponentExample
  6. {
  7. [Test]
  8. public void Example()
  9. {
  10. Document
  11. .Create(document =>
  12. {
  13. document.Page(page =>
  14. {
  15. page.MinSize(new PageSize(0, 0));
  16. page.MaxSize(new PageSize(600, 1200));
  17. page.DefaultTextStyle(x => x.FontSize(20));
  18. page.Margin(25);
  19. page.Content()
  20. .Component(BuildSampleSection());
  21. });
  22. })
  23. .GenerateImages(x => $"code-pattern-component-configurable.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.Best, RasterDpi = 144 });
  24. IComponent BuildSampleSection()
  25. {
  26. var section = new SectionComponent();
  27. section.Text("Product name", Placeholders.Label());
  28. section.Text("Description", Placeholders.Sentence());
  29. section.Text("Price", Placeholders.Price());
  30. section.Text("Date of production", Placeholders.ShortDate());
  31. section.Image("Photo of the product", "Resources/product.jpg");
  32. section.Custom("Status").Text("Accepted").FontColor(Colors.Green.Darken2).Bold();
  33. return section;
  34. }
  35. }
  36. public class SectionComponent : IComponent
  37. {
  38. private List<(string Label, IContainer Content)> Fields { get; set; } = [];
  39. public SectionComponent()
  40. {
  41. }
  42. public void Compose(IContainer container)
  43. {
  44. container
  45. .Border(1)
  46. .Column(column =>
  47. {
  48. foreach (var field in Fields)
  49. {
  50. column.Item().Row(row =>
  51. {
  52. row.RelativeItem()
  53. .Border(1)
  54. .BorderColor(Colors.Grey.Medium)
  55. .Background(Colors.Grey.Lighten3)
  56. .Padding(10)
  57. .Text(field.Label);
  58. row.RelativeItem(2)
  59. .Border(1)
  60. .BorderColor(Colors.Grey.Medium)
  61. .Padding(10)
  62. .Element(field.Content);
  63. });
  64. }
  65. });
  66. }
  67. public void Text(string label, string text)
  68. {
  69. Custom(label).Text(text);
  70. }
  71. public void Image(string label, string imagePath)
  72. {
  73. Custom(label).Image(imagePath);
  74. }
  75. public IContainer Custom(string label)
  76. {
  77. var content = EmptyContainer.Create();
  78. Fields.Add((label, content));
  79. return content;
  80. }
  81. }
  82. }