Hi Richa,
If you want the page to work with images you need to convert the Relative Urls to Absolute ones.
Example : ~/Images/1.jpg must be changed to http://localhost/Images/1.jpg
Check this example. Now please take its reference and correct your code.
HTML
<div>
<img runat="server" id="img" /><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>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
img.Src = GetUrl("Images/1.jpg");
img.Width = 50;
img.Height = 50;
}
}
protected void btnExport_Click(object sender, EventArgs e)
{
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);
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;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
img.Src = GetUrl("Images/1.jpg")
img.Width = 50
img.Height = 50
End If
End Sub
Protected Sub btnExport_Click(sender As Object, e As EventArgs)
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Me.Page.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.[End]()
End Sub
Protected Function GetUrl(ByVal imagepath As String) As String
Dim splits As String() = Request.Url.AbsoluteUri.Split("/"c)
If splits.Length >= 2 Then
Dim url As String = splits(0) & "//"
For i As Integer = 2 To splits.Length - 1 - 1
url += splits(i)
url += "/"
Next
Return url & imagepath
End If
Return imagepath
End Function
Namespaces
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Text;
VB.Net
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Screenshot