SvgImageExample.cs 3.0 KB

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