iTextSharp Error: There was an error opening this document. The file is damaged and could not be repaired

Last Reply 5 months ago By vrindavani

Posted 5 months ago

export gridview to pdf on button click in c#

By this I am able to download it in pdf but as I open it is giving error 

There was an error opening this document. The file is damaged and could not be repaired.

protected void ExportToPDF(object sender, EventArgs e)
{
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            //To Export all pages
            //  pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Book1.pdf");
            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);
            Grid_Live.AllowPaging = false;
            this.ReadData();

            Grid_Live.RenderControl(hw);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }
    }
}

 

Posted 5 months ago

You need to Bind the GridView and Render GridView Control before StringReader, also need to close the Document.

Refer the updated code.

protected void ExportToPDF(object sender, EventArgs e)
{
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            Grid_Live.AllowPaging = false;
            this.ReadData();
            Grid_Live.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=Book1.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }
    }
}

public override void VerifyRenderingInServerForm(Control control)
{
}

 


Posted 5 months ago

I have done with this code , now it is getting opened by acrobat reader also.

protected void ExportToPDF(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment;filename=Book1.pdf");
        Response.ContentType = "application/pdf";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        StringWriter sw = new StringWriter();
        HtmlTextWriter htm = new HtmlTextWriter(sw);

        Grid_Live.AllowPaging = false;
        Grid_Live.DataBind();
        this.ReadData();
        Grid_Live.RenderControl(htm);

        StringReader stringReader = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlWorker = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

        pdfDoc.Open();
        htmlWorker.Parse(stringReader);
        pdfDoc.Close();

        Response.Write(pdfDoc.ToString());
        Response.End();
    }

 previously also it was geting download but in opening it was giving error but with google docs it was opening only.