Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
This code does export my gridview to pdf but some of the text gets cut off. I'm not sure how the formatting works for XMLworker. The problem is that I have so many columns that the rows need to be made higher so that I can fit everything onto the width of an A4 page. Where is the table getting the width from? Here's my code:
C#
GridView1.Columns[0].Visible = false;
       Response.ContentType = "application/pdf";

       Response.AddHeader("content-disposition", "attachment;filename=Equipment.pdf");
       Response.Cache.SetCacheability(HttpCacheability.NoCache);
       StringWriter sw = new StringWriter();
       HtmlTextWriter hw = new HtmlTextWriter(sw);
       HtmlForm frm = new HtmlForm();


       GridView1.Parent.Controls.Add(frm);
       frm.Attributes["runat"] = "server";
       frm.Controls.Add(GridView1);
       frm.RenderControl(hw);
       StringReader sr = new StringReader(sw.ToString());
       Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);//create a document object

       PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);//get a pdf writer instance

       pdfDoc.Open();//open the document
       XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);//invoke XMLWorkerHelper.getInstance().parseXhtml(writer, document, new FileInputStream)

       pdfDoc.Close();//close the document

       Response.Write(pdfDoc);
       Response.End();
Posted
Updated 13-Nov-13 3:10am
v2

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