Hi indradeo,
Check this example. Now please take its reference and correct your code.
HTML
<table>
<tr>
<td>Name</td>
<td>
<asp:TextBox runat="server" ID="txtName" /></td>
</tr>
<tr>
<td>Age</td>
<td>
<asp:TextBox runat="server" ID="txtAge" /></td>
</tr>
</table>
<asp:Button Text="Preview" runat="server" OnClick="OnPreview" />
<asp:Button Text="Download" runat="server" OnClick="OnDownload" />
<hr />
<asp:Literal ID="ltEmbed" runat="server" />
Namespaces
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
VB.Net
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml
Code
C#
protected void OnPreview(object sender, EventArgs e)
{
GeneratePDF();
string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"300px\" height=\"200px\">";
embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
embed += "</object>";
ltEmbed.Text = string.Format(embed, ResolveUrl("~/Report.pdf"));
}
protected void OnDownload(object sender, EventArgs e)
{
GeneratePDF();
byte[] bytes = File.ReadAllBytes(Server.MapPath("~/Report.pdf"));
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=Report.pdf");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
private void GeneratePDF()
{
string html = "<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 15pt'>";
html += "<tr><td>Name :</td><td style='width:120px;border: 1px solid #ccc'>" + txtName.Text.Trim() + "</td></tr>";
html += "<tr><td>Age :</td><td style='width:120px;border: 1px solid #ccc'>" + txtAge.Text.Trim() + "</td></tr>";
html += "</table>";
using (FileStream stream = new FileStream(Server.MapPath("~/Report.pdf"), FileMode.OpenOrCreate))
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
StringReader sr = new StringReader(html);
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
stream.Close();
}
}
VB.Net
Protected Sub OnPreview(ByVal sender As Object, ByVal e As EventArgs)
GeneratePDF()
Dim embed As String = "<object data=""{0}"" type=""application/pdf"" width=""300px"" height=""200px"">"
embed += "If you are unable to view file, you can download from <a href = ""{0}"">here</a>"
embed += " or download <a target = ""_blank"" href = ""http://get.adobe.com/reader/"">Adobe PDF Reader</a> to view the file."
embed += "</object>"
ltEmbed.Text = String.Format(embed, ResolveUrl("~/Report.pdf"))
End Sub
Protected Sub OnDownload(ByVal sender As Object, ByVal e As EventArgs)
GeneratePDF()
Dim bytes As Byte() = File.ReadAllBytes(Server.MapPath("~/Report.pdf"))
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/pdf"
Response.AppendHeader("Content-Disposition", "attachment; filename=Report.pdf")
Response.BinaryWrite(bytes)
Response.Flush()
Response.[End]()
End Sub
Private Sub GeneratePDF()
Dim html As String = "<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 15pt'>"
html += "<tr><td>Name :</td><td style='width:120px;border: 1px solid #ccc'>" & txtName.Text.Trim() & "</td></tr>"
html += "<tr><td>Age :</td><td style='width:120px;border: 1px solid #ccc'>" & txtAge.Text.Trim() & "</td></tr>"
html += "</table>"
Using stream As FileStream = New FileStream(Server.MapPath("~/Report.pdf"), FileMode.OpenOrCreate)
Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0F)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
Dim sr As StringReader = New StringReader(html)
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
pdfDoc.Close()
stream.Close()
End Using
End Sub
Screenshot