Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I wanted to extract value from a grid view and post it on to a PDF. Everything is working except the image extracted from the grid view is always too big.
For eg:
There is 4 columns in the grid view. The 2 column would be the image. It would always overlap with the other 2 columns.

Code for gridview:
XML
<asp:GridView ID="gv_CartProduct" runat="server" AutoGenerateColumns="False" onRowCommand="gv_CartProduct_OnRowCommand" >
            <Columns>
            <asp:TemplateField HeaderText="Title">
                <ItemTemplate>
                    <asp:Label ID="lbl_title" Text='<%# Eval("title") %>' runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
               <asp:TemplateField HeaderText="Image">
                        <ItemTemplate>
                            <asp:Image ID="imgPreview" ImageUrl='<%# Eval("image", GetUrl("{0}"))  %>' runat="server"
                                Height="80px" Width="80px" />
                        </ItemTemplate>
                    </asp:TemplateField>

            <asp:BoundField DataField="price" HeaderText="Price" />
           <asp:TemplateField HeaderText="Quantity">
           <ItemTemplate>
                <asp:Label ID="lbl_quantity" runat="server"  Text='<%# Eval("qty") %>' CommandArgument='<%# Eval("title")%>' ></asp:Label>
            </ItemTemplate>
             </asp:TemplateField>
         </Columns>
         </asp:GridView>



Code for PDF extraction:
private void ExportGridToPDF()
               {
                   username = (string)(Session["username"]);
                   Response.ContentType = "application/pdf";
                   Response.AddHeader("content-disposition", "attachment;filename=" + username + "'s Purchase.pdf");
                   Response.Cache.SetCacheability(HttpCacheability.NoCache);
                   StringWriter sw = new StringWriter();
                   HtmlTextWriter hw = new HtmlTextWriter(sw);
                   //gv_CartProduct.DataBind();
                   gv_CartProduct.RenderControl(hw);
                   gv_CartProduct.HeaderRow.Style.Add("width", "15%");
                   gv_CartProduct.HeaderRow.Style.Add("font-size", "10px");
                   gv_CartProduct.Style.Add("text-decoration", "none");
                   gv_CartProduct.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
                   gv_CartProduct.Style.Add("font-size", "8px");


                   StringReader sr = new StringReader(sw.ToString());
                   string str = "<h1 title=’Header’ align=’Center’> Thank You " +  username + ", for shopping at ShopNow</h1><br><table align=’Center’><tr><td style=’width:100px;color:green’> You have made successful payment at ShopNow online shop. Please keep this invoice. Thank You</td></tr></table>";
                   StringReader sr1 = new StringReader(str);
                   Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
                   HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                   PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                   pdfDoc.Open();
                   htmlparser.Parse(sr1);
                   htmlparser.Parse(sr);
                   ConfirmPurchase();


                   pdfDoc.Close();
                   Response.Write(pdfDoc);
                   Response.End();
               }
Posted
Comments
Avik Ghosh22 31-Jan-15 3:26am    
http://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900