SvgImageExample.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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
  52. .Item()
  53. .Width(200)
  54. .Height(100)
  55. .Background(Colors.Grey.Lighten2)
  56. .Svg(CreateSvg(size.Item1, size.Item2));
  57. }
  58. });
  59. });
  60. string CreateSvg(string width, string height)
  61. {
  62. var svg =
  63. """
  64. <svg width="{width}" height="{height}" viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
  65. <ellipse cx="100" cy="50" rx="100" ry="50" style="fill:#2196F3;stroke:#3F51B5;stroke-width:3" />
  66. </svg>
  67. """;
  68. return svg
  69. .Replace("{width}", width)
  70. .Replace("{height}", height);
  71. }
  72. }
  73. }
  74. }