ConstrainedExtensions.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using QuestPDF.Elements;
  2. using QuestPDF.Infrastructure;
  3. namespace QuestPDF.Fluent
  4. {
  5. public static class ConstrainedExtensions
  6. {
  7. #region Width
  8. private static IContainer ConstrainedWidth(this IContainer element, float? min = null, float? max = null)
  9. {
  10. var constrained = element as Constrained ?? new Constrained();
  11. if (min.HasValue)
  12. constrained.MinWidth = min;
  13. if (max.HasValue)
  14. constrained.MaxWidth = max;
  15. return element.Element(constrained);
  16. }
  17. /// <summary>
  18. /// Sets the exact width of its content.
  19. /// <a href="https://www.questpdf.com/api-reference/width.html">Learn more</a>
  20. /// </summary>
  21. /// <returns>The container with the specified exact width.</returns>
  22. public static IContainer Width(this IContainer element, float value, Unit unit = Unit.Point)
  23. {
  24. value = value.ToPoints(unit);
  25. return element.ConstrainedWidth(min: value, max: value);
  26. }
  27. /// <summary>
  28. /// Sets the minimum width of its content.
  29. /// <a href="https://www.questpdf.com/api-reference/width.html">Learn more</a>
  30. /// </summary>
  31. /// <returns>The container with the specified minimum width.</returns>
  32. public static IContainer MinWidth(this IContainer element, float value, Unit unit = Unit.Point)
  33. {
  34. value = value.ToPoints(unit);
  35. return element.ConstrainedWidth(min: value);
  36. }
  37. /// <summary>
  38. /// Sets the maximum width of its content.
  39. /// <a href="https://www.questpdf.com/api-reference/width.html">Learn more</a>
  40. /// </summary>
  41. /// <returns>The container with the specified maximum width.</returns>
  42. public static IContainer MaxWidth(this IContainer element, float value, Unit unit = Unit.Point)
  43. {
  44. value = value.ToPoints(unit);
  45. return element.ConstrainedWidth(max: value);
  46. }
  47. #endregion
  48. #region Height
  49. private static IContainer ConstrainedHeight(this IContainer element, float? min = null, float? max = null)
  50. {
  51. var constrained = element as Constrained ?? new Constrained();
  52. if (min.HasValue)
  53. constrained.MinHeight = min;
  54. if (max.HasValue)
  55. constrained.MaxHeight = max;
  56. return element.Element(constrained);
  57. }
  58. /// <summary>
  59. /// Sets the exact height of its content.
  60. /// <a href="https://www.questpdf.com/api-reference/height.html">Learn more</a>
  61. /// </summary>
  62. /// <returns>The container with the specified exact height.</returns>
  63. public static IContainer Height(this IContainer element, float value, Unit unit = Unit.Point)
  64. {
  65. value = value.ToPoints(unit);
  66. return element.ConstrainedHeight(min: value, max: value);
  67. }
  68. /// <summary>
  69. /// Sets the minimum height of its content.
  70. /// <a href="https://www.questpdf.com/api-reference/height.html">Learn more</a>
  71. /// </summary>
  72. /// <returns>The container with the specified minimum height.</returns>
  73. public static IContainer MinHeight(this IContainer element, float value, Unit unit = Unit.Point)
  74. {
  75. value = value.ToPoints(unit);
  76. return element.ConstrainedHeight(min: value);
  77. }
  78. /// <summary>
  79. /// Sets the maximum height of its content.
  80. /// <a href="https://www.questpdf.com/api-reference/height.html">Learn more</a>
  81. /// </summary>
  82. /// <returns>The container with the specified maximum height.</returns>
  83. public static IContainer MaxHeight(this IContainer element, float value, Unit unit = Unit.Point)
  84. {
  85. value = value.ToPoints(unit);
  86. return element.ConstrainedHeight(max: value);
  87. }
  88. #endregion
  89. internal static IContainer EnforceSizeWhenEmpty(this IContainer element)
  90. {
  91. (element as Constrained).EnforceSizeWhenEmpty = true;
  92. return element;
  93. }
  94. }
  95. }