TableExamples.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using NUnit.Framework;
  3. using QuestPDF.Examples.Engine;
  4. using QuestPDF.Fluent;
  5. using QuestPDF.Helpers;
  6. using QuestPDF.Infrastructure;
  7. namespace QuestPDF.Examples
  8. {
  9. public class TableExamples
  10. {
  11. public static Random Random { get; } = new Random(0);
  12. [Test]
  13. public void Example()
  14. {
  15. RenderingTest
  16. .Create()
  17. .ProduceImages()
  18. .PageSize(PageSizes.A4)
  19. .ShowResults()
  20. .Render(container =>
  21. {
  22. container
  23. .Padding(25)
  24. .Box()
  25. .Border(2)
  26. .MaxHeight(500)
  27. .Table(table =>
  28. {
  29. table.ColumnsDefinition(columns =>
  30. {
  31. columns.ConstantColumn(100);
  32. columns.RelativeColumn();
  33. columns.ConstantColumn(100);
  34. columns.ConstantColumn(200);
  35. });
  36. table.Cell().Row(1).Column(1).ColumnSpan(2).Element(CreateBox("A"));
  37. table.Cell().Row(1).Column(3).Element(CreateBox("B"));
  38. table.Cell().Row(1).Column(4).Element(CreateBox("C"));
  39. table.Cell().Row(2).Column(1).Element(CreateBox("D"));
  40. table.Cell().Row(2).RowSpan(2).Column(2).Element(CreateBox("E"));
  41. table.Cell().Row(2).RowSpan(3).Column(3).ColumnSpan(2).Element(CreateBox("F"));
  42. table.Cell().Row(3).RowSpan(2).Column(1).Element(CreateBox("G"));
  43. table.Cell().Row(4).RowSpan(2).Column(2).Element(CreateBox("H"));
  44. table.Cell().Row(5).Column(3).Element(CreateBox("I"));
  45. table.Cell().Row(5).Column(4).Element(CreateBox("J"));
  46. table.Cell().Row(5).RowSpan(2).Column(1).Element(CreateBox("K"));
  47. table.Cell().Row(6).Column(2).ColumnSpan(2).Element(CreateBox("L"));
  48. table.Cell().Row(6).Column(4).Element(CreateBox("M"));
  49. });
  50. });
  51. Action<IContainer> CreateBox(string label)
  52. {
  53. return container =>
  54. {
  55. var height = Random.Next(2, 7) * 25;
  56. container
  57. .Border(1)
  58. .Background(Placeholders.BackgroundColor())
  59. .AlignCenter()
  60. .AlignMiddle()
  61. .Border(1)
  62. .MinHeight(height)
  63. .Text($"{label}: {height}");
  64. };
  65. }
  66. }
  67. }
  68. }