2
Answers

How to troubleshoot error while opening downloaded pdf file

Ask a question

In my Windows application, I have a PDF file stored in my database. Problem is, when I try to open the PDF file, I get an error

Adobe Reader could not open 'tem1.pdf' because it is either not a supported file type or because the file has been damaged

But when I open this downloaded PDF normally, it does not give that error. I am getting error when I try to open them through my application.

The code I try to open pdf files which are stored in database is as follows:

private void lstBookParts_SelectedIndexChanged(object sender, EventArgs e)
{
        if (lstBookParts.SelectedItems.Count > 0)
        {
            WiCommonFunction.LoadCommonSettings();
            ShowInformation showData = new ShowInformation();
            string item = lstBookParts.SelectedItems[0].Text;
            string book = bookName;
            CalculateCount(book);
            DataSet ds1 = showData.ShowBookPDF(item);
            DataTable dt1 = ds1.Tables[0];
            FileStream FS = null;
            var index = lstBookParts.SelectedIndices;
            Int32 i = (Int32)(index[0]);
            byte[] bytes = (byte[])(dt1.Rows[i]["Content"]);
            string filepath = "D:\\temp1.pdf";
            FS = new FileStream(filepath, System.IO.FileMode.Create);
            FS.Write(bytes, 0, bytes.Length);
            FS.Close();
            Process proc = new Process();
            proc.StartInfo.FileName = filepath;
            proc.Start();
        }
}

By using this code some PDF files get open properly, others are giving error.

How can I solve this problem? Is there any need to make changes in the code?

Please suggest me any solution for this.

Thanks in advance.


Answers (2)