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

Last Reply 3 months ago By kalpesh

Posted 3 months ago

Hi,

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:

<body>
    <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"/>

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

        <div>
            <img runat="server" id="img" width="50" height="50"/><br />
        </div>
        <div style="font-family: Arial">
            This is a test page</div>
        <div>
            <table border="1" width="100">
                <tr>
                    <td>Name</td>
                    <td>Age</td>
                </tr>
                <tr>
                    <td>John</td>
                    <td>11</td>
                </tr>
                <tr>
                    <td>Sam</td>
                    <td>13</td>
                </tr>
                <tr>   
                    <td>Tony</td>
                    <td>12</td>
                </tr>
            </table>
        </div>
        <div>
        <asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" /></div>
    </form>
</body>

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.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            this.Page.RenderControl(hw);
            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);
            pdfDoc.Open();
            htmlparser.Parse(sr); //error here: .svg is not a recognized imageformat.
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
        }

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

 

You are viewing reply posted by: kalpesh 3 months ago.
Posted 3 months ago Modified on 3 months 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

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html