TableExamples.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using QuestPDF.Fluent;
  2. using QuestPDF.Helpers;
  3. using QuestPDF.Infrastructure;
  4. namespace QuestPDF.DocumentationExamples;
  5. public class TableExamples
  6. {
  7. [Test]
  8. public void Basic()
  9. {
  10. Document
  11. .Create(document =>
  12. {
  13. document.Page(page =>
  14. {
  15. page.MinSize(new PageSize(0, 0));
  16. page.MaxSize(new PageSize(700, 1000));
  17. page.DefaultTextStyle(x => x.FontSize(20));
  18. page.Margin(25);
  19. page.Content()
  20. .Table(table =>
  21. {
  22. table.ColumnsDefinition(columns =>
  23. {
  24. columns.ConstantColumn(75);
  25. columns.ConstantColumn(150);
  26. columns.ConstantColumn(200);
  27. columns.ConstantColumn(200);
  28. });
  29. table.Cell().Row(1).Column(3).ColumnSpan(2)
  30. .Element(HeaderCellStyle).AlignCenter()
  31. .Text("Predicted condition").Bold();
  32. table.Cell().Row(3).Column(1).RowSpan(2)
  33. .Element(HeaderCellStyle).RotateLeft().AlignCenter().AlignMiddle()
  34. .Text("Actual\ncondition").Bold();
  35. table.Cell().Row(2).Column(3)
  36. .Element(HeaderCellStyle).AlignCenter()
  37. .Text("Positive (PP)");
  38. table.Cell().Row(2).Column(4)
  39. .Element(HeaderCellStyle).AlignCenter()
  40. .Text("Negative (PN)");
  41. table.Cell().Row(3).Column(2)
  42. .Element(HeaderCellStyle).AlignMiddle().Text("Positive (P)");
  43. table.Cell().Row(4).Column(2)
  44. .Element(HeaderCellStyle).AlignMiddle()
  45. .Text("Negative (N)");
  46. table.Cell()
  47. .Row(3).Column(3).Element(GoodCellStyle)
  48. .Text("True positive (TP)");
  49. table.Cell()
  50. .Row(3).Column(4).Element(BadCellStyle)
  51. .Text("False negative (FN)");
  52. table.Cell().Row(4).Column(3)
  53. .Element(BadCellStyle).Text("False positive (FP)");
  54. table.Cell().Row(4).Column(4)
  55. .Element(GoodCellStyle).Text("True negative (TN)");
  56. static IContainer CellStyle(IContainer container, Color color)
  57. => container.Border(1).Background(color).PaddingHorizontal(10).PaddingVertical(15);
  58. static IContainer HeaderCellStyle(IContainer container)
  59. => CellStyle(container, Colors.Grey.Lighten3);
  60. static IContainer GoodCellStyle(IContainer container)
  61. => CellStyle(container, Colors.Green.Lighten4).DefaultTextStyle(x => x.FontColor(Colors.Green.Darken2));
  62. static IContainer BadCellStyle(IContainer container)
  63. => CellStyle(container, Colors.Red.Lighten4).DefaultTextStyle(x => x.FontColor(Colors.Red.Darken2));
  64. });
  65. });
  66. })
  67. .GenerateImages(x => "table-basic.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.VeryHigh, RasterDpi = 144 });
  68. }
  69. [Test]
  70. public void ColumnsDefinition()
  71. {
  72. Document
  73. .Create(document =>
  74. {
  75. document.Page(page =>
  76. {
  77. page.MinSize(new PageSize(0, 0));
  78. page.MaxSize(new PageSize(700, 1000));
  79. page.DefaultTextStyle(x => x.FontSize(20));
  80. page.Margin(25);
  81. page.Content()
  82. .Width(325)
  83. .Table(table =>
  84. {
  85. table.ColumnsDefinition(columns =>
  86. {
  87. columns.ConstantColumn(100);
  88. columns.RelativeColumn();
  89. columns.RelativeColumn(2);
  90. });
  91. table.Cell().ColumnSpan(3)
  92. .Background(Colors.Grey.Lighten2).Element(CellStyle)
  93. .Text("Total width: 325px");
  94. table.Cell().Element(CellStyle).Text("C: 100");
  95. table.Cell().Element(CellStyle).Text("R: 1");
  96. table.Cell().Element(CellStyle).Text("R: 2");
  97. table.Cell().Element(CellStyle).Text("100px");
  98. table.Cell().Element(CellStyle).Text("75px");
  99. table.Cell().Element(CellStyle).Text("150px");
  100. static IContainer CellStyle(IContainer container)
  101. => container.Border(1).Padding(10);
  102. });
  103. });
  104. })
  105. .GenerateImages(x => "table-columns-definition.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.VeryHigh, RasterDpi = 144 });
  106. }
  107. }