Using Adobe's SVG control 3 beta from .NET

Anyone interested in using Adobe's SVG control from within .NET WinForms can start with the example provided here.

Here is the C# code - changes for version 3.0 beta in bold (SvgHost.cs):

//------------------------------------------------------------------------------
// Hosting Adobe's SVG Viewer 3 beta control .. (c) MecXpert
//------------------------------------------------------------------------------
namespace MecXpert.Svg {
   using System;
   using System.IO;
   using System.Windows.Forms;
   using System.Reflection;
   using System.Runtime.InteropServices;
   using System.Threading;
   using SVGACTIVEXLib;

   class SvgHost : Form 
   {
      private AxSVGACTIVEXLib.AxSVGCtl  svgCtl = null;
      private string                    fileName = null;

      public SvgHost(string fname) 
      {
         fileName = fname;
         InitializeComponent();
      }

      private void InitializeComponent()
      {
         InitializeSvgControl();

         this.SetStyle(ControlStyles.ResizeRedraw, true);
         this.ClientSize = new System.Drawing.Size(svgCtl.Size.Width+20, 
                                                   svgCtl.Size.Height+30);
         this.Text = "C# SvgHost";
      }

      private void InitializeSvgControl()
      {
         svgCtl = new AxSVGACTIVEXLib.AxSVGCtl();

         this.svgCtl.BeginInit();
         this.svgCtl.Location = new System.Drawing.Point(10, 10);
         Controls.Add(this.svgCtl);
         this.svgCtl.EndInit();

         this.svgCtl.SRC = fileName;

         SVGDocument doc = new SVGDocument(this.svgCtl);
         IDOMElement  docElem = doc.documentElement;
         this.svgCtl.Size = new System.Drawing.Size(Convert.ToInt32(docElem.getAttribute("width")),
                                                    Convert.ToInt32(docElem.getAttribute("height")));
      }

      [STAThread]	
      public static int Main(String[] args) 
      {
         string filename;
         if (args.Length == 1)
         {
            filename = (Path.GetFullPath(args[0]));
            if (File.Exists(filename))
               Application.Run(new SvgHost("file://" + filename));
            else
               Console.Write("File '" + filename + "' does not exist!\n");
         }
         else
            Console.Write("Usage: SvgHost \n");  

         return 0;
      }
   }

   class SVGDocument
   {
      // -- data ----
      object doc = null;

      // -- methods ---
      public SVGDocument(AxSVGACTIVEXLib.AxSVGCtl ctl) { doc = (object)ctl.getSVGDocument(); }

      public IDOMElement documentElement
      {
         get { return new IDOMElement(doc.GetType().InvokeMember("getDocumentElement", BindingFlags.InvokeMethod, null, doc, null)); }
      }
   }

   class IDOMElement
   {
      // -- data ----
      object elem = null;

      // -- methods ---
      public IDOMElement(object o) { elem = o; }

      public string getAttribute(string name)
      {
         return (string)elem.GetType().InvokeMember("getAttribute", BindingFlags.InvokeMethod, null, elem, new object[] {name});
      }
   }
}

Follow these steps to get it up and running:


© 2000 MecXpert