Remove empty spaces around icon of svg and drwa it on pdf in c#

Viewed 24

As per the following code i was getting HTML elements. With help of this code, i was trying to remove the empty spaces around the icon.

   public Image DownloadImage(string fromUrl)
    {
        HtmlToImage htmlToImageConverter = new HtmlToImage();

        XDocument document = XDocument.Load(fromUrl);
        XElement svg_Element = document.Root;
        var viewBoxValues = svg_Element.Attribute("viewBox").Value.ToString().Split(' ').Select(int.Parse).ToArray();

        var svgString = svg_Element.ToString();

        if (viewBoxValues[2] > viewBoxValues[3])
        {
            svgString = svgString.Insert(5, "style=\"height:100%\"");
        }
        else
        {
            svgString = svgString.Insert(5, "style=\"width:100%\"");
        }


        StringBuilder htmlString = new StringBuilder();
        htmlString.Append(@"<html> <body style=""margin:0px;background-color:#cccbbd;"">");
        htmlString.Append(svgString);
        htmlString.Append(@"<script>");
        htmlString.Append(@"var svg = document.getElementsByTagName(""svg"")[0];");
        htmlString.Append(@"var bbox = svg.getBBox();");
        htmlString.Append(@"var viewBox = [bbox.x, bbox.y, bbox.width, bbox.height].join("" "");");
        htmlString.Append(@"svg.setAttribute(""viewBox"", viewBox);");
        htmlString.Append(@"</script>");
        htmlString.Append(@"</body></html>");

        try
        {
            var data = htmlString.ToString();
            data.Remove(0, 1);
            data.Remove(htmlString.Length - 1, 1);
            return htmlToImageConverter.ConvertHtmlString(data);
        }
        catch
        {

        }

        return null;
    }

When I try those elements on HTML page, the icon propotion perfectly match with browser window.

Ex : enter image description here

But when I was try to bind the same elements on pdf, the propotion of the icon doesn't match with pdf. it was placed at center

Ex : enter image description here

How can i solve this issue or Is it possible to do dynamically?

0 Answers
Related