RenderSvg.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Collections.Generic;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Svg;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15. using DsPdfWeb.Demos.Common;
  16.  
  17. namespace DsPdfWeb.Demos
  18. {
  19. // This short sample shows how to measure and render an SVG image.
  20. // Note that when an SVG is rendered on GcPdfGraphics, it is NOT rasterized,
  21. // so the image quality is preserved if the SVG is scaled up or down.
  22. //
  23. // The SVG art used in this sample is from freesvg.org.
  24. public class RenderSvg
  25. {
  26. public int CreatePDF(Stream stream)
  27. {
  28. // Load the SVG:
  29. var svgPath = Path.Combine("Resources", "SvgClipArt", "Smiling-Girl.svg");
  30. using var svg = GcSvgDocument.FromFile(svgPath);
  31.  
  32. // Measure and render the image:
  33. var doc = new GcPdfDocument();
  34. var g = doc.NewPage().Graphics;
  35. var pt = new PointF(g.Resolution, g.Resolution);
  36. var rc = new RectangleF(pt, svg.GetIntrinsicSize(SvgLengthUnits.Points));
  37. // Draw the SVG and a border around it:
  38. g.DrawSvg(svg, pt);
  39. g.DrawRectangle(rc, Color.DarkGray);
  40.  
  41. // Done:
  42. doc.Save(stream);
  43. return doc.Pages.Count;
  44. }
  45. }
  46. }
  47.