<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Webpdfgenerate.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<%--<asp:Panel ID="pnlPerson" runat="server">--%>
<div ID="pnlPerson" runat="server">
<table border="1" style="font-family: Arial; font-size: 10pt; width: 200px">
<tr><td>
<div><img src="http://localhost:60736/img/mukesh1.jpg" /></div>
</td>
<tr>
<td colspan="2" style="background-color: #18B5F0; height: 18px; color: White; border: 1px solid white">
<b>Personal Details</b>
</td>
</tr>
<tr>
<td><b>Name:</b></td>
<td><asp:Label ID="lblName" runat="server"></asp:Label></td>
</tr>
<tr>
<td><b>Age:</b></td>
<td><asp:Label ID="lblAge" runat="server"></asp:Label></td>
</tr>
<tr>
<td><b>City:</b></td>
<td><asp:Label ID="lblCity" runat="server"></asp:Label></td>
</tr>
<tr>
<td><b>Country:</b></td>
<td><asp:Label ID="lblCountry" runat="server"></asp:Label></td>
</tr>
</table>
</div>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
</form>
</body>
</html>===========================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Data;
namespace Webpdfgenerate
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Populate DataTable
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Age");
dt.Columns.Add("City");
dt.Columns.Add("Country");
dt.Rows.Add();
dt.Rows[0]["Name"] = "Mudassar Khan";
dt.Rows[0]["Age"] = "27";
dt.Rows[0]["City"] = "Mumbai";
dt.Rows[0]["Country"] = "India";
//Bind Datatable to Labels
lblName.Text = dt.Rows[0]["Name"].ToString();
lblAge.Text = dt.Rows[0]["Age"].ToString();
lblCity.Text = dt.Rows[0]["City"].ToString();
lblCountry.Text = dt.Rows[0]["Country"].ToString();
}
}
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlPerson.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();
}
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<%--<asp:Panel ID="pnlPerson" runat="server">--%>
<div ID="pnlPerson" runat="server">
<table border="1" style="font-family: Arial; font-size: 10pt; width: 200px">
<tr><td>
<div><img src="http://localhost:60736/img/mukesh1.jpg" /></div>
</td>
<tr>
<td colspan="2" style="background-color: #18B5F0; height: 18px; color: White; border: 1px solid white">
<b>Personal Details</b>
</td>
</tr>
<tr>
<td><b>Name:</b></td>
<td><asp:Label ID="lblName" runat="server"></asp:Label></td>
</tr>
<tr>
<td><b>Age:</b></td>
<td><asp:Label ID="lblAge" runat="server"></asp:Label></td>
</tr>
<tr>
<td><b>City:</b></td>
<td><asp:Label ID="lblCity" runat="server"></asp:Label></td>
</tr>
<tr>
<td><b>Country:</b></td>
<td><asp:Label ID="lblCountry" runat="server"></asp:Label></td>
</tr>
</table>
</div>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
</form>
</body>
</html>===========================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Data;
namespace Webpdfgenerate
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Populate DataTable
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Age");
dt.Columns.Add("City");
dt.Columns.Add("Country");
dt.Rows.Add();
dt.Rows[0]["Name"] = "Mudassar Khan";
dt.Rows[0]["Age"] = "27";
dt.Rows[0]["City"] = "Mumbai";
dt.Rows[0]["Country"] = "India";
//Bind Datatable to Labels
lblName.Text = dt.Rows[0]["Name"].ToString();
lblAge.Text = dt.Rows[0]["Age"].ToString();
lblCity.Text = dt.Rows[0]["City"].ToString();
lblCountry.Text = dt.Rows[0]["Country"].ToString();
}
}
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlPerson.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();
}
}
}