XSL-FO SVG Format support - .Net

谁说胖子不能爱 提交于 2019-12-07 09:23:36

问题


I need to render svg in my XSL fo in c#.Net which is available in https://fonet.codeplex.com/. I tried to use svg in the xsl-fo but it does not render any pdf and fails silently.

If anybody has found a solution for this issue please help.

I need my pdf report to support svg contents.


回答1:


Use the below code to add Hander of an image incase of svg extensions

 FonetDriver fonetDriver = FonetDriver.Make();
 fonetDriver.ImageHandler = SvgImageHandler;

Add the SvgImageHandler Hander

 private static byte[] SvgImageHandler(string svgContent)
        {
            if (svgContent.Contains("http://www.w3.org/2000/svg"))
            {
                var svgByteAry = Encoding.UTF8.GetBytes(svgContent);
                using (var stream = new MemoryStream(svgByteAry))
                {
                    var svgDocument = SvgDocument.Open<SvgDocument>(stream);
                    using (var memoryStream = new MemoryStream())
                    {
                        svgDocument.Draw()
                                   .Save(memoryStream, ImageFormat.Png);
                        var byteArray = memoryStream.ToArray();
                        return byteArray;
                    }
                }
            }
            //Skip if not url based image
            if (!Uri.IsWellFormedUriString(svgContent, UriKind.RelativeOrAbsolute))
                return null;

            if (!ValidateUrlImage(svgContent))
            {
                ICacheService cacheService = new HttpCache();
                return cacheService.Get(Constants.NoImage,
                                        () =>
                                        {
                                            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings[Constants.ImagePath];
                                            var defaultUrl = Path.Combine(baseDirectory, Constants.NoImageFile);
                                            var img = Image.FromFile(defaultUrl);
                                            var imgCon = new ImageConverter();
                                            return (byte[])imgCon.ConvertTo(img, typeof(byte[]));
                                        });
            }
            return null;
        }

Return proper image if the url is valid or pass false so the No Image can be rendered. keeping the code more robust.

private static bool ValidateUrlImage(string absoluteUrl)
        {
            Uri uri;
            if (!Uri.TryCreate(absoluteUrl, UriKind.Absolute, out uri))
            {
                return true;
            }
            using (var client = new WebClient())
            {
                try
                {
                    using (var stream = client.OpenRead(uri))
                    {
                        Image.FromStream(stream);
                        return true;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }


来源:https://stackoverflow.com/questions/35218571/xsl-fo-svg-format-support-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!