Sumit Banglorkar

Sumit Banglorkar

  • NA
  • 38
  • 29.6k

Create Blank page in PDF using Itextsharp library

Jun 13 2013 2:44 AM
Hello Everyone,
I'm facing problem while generating PDF. The below code is used for every page, but for some pages its generates PDF with data & some page generates PDF with No Data(blank).
Please help me out. Thanks in advance...!!

Namespace
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;

Here is my code:-
            if (GridViewAnalysisCategoryMaster.Rows.Count == AppConstants.EMPTY_STRING_LEN)
            {
                ChangeLabelColor(lblerror, "No Records Found to Export", AppConstants.BOOL_TRUE);
                return;
            }
            else
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=AnalysisCategoryMaster_" + DateTime.Now.ToString("dd/MM/yyyy") + ".pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                BindData();  // Here i'm binding gridview data to generate PDF.
                GridViewAnalysisCategoryMaster.AllowPaging = AppConstants.BOOL_FALSE;
                GridViewAnalysisCategoryMaster.DataBind();
                GridViewAnalysisCategoryMaster.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.Write(pdfDoc);
                Response.End();
            }

Regards,
Sumit

Answers (2)