ScaleSvg.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 scale the actual content of an SVG image.
  20. // It uses a sample SVG with intrinsic size larger than the actual content,
  21. // and content that is offset within the viewport.
  22. //
  23. // The same SVG is used to illustrate the difference between various SVG sizes in RenderSvgContent sample.
  24. //
  25. // The SVG art used in this sample is from freesvg.org.
  26. public class ScaleSvg
  27. {
  28. public int CreatePDF(Stream stream)
  29. {
  30. var svgPath = Path.Combine("Resources", "SvgMisc", "Smiling-Girl-offset.svg");
  31. using var svg = GcSvgDocument.FromFile(svgPath);
  32.  
  33. var doc = new GcPdfDocument();
  34. var page = doc.NewPage();
  35. page.Landscape = true;
  36. var g = page.Graphics;
  37. var margin = g.Resolution / 4;
  38. // var s = new SizeF(page.Size.Width - margin * 2, page.Size.Height - margin * 2);
  39.  
  40. // Get the actual SVG image content bounds:
  41. var contentRc = g.MeasureSvg(svg, PointF.Empty);
  42. // Make sure the SVG content fits height-wise twice:
  43. var q = page.Size.Height / (contentRc.Height * 2.1f);
  44. contentRc.X *= q;
  45. contentRc.Y *= q;
  46. contentRc.Width *= q;
  47. contentRc.Height *= q;
  48.  
  49. // Align the actual SVG content to point (0,0):
  50. var s = svg.GetIntrinsicSize(SvgLengthUnits.Points);
  51. s.Width *= q;
  52. s.Height *= q;
  53. // This rectangle (in points) is similar to the SVG view port.
  54. // It is also scaled so that the SVG content fits twice heightwise,
  55. // and the actual SVG content's top left corner is at the top left corner
  56. // of the rectangle:
  57. var rc = new RectangleF(-contentRc.X, -contentRc.Y, s.Width, s.Height);
  58.  
  59. // Set up padding:
  60. const float pad = 12;
  61. rc.Offset(pad, pad);
  62.  
  63. // Scale down, limit number of iterations for sanity:
  64. const float qDown = 0.8f;
  65. var currRc = rc;
  66. var currContentRc = contentRc;
  67. while (currRc.X + currContentRc.Right < page.Size.Width && currContentRc.Height > 8)
  68. {
  69. g.DrawSvg(svg, currRc);
  70. // Draw content bounds if debugging:
  71. // var trc = currContentRc;
  72. // trc.Offset(currRc.Location);
  73. // g.DrawRectangle(trc, Color.MediumPurple);
  74. // Scale SVG content down:
  75. currRc.Height *= qDown;
  76. currRc.Width *= qDown;
  77. currRc.Y -= (currContentRc.Top * qDown - currContentRc.Top);
  78. currRc.X += currContentRc.Width + currContentRc.Left - currContentRc.Left * qDown;
  79. currContentRc.X *= qDown;
  80. currContentRc.Y *= qDown;
  81. currContentRc.Width *= qDown;
  82. currContentRc.Height *= qDown;
  83. }
  84. // Scale up:
  85. const float qUp = 1.2f;
  86. currRc = rc;
  87. currContentRc = contentRc;
  88. currRc.Offset(0, page.Size.Height - contentRc.Height - pad * 2);
  89. while (currRc.X + currContentRc.Right < page.Size.Width)
  90. {
  91. g.DrawSvg(svg, currRc);
  92. // Draw content bounds if debugging:
  93. // var trc = currContentRc;
  94. // trc.Offset(currRc.Location);
  95. // g.DrawRectangle(trc, Color.MediumPurple);
  96. // Scale SVG content up:
  97. currRc.Height *= qUp;
  98. currRc.Width *= qUp;
  99. currRc.Y -= (currContentRc.Bottom * qUp - currContentRc.Bottom);
  100. currRc.X += currContentRc.Width + currContentRc.Left - currContentRc.Left * qUp;
  101. currContentRc.X *= qUp;
  102. currContentRc.Y *= qUp;
  103. currContentRc.Width *= qUp;
  104. currContentRc.Height *= qUp;
  105. }
  106. // Done:
  107. doc.Save(stream);
  108. return doc.Pages.Count;
  109. }
  110. }
  111. }
  112.