SvgImageExample.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using NUnit.Framework;
  2. using QuestPDF.Examples.Engine;
  3. using QuestPDF.Fluent;
  4. using QuestPDF.Helpers;
  5. using QuestPDF.Infrastructure;
  6. using SkiaSharp;
  7. using Svg.Skia;
  8. namespace QuestPDF.Examples
  9. {
  10. public class SvgImageExample
  11. {
  12. [Test]
  13. public void ImageSVG()
  14. {
  15. RenderingTest
  16. .Create()
  17. .PageSize(new PageSize(75f, 92f, Unit.Millimetre))
  18. .ProducePdf()
  19. .ShowResults()
  20. .Render(container =>
  21. {
  22. container.Svg(SvgImage.FromFile("pdf-icon.svg"));
  23. });
  24. }
  25. [Test]
  26. public void SupportForDifferentUnits()
  27. {
  28. RenderingTest
  29. .Create()
  30. .PageSize(PageSizes.A4)
  31. .ProducePdf()
  32. .ShowResults()
  33. .Render(container =>
  34. {
  35. container.Padding(20).Column(column =>
  36. {
  37. column.Spacing(20);
  38. var sizes = new[]
  39. {
  40. ("200", "100"),
  41. ("200px", "100px"),
  42. ("200pt", "100pt"),
  43. ("200cm", "100cm"),
  44. ("200mm", "100mm"),
  45. ("200in", "100in"),
  46. ("200pc", "100pc"),
  47. ("100%", "100%")
  48. };
  49. foreach (var size in sizes)
  50. {
  51. column.Item().Row(row =>
  52. {
  53. // normal SVG
  54. row.RelativeItem()
  55. .Width(200)
  56. .Height(100)
  57. .Background(Colors.Grey.Lighten2)
  58. .Svg(CreateSvg(size.Item1, size.Item2));
  59. // dynamic SVG
  60. row.RelativeItem()
  61. .Width(200)
  62. .Height(100)
  63. .Background(Colors.Grey.Lighten2)
  64. .Svg(_ => CreateSvg(size.Item1, size.Item2));
  65. });
  66. }
  67. });
  68. });
  69. string CreateSvg(string width, string height)
  70. {
  71. var svg =
  72. """
  73. <svg width="{width}" height="{height}" viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
  74. <ellipse cx="100" cy="50" rx="100" ry="50" style="fill:#2196F3;stroke:#3F51B5;stroke-width:3" />
  75. </svg>
  76. """;
  77. return svg
  78. .Replace("{width}", width)
  79. .Replace("{height}", height);
  80. }
  81. }
  82. }
  83. }