Monday, June 16, 2008

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

Few days ago, I was trying to export report in Excel, Word and PDF format.

The code that I used works perfect for the Excel and Word file, however, when I tried to do the same on PDF file, I got the following error in Adobe Acrobat Reader version 7 and Internet Explorer 6.0:

There was an error opening this document. The file is damaged and could not be repaired.
I had been searched the solution for hours. I had tried various other things such as Response.BinaryWrite, Response.ClearContent, Response.ClearHeaders, and also some other header settings, but no luck.

Finally, I got this code that works fine.

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
Response.ContentType = "application/pdf";
Response.BinaryWrite(Report.ToArray());
Response.End();


Hope this can hope those facing the same problem. Cheers!

5 comments:

  1. Hi, i have the same problem exporting to pdf, but i cant find some of the methods you putted, like Clear or End. My responde object type is HttpServletResponse, yours is different?

    ReplyDelete
  2. I am not sure what your code looks like, but may be you can try this:

    BufferedInputStream input = null;

    // Get file.
    BufferedInputStream input1 = new BufferedInputStream(new FileInputStream("test.pdf"));
    int contentLength = input1.available();

    // Init servlet response.
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
    response.setContentType("application/pdf");
    response.setContentLength(contentLength);
    response.setHeader("Content-disposition", "attachment; filename=\test.pdf"+ "\"");

    // Write file contents to response.
    while (contentLength-- > 0) {
    response.getOutputStream().write(input1.read());
    }

    // Finalize task.
    context.responseComplete();

    ReplyDelete
  3. Hi, i have some problem exporting to pdf
    Document document = new Document();

    System.IO.MemoryStream msReport = new System.IO.MemoryStream();
    PdfWriter writer = PdfWriter.GetInstance(document, msReport);
    document.Open();
    iTextSharp.text.Table datatable = new iTextSharp.text.Table(8);
    datatable.Padding = 4;
    datatable.Spacing = 0;
    //datatable.setBorder(Rectangle.NO_BORDER);
    float[] headerwidths = { 50, 50, 50, 50, 50, 40, 40, 45 };
    datatable.Widths = headerwidths;
    datatable.WidthPercentage = 100;
    Cell cell = new Cell(new Phrase("Applicant Wise Report", FontFactory.GetFont(FontFactory.HELVETICA, 15 ,Font.BOLD)));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    cell.Leading = 15;
    cell.Colspan = 8;
    cell.Border = Rectangle.NO_BORDER;
    cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
    datatable.AddCell(cell);
    datatable.DefaultCellBorderWidth = 1;
    datatable.DefaultHorizontalAlignment = 1;
    datatable.DefaultRowspan = 1;
    datatable.AddCell(new Phrase("Name", FontFactory.GetFont(FontFactory.TIMES, 11, Font.BOLD)));
    datatable.AddCell(new Phrase("Address", FontFactory.GetFont(FontFactory.TIMES, 11, Font.BOLD)));
    datatable.AddCell(new Phrase("State", FontFactory.GetFont(FontFactory.TIMES, 11, Font.BOLD)));
    datatable.AddCell(new Phrase("City", FontFactory.GetFont(FontFactory.TIMES, 11, Font.BOLD)));
    datatable.AddCell(new Phrase("Email ID", FontFactory.GetFont(FontFactory.TIMES, 11, Font.BOLD)));
    datatable.AddCell(new Phrase("Exam Attended", FontFactory.GetFont(FontFactory.TIMES, 11, Font.BOLD)));
    datatable.AddCell(new Phrase("Result Status", FontFactory.GetFont(FontFactory.TIMES, 11, Font.BOLD)));
    datatable.AddCell(new Phrase("Exam Date", FontFactory.GetFont(FontFactory.TIMES, 11, Font.BOLD)));

    datatable.DefaultCellBorderWidth = 1;
    datatable.DefaultRowspan = 1;
    if (Dt.Rows.Count > 0)
    {
    for (int i = 0; i <= Dt.Rows.Count - 1; i++)
    {
    datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
    string name = Convert.ToString(Dt.Rows[i][0]);
    datatable.AddCell(new Cell(new Phrase(name, FontFactory.GetFont(FontFactory.TIMES, 10, Font.NORMAL))),i+1,0);
    string address = Convert.ToString(Dt.Rows[i][1]);
    datatable.AddCell(new Cell(new Phrase(address, FontFactory.GetFont(FontFactory.TIMES, 10, Font.NORMAL))), i + 1, 1);
    string state = Convert.ToString(Dt.Rows[i][2]);
    datatable.AddCell(new Cell(new Phrase(state, FontFactory.GetFont(FontFactory.TIMES, 10, Font.NORMAL))), i + 1, 2);
    string city = Convert.ToString(Dt.Rows[i][3]);
    datatable.AddCell(new Cell(new Phrase(city, FontFactory.GetFont(FontFactory.TIMES, 10, Font.NORMAL))), i + 1, 3);
    string mailid = Convert.ToString(Dt.Rows[i][4]);
    datatable.AddCell(new Cell(new Phrase(mailid, FontFactory.GetFont(FontFactory.TIMES, 10, Font.NORMAL))), i + 1, 4);
    string exam = Convert.ToString(Dt.Rows[i][5]);
    datatable.AddCell(new Cell(new Phrase(exam, FontFactory.GetFont(FontFactory.TIMES, 10, Font.NORMAL))), i + 1, 5);
    string result = Convert.ToString(Dt.Rows[i][6]);
    datatable.AddCell(new Cell(new Phrase(result, FontFactory.GetFont(FontFactory.TIMES, 10, Font.NORMAL))), i + 1, 6);
    string examdate = Convert.ToString(Dt.Rows[i][7]);
    datatable.AddCell(new Cell(new Phrase(examdate, FontFactory.GetFont(FontFactory.TIMES, 10, Font.NORMAL))), i + 1, 7);

    }

    document.Add(datatable);
    }


    document.Close();
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=ApplicantWiseReport.pdf");
    Response.ContentType = "application/pdf";
    Response.BinaryWrite(msReport.ToArray());
    Response.End();

    ReplyDelete
  4. Hi Amit Panwar,

    pls refer to my previous post ->
    Export Gridview to PDF

    :)

    ReplyDelete
  5. Thanks very much.

    Your solution worked like a charm.
    Thanks a lot
    - Hidha.

    ReplyDelete