Home > Net >  Convert SVG to .vsdx MS Visio file
Convert SVG to .vsdx MS Visio file

Time:06-15

I have an application that produces SVG diagrams, we would like to create an export to Visio option that would, ideally, behind the scenes (via server-side C#) take an SVG file and convert it directly to an MS Visio .vsdx file that the user can download and then ofcourse open and edit in Visio seamlessly (as opposed to having to download the SVG file, and then importing it themselves into Visio). In effect, trying to replicate the "Open SVG" functionality that the Visio GUI supplies in backend C#.

I have seen a lot of answers on going from Visio TO SVG, but this is the opposite.

I also know that .vsdx is simply a zip file, but with that extension. Inside are a bunch of visio specific files and folders - if anyone knows what is the bare minimum required of these files/folders and their contents and where the SVG fits in, perhaps that's one way to do it.

Any guidance would be much appreciated.

CodePudding user response:

Dug this up, credit/source: https://programmer.group/5c650f3227420.html

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 });
            }
        }
    }
}
  • Related