SVG format to Visio vsd format method, with C# dynamic call Office Com component method

Keywords: C# svn

The SVG format can be displayed directly on the web page to implement functions such as statistical Chart charts, flow charts and organization charts. In order to make the image downloadable and easy to modify, SVG can be converted to Visio's vsd format. The method is very simple. The main method is to open the SVG file and save it as a vsd file. The invocation method is as follows:

        /// <summary>
        /// svg turn vsd
        /// </summary>
        /// <param name="svgFn">svn file name</param>
        /// <param name="desVsdFn">Preserved vsd file name</param>
        private static void Svg2Vsd(string svgFn, string desVsdFn)
        {
            var app = ComObj.Create("Visio.Application");
            app["Visible"] = new ComObj(false);
            var docs = app["Documents"];
            short visOpenHidden = 64, visOpenRO = 2;
            var doc = docs.Call("OpenEx", svgFn, visOpenHidden + visOpenRO);
            doc.Call("SaveAs", desVsdFn);
            doc.Call("Close");

            var win = app["Window"];
            app.Call("Quit");
        }

Here I use a ComObj class I wrote myself. Its purpose is to make it easy to invoke Com components such as Office by reflection, and to make the code concise when invoking.

Why use reflection to invoke dynamically instead of directly referencing Com components? The main purpose is to reduce the dependence and coupling of program code to COM components, so as to facilitate the compilation and distribution of code deployment. Dynamic invocation can be compiled and run without adding component references. If the Com component is not installed on the server, you can also give an intuitive prompt instead of a program error.

The code for this class is as follows:

using System;
using System.Reflection;

namespace HZ.Common
{
    /// <summary>
    /// For convenience Com Object attributes, method calls
    /// </summary>
    public class ComObj
    {
        public static ComObj Create(string progId)
        {
            var type = Type.GetTypeFromProgID(progId);
            if (type == null)
            {
                throw new Exception("Servers need to be installed" + progId + "To use this feature");
            }
            return new ComObj(Activator.CreateInstance(type));
        }

        private object _val;
        /// <summary>
        /// Actual value
        /// </summary>
        public object Val
        {
            get { return _val; }
        }
        public ComObj(object comObject)
        {
            _val = comObject;
        }

        public ComObj Call(string mehtod, params object[] args)
        {
            if (_val == null)
                return null;
            var ret = _val.GetType().InvokeMember(mehtod, BindingFlags.InvokeMethod, null, _val, args);
            return new ComObj(ret);
        }
        public ComObj this[string property]
        {
            get
            {
                if (_val == null)
                    return null;
                var ret = _val.GetType().InvokeMember(property, BindingFlags.GetProperty, null, _val, null);
                return new ComObj(ret);
            }
            set
            {
                if (_val != null)
                    _val.GetType().InvokeMember(property, BindingFlags.SetProperty, null, _val, new object[] { value.Val });
            }
        }
    }
}

Posted by jmarco on Wed, 13 Feb 2019 22:48:18 -0800