AddSvg.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Diagnostics;
  9. using GrapeCity.Documents.Word;
  10. using GrapeCity.Documents.Imaging;
  11.  
  12. namespace DsWordWeb.Demos
  13. {
  14. // This demo shows how to add an SVG image to am MS Word DOCX document.
  15. public class AddSvg
  16. {
  17. public GcWordDocument CreateDocx()
  18. {
  19. var doc = new GcWordDocument();
  20. var par = doc.Body.AddParagraph("The picture below is produced by a vector SVG image added to the document. " +
  21. "Mimicking the MS Word behavior, when an SVG is added, DsWord also creates a fallback raster version of the image.");
  22.  
  23. var pic = doc.Body.AddParagraph().AddRun().AddPicture();
  24. // Load picture data:
  25. var picBytes = File.ReadAllBytes(Path.Combine("Resources", "SVG", "Smiling-Girl.svg"));
  26. pic.ImageData.SetImage(picBytes, "image/svg+xml");
  27.  
  28. // In a debug configuration this shows that both vector and raster image versions are present:
  29. Debug.Assert(pic.ImageData.ImageBytes != null);
  30. Debug.Assert("image/png" == pic.ImageData.ContentType);
  31. Debug.Assert(pic.ImageData.ComplementaryVectorData != null);
  32. Debug.Assert(pic.ImageData.ComplementaryVectorData.ImageBytes.Length > 0);
  33. Debug.Assert("image/svg+xml" == pic.ImageData.ComplementaryVectorData.ContentType);
  34.  
  35. // Specify the picture size:
  36. pic.Size.Width.Value = 200;
  37. pic.Size.Height.Value = 400;
  38.  
  39. doc.Body.AddParagraph("The End.");
  40.  
  41. // Done:
  42. return doc;
  43. }
  44. }
  45. }
  46.