Example #1
0
        private string CreateSVG(int printableHeight, int printableWidth, IEnumerable <ImgInfo> listSort)
        {
            string svg = "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' height='" + printableHeight + "' width='" + printableWidth + "'>";

            svg = svg + "<defs><style type='text/css'><![CDATA[";

            foreach (var item in listSort)
            {
                if (item.FontFamily != "")
                {  //font-style: normal; font-weight: 400;
                    svg = svg + "@font-face{font-family:'" + item.FontFamily + "';src: local('" + item.FontFamily + "'), local('" + item.FontFamily + "'), url('data:application/font-woff;charset=utf-8;base64,";

                    var fontName = _fontService.GetFontByFamily(item.FontFamily);

                    var imageFilePath = System.Web.Hosting.HostingEnvironment.MapPath("/Modules/Teeyoot.Module/Content/fonts/" + fontName.FileName + "-webfont.woff");

                    if (System.IO.File.Exists(imageFilePath))
                    {
                        Byte[] bytes = System.IO.File.ReadAllBytes(imageFilePath);
                        String file  = Convert.ToBase64String(bytes);
                        svg = svg + file;
                    }
                    svg = svg + "') format('woff');}";
                }
            }
            svg = svg + "]]></style></defs>";

            foreach (var item in listSort)
            {
                var top  = item.Top.Replace("px", "");
                var left = item.Left.Replace("px", "");

                if (item.FileStandart != "")
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(item.Svg);
                    var root = doc.FirstChild;

                    if (root.Attributes.Count > 0)
                    {
                        if (root.Attributes["x"] != null)
                        {
                            root.Attributes.Remove(root.Attributes["x"]);
                        }
                        if (root.Attributes["y"] != null)
                        {
                            root.Attributes.Remove(root.Attributes["y"]);
                        }
                    }
                    item.Svg = doc.OuterXml;
                }

                if (item.Rotate != 0)
                {
                    int width  = int.Parse(item.Width.Replace("px", "")) / 2;
                    int height = int.Parse(item.Height.Replace("px", "")) / 2;

                    svg = svg + "<g transform=\"translate(" + left + ", " + top + ")  rotate(" + item.Rotate + " , " + width + " , " + height + ")\">" + item.Svg + "</g>";
                }
                else
                {
                    svg = svg + item.Svg.Replace("<svg ", "<svg y='" + top + "' x='" + left + "' ");
                }
            }

            svg = svg + "</svg>";
            svg = svg.Replace("<?xml version='1.0'?>", "");

            return(svg);
        }