[Solved] SVG Files not getting converted to PDF using iTextSharp in C# .Net

Last Reply one year ago By kalpesh

Posted one year ago


I am using ITextSharp in my WebApplication to export a WebPage on a button click. I am facing 2 issues at present:

1. Web Page is throwing error : image.svg is not a recognized imageformat 2. Web Page is not exporting as it is. There are so many spacing and alignment issue. I want ot export the WebPage exactly as it looks like.

At present I have created a sample solution for my requirement. The code is as below:

HTML Code:

    <form id="form1" runat="server">

        <div id="sidenav" class="sidenav">
            <div class="menu" id="home1" data-id="1" runat="server" title="Home">
                <a href="../Login/Home.aspx" >
                    <img runat="server" id="img1" width="50" height="50"/>

            <div class="menu" id="menu1" data-id="2" runat="server" title="myPage">
                <img runat="server" id="img2" width="50" height="50">

            <img runat="server" id="img" width="50" height="50"/><br />
        <div style="font-family: Arial">
            This is a test page</div>
            <table border="1" width="100">
        <asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" /></div>

C# Code:

protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                img.Src = GetUrl("Images/Koala.jpg");
                //img.Width = 50;
                //img.Height = 50;
                img1.Src = GetUrl("Img/01 Home.svg");
                img2.Src = GetUrl("Img/02 myPage.svg");
        protected void btnExport_Click(object sender, EventArgs e)
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            htmlparser.Parse(sr); //error here: .svg is not a recognized imageformat.

        protected string GetUrl(string imagepath)
            string[] splits = Request.Url.AbsoluteUri.Split('/');
            if (splits.Length >= 2)
                string url = splits[0] + "//";
                for (int i = 2; i < splits.Length - 1; i++)
                    url += splits[i];
                    url += "/";
                return url + imagepath;
            return imagepath;


Posted one year ago Modified on one year ago

iTextSharp doesn't support SVG format. You'd need to use another library to either convert it to one of the supported formats (JPEG, GIF, PNG, TIFF, BMP, WMF and EPS) before you insert it.
You can convert .SVG file to PNG and then you can use that file as SRC file for Image tag.

Refer the below link for your reference to convert SVG file to Other Format as Valid Image format and then assign the converted file path as SRC to your image tag to work with iTextSharp.

 Convert SVG to PNG or JPEG using C# in ASP.Net