SvgImage.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.IO;
  3. using QuestPDF.Drawing;
  4. using QuestPDF.Drawing.Exceptions;
  5. using QuestPDF.Skia;
  6. namespace QuestPDF.Infrastructure;
  7. /// <summary>
  8. /// Caches the SVG image in local memory for efficient reuse.
  9. /// </summary>
  10. /// <remarks>
  11. /// This class is thread safe.
  12. /// </remarks>
  13. public class SvgImage
  14. {
  15. internal SkSvgImage SkSvgImage { get; }
  16. private SvgImage(string content)
  17. {
  18. SkSvgImage = new SkSvgImage(content, SkResourceProvider.CurrentResourceProvider, FontManager.CurrentFontManager);
  19. }
  20. ~SvgImage()
  21. {
  22. SkSvgImage?.Dispose();
  23. }
  24. /// <summary>
  25. /// Loads the SVG image from a file with specified path.
  26. /// <a href="https://www.questpdf.com/api-reference/image.html">Learn more</a>
  27. /// </summary>
  28. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="image.remarks"]/*' />
  29. public static SvgImage FromFile(string filePath)
  30. {
  31. if (!File.Exists(filePath))
  32. throw new DocumentComposeException($"Cannot load provided image, file not found: ${filePath}");
  33. var svg = File.ReadAllText(filePath);
  34. return new SvgImage(svg);
  35. }
  36. /// <summary>
  37. /// Loads the SVG image from a stream.
  38. /// <a href="https://www.questpdf.com/api-reference/image.html">Learn more</a>
  39. /// </summary>
  40. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="image.remarks"]/*' />
  41. public static SvgImage FromText(string svg)
  42. {
  43. return new SvgImage(svg);
  44. }
  45. }