RoundedCornersExample.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using NUnit.Framework;
  3. using QuestPDF.Fluent;
  4. using QuestPDF.Helpers;
  5. using QuestPDF.Infrastructure;
  6. namespace QuestPDF.Examples;
  7. public class RoundedCornersExample
  8. {
  9. [Test]
  10. public void ItemTypes()
  11. {
  12. Document
  13. .Create(document =>
  14. {
  15. document.Page(page =>
  16. {
  17. var roundedRectangle = new RoundedRectangleParameters
  18. {
  19. TopLeftRadius = 5,
  20. TopRightRadius = 10,
  21. BottomRightRadius = 15,
  22. BottomLeftRadius = 20,
  23. FillColor = Colors.Blue.Lighten3
  24. };
  25. page.Content()
  26. .Padding(10)
  27. .Shrink()
  28. .Layers(layers =>
  29. {
  30. layers.Layer().Svg(roundedRectangle.GenerateSVG);
  31. layers.PrimaryLayer().Padding(10).Text(Placeholders.Sentence());
  32. });
  33. });
  34. })
  35. .GeneratePdfAndShow();
  36. }
  37. }
  38. public class RoundedRectangleParameters
  39. {
  40. public double TopLeftRadius { get; set; }
  41. public double TopRightRadius { get; set; }
  42. public double BottomLeftRadius { get; set; }
  43. public double BottomRightRadius { get; set; }
  44. public string FillColor { get; set; }
  45. public string GenerateSVG(Size size)
  46. {
  47. var maxAllowedRadiusX = size.Width / 2.0;
  48. var maxAllowedRadiusY = size.Height / 2.0;
  49. TopLeftRadius = Math.Min(TopLeftRadius, Math.Min(maxAllowedRadiusX, maxAllowedRadiusY));
  50. TopRightRadius = Math.Min(TopRightRadius, Math.Min(maxAllowedRadiusX, maxAllowedRadiusY));
  51. BottomRightRadius = Math.Min(BottomRightRadius, Math.Min(maxAllowedRadiusX, maxAllowedRadiusY));
  52. BottomLeftRadius = Math.Min(BottomLeftRadius, Math.Min(maxAllowedRadiusX, maxAllowedRadiusY));
  53. var pathData = string.Format(System.Globalization.CultureInfo.InvariantCulture,
  54. "M {0},0 " +
  55. "H {1} " +
  56. "A {2},{2} 0 0 1 {3},{4} " +
  57. "V {5} " +
  58. "A {6},{6} 0 0 1 {7},{8} " +
  59. "H {9} " +
  60. "A {10},{10} 0 0 1 {11},{12} " +
  61. "V {13} " +
  62. "A {14},{14} 0 0 1 {15},0 Z",
  63. TopLeftRadius,
  64. size.Width - TopRightRadius,
  65. TopRightRadius, size.Width, TopRightRadius,
  66. size.Height - BottomRightRadius,
  67. BottomRightRadius, size.Width - BottomRightRadius, size.Height,
  68. BottomLeftRadius,
  69. BottomLeftRadius, 0, size.Height - BottomLeftRadius,
  70. TopLeftRadius,
  71. TopLeftRadius, TopLeftRadius);
  72. return string.Format(System.Globalization.CultureInfo.InvariantCulture,
  73. "<svg size.Width=\"{0}\" size.Height=\"{1}\" xmlns=\"http://www.w3.org/2000/svg\" " +
  74. "viewBox=\"0 0 {0} {1}\">" +
  75. "<path d=\"{2}\" fill=\"{3}\" />" +
  76. "</svg>",
  77. size.Width, size.Height,
  78. pathData,
  79. FillColor);
  80. }
  81. }