SaveAsSvg.cs
// 完毕:
using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Collections.Generic;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Svg;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;

namespace DsPdfWeb.Demos.Basics
{
    // This sample shows how to use the SaveAsSvg() method to save a PDF
    // page as SVG (scalable vector graphics) image, and how to load
    // and render that image on a new PDF page.
    // The PDF used as the source was generated by the SvgSpecArt sample,
    // but any valid PDF can be used instead.
    public class SaveAsSvg
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var page = doc.NewPage();

            var rc = Common.Util.AddNote(
                "We load an existing PDF into a temporary GcPdfDocument, " +
                "save its first page as an SVG image to a stream, and then " +
                "draw that image on the page of the resulting PDF.",
                page);

            // Load a one page document into a temp PDF:
            using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "svg-spec-art.pdf"));
            var docSrc = new GcPdfDocument();
            docSrc.Load(fs);

            // Save the first page of the loaded PDF as SVG:
            using var svgStream = new MemoryStream();
            docSrc.Pages[0].SaveAsSvg(svgStream, new SaveAsImageOptions() { BackColor = Color.Transparent });
            // PDF pages can also be saved as SVGZ (compressed SVG) using the ToSvgz() method:
            // docSrc.Pages[0].ToSvgz();
            svgStream.Position = 0;
            // Create a GcSvgDocument from the saved SVG so that we can draw it in the resulting PDF:
            using var svgDoc = GcSvgDocument.FromStream(svgStream);

            // For illustration purposes we render the SVG with transparent background
            // on a rectangle with a custom fill and border:
            rc = new RectangleF(0, rc.Bottom, page.Size.Width, page.Size.Height - rc.Bottom);
            rc.Inflate(-4f, -4f);
            page.Graphics.DrawRectangle(rc, Color.DarkGoldenrod);
            page.Graphics.FillRectangle(rc, Color.PaleGoldenrod);
            page.Graphics.DrawSvg(svgDoc, rc);

            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}