ScaleSvg.cs
- //
- // このコードは、DioDocs for PDF のサンプルの一部として提供されています。
- // © MESCIUS inc. All rights reserved.
- //
- using System;
- using System.IO;
- using System.Drawing;
- using System.Linq;
- using System.Collections.Generic;
- using GrapeCity.Documents.Pdf;
- using GrapeCity.Documents.Text;
- using GrapeCity.Documents.Svg;
- using GCTEXT = GrapeCity.Documents.Text;
- using GCDRAW = GrapeCity.Documents.Drawing;
- using DsPdfWeb.Demos.Common;
-
- namespace DsPdfWeb.Demos
- {
- // このサンプルでは、SVG 画像の実際のコンテンツを測定し拡大縮小する方法を示しています。
- // 実際のコンテンツよりも固有サイズが大きく、かつコンテンツがビューポート内でオフセットされた
- // SVG 画像をサンプルとして使用します。
- //
- // なお RenderSvgContent サンプルでは、同じ SVG を使用して様々な SVG サイズによる違いを示しています。
- //
- // このサンプルで使用している SVG アートは、freesvg.org のものです。
- public class ScaleSvg
- {
- public int CreatePDF(Stream stream)
- {
- var svgPath = Path.Combine("Resources", "SvgMisc", "Smiling-Girl-offset.svg");
- using var svg = GcSvgDocument.FromFile(svgPath);
-
- var doc = new GcPdfDocument();
- var page = doc.NewPage();
- page.Landscape = true;
- var g = page.Graphics;
- var margin = g.Resolution / 4;
- // var s = new SizeF(page.Size.Width - margin * 2, page.Size.Height - margin * 2);
-
- // SVG 画像の実際のコンテンツを測定します。
- var contentRc = g.MeasureSvg(svg, PointF.Empty);
- // SVG コンテンツが縦方向に2回収まるようにします。
- var q = page.Size.Height / (contentRc.Height * 2.1f);
- contentRc.X *= q;
- contentRc.Y *= q;
- contentRc.Width *= q;
- contentRc.Height *= q;
-
- // 実際の SVG コンテンツをポイント (0,0) に配置します。
- var s = svg.GetIntrinsicSize(SvgLengthUnits.Points);
- s.Width *= q;
- s.Height *= q;
- // このポイントの矩形は、SVG のビューポートと同様のものです。
- // SVG コンテンツが縦方向に2回収まるように拡大され、
- // 実際の SVG コンテンツの左上隅が矩形の左上隅に
- // 位置するようになります。
- var rc = new RectangleF(-contentRc.X, -contentRc.Y, s.Width, s.Height);
-
- // パディングを設定します。
- const float pad = 12;
- rc.Offset(pad, pad);
-
- // 縮小します。(健全性のために反復回数を制限)
- const float qDown = 0.8f;
- var currRc = rc;
- var currContentRc = contentRc;
- while (currRc.X + currContentRc.Right < page.Size.Width && currContentRc.Height > 8)
- {
- g.DrawSvg(svg, currRc);
- // デバッグの場合、コンテンツのボーダーを描画します。
- // var trc = currContentRc;
- // trc.Offset(currRc.Location);
- // g.DrawRectangle(trc, Color.MediumPurple);
- // SVG コンテンツを縮小します。
- currRc.Height *= qDown;
- currRc.Width *= qDown;
- currRc.Y -= (currContentRc.Top * qDown - currContentRc.Top);
- currRc.X += currContentRc.Width + currContentRc.Left - currContentRc.Left * qDown;
- currContentRc.X *= qDown;
- currContentRc.Y *= qDown;
- currContentRc.Width *= qDown;
- currContentRc.Height *= qDown;
- }
- // 拡大します。
- const float qUp = 1.2f;
- currRc = rc;
- currContentRc = contentRc;
- currRc.Offset(0, page.Size.Height - contentRc.Height - pad * 2);
- while (currRc.X + currContentRc.Right < page.Size.Width)
- {
- g.DrawSvg(svg, currRc);
- // デバッグの場合、コンテンツのボーダーを描画します。
- // var trc = currContentRc;
- // trc.Offset(currRc.Location);
- // g.DrawRectangle(trc, Color.MediumPurple);
- // SVG コンテンツを拡大します。
- currRc.Height *= qUp;
- currRc.Width *= qUp;
- currRc.Y -= (currContentRc.Bottom * qUp - currContentRc.Bottom);
- currRc.X += currContentRc.Width + currContentRc.Left - currContentRc.Left * qUp;
- currContentRc.X *= qUp;
- currContentRc.Y *= qUp;
- currContentRc.Width *= qUp;
- currContentRc.Height *= qUp;
- }
- // PDF ドキュメントを保存します。
- doc.Save(stream);
- return doc.Pages.Count;
- }
- }
- }
-