RoundedCornersExample.cs 3.1 KB

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