Hello
Friends in last article(link)
I have shown how to export gridview data to excel and word format. Now in this
article
I am going to show export to pdf.
For this you need gridview you can bind grid using
database refer this link for code to bind gridview (Bind Grid)
Aspx
page code for gridview and button click
<asp:ImageButton ID="btnPDF" runat="server" ImageUrl="~/PDF.jpg" Width="32px" Height="32px"
OnClick="btnPDF_Click"
/>
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
Font-Names="Arial"
Font-Size="11pt"
AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green"
AllowPaging="True"
OnPageIndexChanging="OnPaging"
CellPadding="4"
ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle
BackColor="White"></AlternatingRowStyle>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkCol0" runat="server" Checked="true" />
<asp:Label ID="lblCol0" runat="server" Text="CustomerID" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblCustomerID" runat="server" Text='<%# Eval("CustomerID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkCol1" runat="server" Checked="true" />
<asp:Label ID="lblCol1" runat="server" Text="ContactName" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkCol2" runat="server" Checked="true" />
<asp:Label ID="lblCol2" runat="server" Text="City"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%# Eval("City")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF"
/>
<FooterStyle BackColor="#507CD1"
Font-Bold="True"
ForeColor="White"
/>
<HeaderStyle BackColor="#507CD1"
Font-Bold="True"
ForeColor="White"></HeaderStyle>
<PagerStyle BackColor="#2461BF"
ForeColor="White"
HorizontalAlign="Center"
/>
<RowStyle BackColor="#EFF3FB"
/>
<SelectedRowStyle
BackColor="#D1DDF1"
Font-Bold="True"
ForeColor="#333333"
/>
<SortedAscendingCellStyle
BackColor="#F5F7FB"
/>
<SortedAscendingHeaderStyle
BackColor="#6D95E1"
/>
<SortedDescendingCellStyle
BackColor="#E9EBEF"
/>
<SortedDescendingHeaderStyle
BackColor="#4870BE"
/>
</asp:GridView>
Code
for export to pdf format
protected void btnPDF_Click(object sender, ImageClickEventArgs
e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new
StringWriter();
HtmlTextWriter hw = new
HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
GridView1.HeaderRow.Style.Add("width",
"15%");
GridView1.HeaderRow.Style.Add("font-size",
"10px");
GridView1.Style.Add("text-decoration",
"none");
GridView1.Style.Add("font-family",
"Arial, Helvetica, sans-serif;");
GridView1.Style.Add("font-size",
"8px");
StringReader sr = new
StringReader(sw.ToString());
Document pdfDoc = new
Document(PageSize.A2,
7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new
HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,
Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
You can download complete from this link Download Code
Comments
Post a Comment