Description:-
In this Example we will See How to Create Pdf File from Panel in dot net. For Export Data in Pdf file from Panel we will use Attachment in Code behind. Here I have user table Controls to see data in tabular format and we will Export this panel Control in Pdf using Content type through Application. So let’s start to Create Table & Panel and Export in Pdf file through Code behind in dot net.
In this Example we will See How to Create Pdf File from Panel in dot net. For Export Data in Pdf file from Panel we will use Attachment in Code behind. Here I have user table Controls to see data in tabular format and we will Export this panel Control in Pdf using Content type through Application. So let’s start to Create Table & Panel and Export in Pdf file through Code behind in dot net.
Default.aspx:-
<div style="height: 299px; width: 570px">
<asp:Panel ID="pnlPerson" runat="server" Height="260px" Width="369px">
<table border="1" style="font-family: Arial; font-size: 10pt; width: 370px; height: 253px;">
<tr>
<td colspan="2" style="background-color: #18B5F0; color: White; border: 1px solid white" class="style1">
<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>Gender:</b></td>
<td>
<asp:Label ID="lblgender" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td><b>Email-ID:</b></td>
<td>
<asp:Label ID="lblEmail" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td><b>Mobile Number:</b></td>
<td>
<asp:Label ID="lblMobileNumber" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td><b>Country:</b></td>
<td>
<asp:Label ID="lblCountry" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td><b>State:</b></td>
<td>
<asp:Label ID="lblState" 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>Status:</b></td>
<td>
<asp:Label ID="lblstatus" runat="server"></asp:Label>
</td>
</tr>
</table>
</asp:Panel>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
</div>
Style:-
<style type="text/css"> .style1 { height: 33px; font-size: 17px; text-align: center; } </style>
Default.aspx.cs:-
Now go to Code behind and we will code for Bind Data in table. Now Create
Dynamic table and Add data in table.
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.Columns.Add("State"); dt.Columns.Add("MobileNumber"); dt.Columns.Add("Email"); dt.Columns.Add("Status"); dt.Columns.Add("Gender"); dt.Rows.Add(); dt.Rows[0]["Name"] = "Adroja Umesh"; dt.Rows[0]["Age"] = "25"; dt.Rows[0]["City"] = "Amreli"; dt.Rows[0]["Country"] = "India"; dt.Rows[0]["State"] = "Gujrat"; dt.Rows[0]["MobileNumber"] = "9898989898"; dt.Rows[0]["Email"] = "UmesH@gmail.com"; dt.Rows[0]["Gender"] = "Male"; dt.Rows[0]["Status"] = "Nothig is Possible"; //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(); lblMobileNumber.Text = dt.Rows[0]["MobileNumber"].ToString(); lblState.Text = dt.Rows[0]["State"].ToString(); lblgender.Text = dt.Rows[0]["Gender"].ToString(); lblEmail.Text = dt.Rows[0]["Email"].ToString(); lblstatus.Text = dt.Rows[0]["Status"].ToString(); } }
Now on Button click event Code for Creating Pdf File.
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(); }
Thanks for comments.....