SVG format support XSL-FO -.Net

I need to display svg in my XSL fo in C # .Net, which is available at https://fonet.codeplex.com/ . I tried using svg in xsl-fo but it does not display pdf and fails.

If someone has found a solution to this problem, please help.

I need my pdf report to support svg content.

+4
source share
1 answer

Use the code below to add hander images with svg extension

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

Add 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 the correct image if the URL is valid or passed false so that the image does not display. keeping the code more reliable.

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;
                }
            }
        }
+4

Source: https://habr.com/ru/post/1627400/


All Articles