ScaleSvg.cs
  1. //
  2. // このコードは、DioDocs for PDF のサンプルの一部として提供されています。
  3. // © 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. // このサンプルでは、SVG 画像の実際のコンテンツを測定し拡大縮小する方法を示しています。
  20. // 実際のコンテンツよりも固有サイズが大きく、かつコンテンツがビューポート内でオフセットされた
  21. // SVG 画像をサンプルとして使用します。
  22. //
  23. // なお RenderSvgContent サンプルでは、同じ SVG を使用して様々な SVG サイズによる違いを示しています。
  24. //
  25. // このサンプルで使用している SVG アートは、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. // SVG 画像の実際のコンテンツを測定します。
  41. var contentRc = g.MeasureSvg(svg, PointF.Empty);
  42. // SVG コンテンツが縦方向に2回収まるようにします。
  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. // 実際の SVG コンテンツをポイント (0,0) に配置します。
  50. var s = svg.GetIntrinsicSize(SvgLengthUnits.Points);
  51. s.Width *= q;
  52. s.Height *= q;
  53. // このポイントの矩形は、SVG のビューポートと同様のものです。
  54. // SVG コンテンツが縦方向に2回収まるように拡大され、
  55. // 実際の SVG コンテンツの左上隅が矩形の左上隅に
  56. // 位置するようになります。
  57. var rc = new RectangleF(-contentRc.X, -contentRc.Y, s.Width, s.Height);
  58.  
  59. // パディングを設定します。
  60. const float pad = 12;
  61. rc.Offset(pad, pad);
  62.  
  63. // 縮小します。(健全性のために反復回数を制限)
  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. // デバッグの場合、コンテンツのボーダーを描画します。
  71. // var trc = currContentRc;
  72. // trc.Offset(currRc.Location);
  73. // g.DrawRectangle(trc, Color.MediumPurple);
  74. // SVG コンテンツを縮小します。
  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. // 拡大します。
  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. // デバッグの場合、コンテンツのボーダーを描画します。
  93. // var trc = currContentRc;
  94. // trc.Offset(currRc.Location);
  95. // g.DrawRectangle(trc, Color.MediumPurple);
  96. // SVG コンテンツを拡大します。
  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. // PDF ドキュメントを保存します。
  107. doc.Save(stream);
  108. return doc.Pages.Count;
  109. }
  110. }
  111. }
  112.