Answered by:
gridview to pdf using itextsharp

Question
-
User-1118395791 posted
hi,
when i am coverting my gridview to pdf it is giving the correct output but the data in pdf is page is missing with the header text always and also not showing all the columns of the gridview..this is really a problem please help!!!
below is the code for export to pdf:
using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter hw = new HtmlTextWriter(sw)) { //To Export all pages GridView1.AllowPaging = false; this.BindGrid(); GridView1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Write(pdfDoc); Response.End(); }
below is the code of gridview in aspx:
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging"> <Columns> <asp:BoundField DataField="id" HeaderText="id" ItemStyle-Width="50px" /> <asp:BoundField DataField="Complaint_id" HeaderText="Complaint ID" ItemStyle-Width="100px" /> <asp:BoundField DataField="Registered_by" HeaderText="Registered BY" ItemStyle-Width="100px" /> <asp:BoundField DataField="Customer" HeaderText="Customer" ItemStyle-Width="100px" /> <asp:BoundField DataField="Status" HeaderText="Status" ItemStyle-Width="100px" /> <asp:BoundField DataField="Category" HeaderText="Category" ItemStyle-Width="100px" /> <asp:BoundField DataField="Sub_Category" HeaderText="Sub_Category" ItemStyle-Width="100px" /> <asp:BoundField DataField="Items" HeaderText="Items" ItemStyle-Width="100px" /> <asp:BoundField DataField="Start_Date" HeaderText="Start Date" ItemStyle-Width="100px" /> <asp:BoundField DataField="Title" HeaderText="Title" ItemStyle-Width="150px" /> <asp:BoundField DataField="Engineer_on_site" HeaderText="Engineer on site" ItemStyle-Width="100px" /> <asp:BoundField DataField="Location" HeaderText="Location" ItemStyle-Width="100px" /> <asp:BoundField DataField="Engineer_on_call" HeaderText="Engineer_on_call" ItemStyle-Width="100px" /> <asp:BoundField DataField="Priority" HeaderText="Priority" ItemStyle-Width="100px" /> <asp:BoundField DataField="Closed_date" HeaderText="Closed_date" ItemStyle-Width="100px" /> <asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-Width="100px" /> </Columns> </asp:GridView>
Tuesday, October 15, 2013 1:46 AM
Answers
-
User1124648084 posted
Hi Neha,
Please refer following link for source code and detailed explaination:
If you have any doubts related with code, please let me know.
If your problem is already sorted then please mark appropriate post as answer, which will help others to locate the correct answer.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 15, 2013 9:00 AM
All replies
-
User1992938117 posted
Hi Neha,
Please don't duplicate the post, wait untill your post approved by a moderator.
I thin you have to remove ItemStyle-Width="100px" from each bound field then try.
Also you can set "PageSize.A2" to "PageSize.A4" and try.
Some examples:
http://www.c-sharpcorner.com/UploadFile/raj1979/export-grid-view-data-to-pdf-using-itextsharp/
Tuesday, October 15, 2013 1:55 AM -
User-1118395791 posted
Please tell me if u think the code has errors..........y it is not showing color scheme or the same style as i have used in my gridview
Tuesday, October 15, 2013 2:01 AM -
User1992938117 posted
y it is not showing color scheme or the same style as i have used in my gridviewAs i know external css is not supported by iTextSharp, See the below liks for possibilities:
Tuesday, October 15, 2013 2:12 AM -
User-46109667 posted
hi,
pls trace the code and see the "hw" value for whether it add the style to the rendered html like
<table...>
<tr><th style="back-color:some;"....>.......
</table>
if not you have to style as inline or can do at code behind before rendering to "hw"...
thank you..
Tuesday, October 15, 2013 2:14 AM -
User-1118395791 posted
How???
Tuesday, October 15, 2013 2:17 AM -
User-46109667 posted
do u traced the hw value?
can do like..
Gridview1.HeaderStyle.BackColor = System.Drawing.Color.Black;
Gridview1.HeaderStyle.ForeColor = System.Drawing.Color.White;
Gridview1.RowStyle.BackColor = System.Drawing.Color.White;
Gridview1.AlternatingRowStyle.BackColor = System.Drawing.Color.WhiteSmoke;.........
Gridview1.RenderControl(hw);
i remember i did like this....
thank you..
Tuesday, October 15, 2013 2:29 AM -
User-1118395791 posted
No output :(
Tuesday, October 15, 2013 3:08 AM -
User-46109667 posted
do u traced the hw value? whether it renders the control with style. please trace and show that....
Tuesday, October 15, 2013 3:13 AM -
Tuesday, October 15, 2013 5:24 AM
-
User1124648084 posted
Hi Neha,
Please refer following link for source code and detailed explaination:
If you have any doubts related with code, please let me know.
If your problem is already sorted then please mark appropriate post as answer, which will help others to locate the correct answer.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 15, 2013 9:00 AM