0
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);
            grdUOM.AllowPaging = false;
            grdUOM.DataSource = (DataSet)(ViewState["pdf"]);
            grdUOM.DataBind();
            grdUOM.RenderControl(hw);
            grdUOM.HeaderRow.Style.Add("width", "15%");
            grdUOM.HeaderRow.Style.Add("font-size", "10px");
            grdUOM.Style.Add("text-decoration", "none");
            grdUOM.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdUOM.Style.Add("font-size", "8px");
            StringReader sr = new StringReader(sw.ToString());
            iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A4, 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();

Above is my code for exporting my gridview to PDF.Screen shot of the grid

Screen shot of the pdf

Above screen shots are of the gridview and the PDF file, which is generated after. In the PDF screen shot, the alignment is totally distorted. What changes will I have to make in my above code to get the right table format in my PDF file?

emshore
  • 489
  • 6
  • 15
user2998990
  • 970
  • 5
  • 18
  • 36
  • 1
    `HTMLWorker` is deprecated and very limited. Change your code to use `XMLWorker` and tell us if you still have problems. – Paulo Soares Feb 22 '15 at 08:52
  • @PauloSoares : Can you provide me some code, I am not able to get XMLWorker. Also I am not using HTMLWorker – user2998990 Feb 22 '15 at 14:10
  • You are using `HTMLWorker`, unless someone hijacked you code. You have XMLWorker examples [here](http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html). – Paulo Soares Feb 22 '15 at 14:20
  • @PauloSoares : I am not able to find XMLWorker. It works fine when the number of columns is more. Now the number of columns are 2. I am not getting any problem when the number of coluns are more than 2. – user2998990 Feb 22 '15 at 14:22
  • [Here's a good starter to read](http://stackoverflow.com/q/25164257/231316). The fourth paragraph is very important in your case. iTextSharp **does not work with GridViews**! Very, very important. iTextSharp works with HTML. It is your job to convert your GridView to HTML which you are doing but I'm guessing you haven't actually looked at the HTML. You need to inspect the HTML (and don't just view source on the page, that can be different HTML) and if you still have some problems, then post your HTML that's causing issues. – Chris Haas Feb 22 '15 at 16:03

0 Answers0