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