SvgSpecArt.cs
//
// This code is part of GrapeCity Documents for PDF samples.
// Copyright (c) GrapeCity, 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 GcPdfWeb.Samples.Common;

namespace GcPdfWeb.Samples
{
    // Use GcSvgDocument to render a few images used as illustrations
    // in the SVG spec.
    public class SvgSpecArt
    {
        public int CreatePDF(Stream stream)
        {
            // Load images from resources:
            var fnames = new List<string>() { "dash-path.svg", "opacity.svg", "paths.svg", "shadowstyle.svg", "units.svg", };
            var images = new List<(string, GcSvgDocument)>();
            foreach (var f in fnames)
                images.Add((f, GcSvgDocument.FromFile(Path.Combine("Resources", "SvgSpecArt", f))));

            const float margin = 36;
            const float gapy = 18;
            var ip = new PointF(margin, margin);

            // Render the images:
            var doc = new GcPdfDocument();
            var g = doc.NewPage().Graphics;
            for (int i = 0; i < images.Count(); ++i)
            {
                var svg = images[i].Item2;
                var s = svg.GetIntrinsicSize(SvgLengthUnits.Points);
                g.DrawSvg(svg, ip);
                ip.Y += s.Height + gapy;
            }
            // Done:
            doc.Save(stream);
            // Dispose images after saving the PDF:
            images.ForEach(t_ => t_.Item2.Dispose());
            return doc.Pages.Count;
        }
    }
}