RenderSvgContent.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 sample shows how to measure and render the actual SVG content
  20. // that sometimes is offset within the SVG viewport.
  21. // In the left part of the page, we render such an SVG as it is supposed
  22. // to be laid out within its viewport. A gray border is drawn around the viewport,
  23. // and a magenta border is around the SVG content.
  24. // In the right part of the page we locate just the content at a specific point
  25. // in the final image, and draw a purple border around it.
  26. //
  27. // This short sample shows how to measure and render an SVG image.
  28. // The SVG art used in this sample is from freesvg.org.
  29. public class RenderSvgContent
  30. {
  31. public int CreatePDF(Stream stream)
  32. {
  33. // Load the SVG:
  34. var svgPath = Path.Combine("Resources", "SvgMisc", "Smiling-Girl-offset.svg");
  35. using var svg = GcSvgDocument.FromFile(svgPath);
  36.  
  37. var doc = new GcPdfDocument();
  38. var page = doc.NewPage();
  39. page.Landscape = true;
  40. var g = page.Graphics;
  41. var margin = g.Resolution / 2;
  42. var pt0 = new PointF(margin, margin);
  43.  
  44. // Render the SVG content as specified by the SVG layout within viewport:
  45. var rcViewport = new RectangleF(pt0, svg.GetIntrinsicSize(SvgLengthUnits.Points));
  46. // Measure the content size/location relative to 'pt':
  47. var rcContent0 = g.MeasureSvg(svg, pt0);
  48. // Draw the SVG and viewport and content bounds:
  49. g.DrawSvg(svg, pt0);
  50. g.DrawRectangle(rcViewport, Color.DarkGray);
  51. g.DrawRectangle(rcContent0, Color.Magenta);
  52.  
  53. // In the left part of the bitmap, render just the SVG content
  54. // aligning its top left corner to a specific point on the bitmap
  55. // (center of page horizontally, 1" from the top):
  56. var pt1 = new PointF(page.Size.Width / 2, margin);
  57. // An alternative to GcGraphics.MeasureSvg() is GcSvgDocument.Measure(),
  58. // here we use it for illustration but these methods are calculations heavy,
  59. // so in a real app we would re-use the results from GcGraphics.MeasureSvg() above:
  60. var rcContent1 = svg.Measure(PointF.Empty, g.Resolution);
  61. g.DrawSvg(svg, new PointF(pt1.X - rcContent1.X, pt1.Y - rcContent1.Y));
  62. // Add a border:
  63. g.DrawRectangle(new RectangleF(pt1, rcContent1.Size), Color.Purple);
  64.  
  65. // Done:
  66. doc.Save(stream);
  67. return doc.Pages.Count;
  68. }
  69. }
  70. }
  71.