Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
In my Web Application project I am retrieving the File Data Stored in the database storing that data in a temporary file and printing that Temporary File (the file will be either PDF or Word File While I try to print the file the Application Adobe reader or MS Word tells that file is not found, Can any one say what is the Problem in my Code which I have mentioned below,

C#
protected void ggvqpdetail_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           if (e.CommandName.ToUpper().ToString() == "PRINTREC")
           {

               try
               {
                   string a = TextBox1.Text + TextBox2.Text + TextBox3.Text;
                   DataTable dt = new DataTable();
                   IDataReader dr = ExamManagement.SP.Eval_QP_PrintSelect(a).GetReader();
                   dt.Load(dr);
                   dr.Close();
                   dr.Dispose();

                   Byte[] data = (Byte[])dt.Rows[0]["Data"];

                   if (ddlprint.SelectedIndex != -1 && dt.Rows.Count > 0)
                   {
                       if (dt.Rows[0]["Type"].ToString() == "application/pdf")
                       {
                          PrintFormPdfData(data);
                       }
                       else if (dt.Rows[0]["Type"].ToString() == "application/vnd.ms-word")
                       {
                           PrintFormWordData(data);
                       }

                   }
               }
               catch(Exception ex)
               {
                   ClientMessaging(ex.Message);
               }
           }
       }
       private void PrintFormPdfData(byte[] formPdfData)
       {
           string tempFile;

           tempFile = Path.GetTempFileName();

           using (FileStream fs = new FileStream(tempFile, FileMode.Create))
           {
               fs.Write(formPdfData, 0, formPdfData.Length);
               fs.Flush();
           }

           try
           {
               string gsArguments;
               string gsLocation;
               ProcessStartInfo gsProcessInfo;
               Process gsProcess;

               gsArguments = string.Format("/h /t \""+ddlprint.SelectedItem.ToString()+"\" \"{0}\"", tempFile);
               gsLocation = @"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe";

               gsProcessInfo = new ProcessStartInfo();
               gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
               gsProcessInfo.FileName = gsLocation;
               gsProcessInfo.Arguments = gsArguments;

               gsProcess = Process.Start(gsProcessInfo);
               gsProcess.WaitForExit();
               gsProcess.Kill();
           }
           finally
           {

               File.Delete(tempFile);
           }
       }
       private void PrintFormWordData(byte[] formWordData)
       {
           string tempFile;

           tempFile = Path.GetTempFileName();

           using (FileStream fs = new FileStream(tempFile, FileMode.Create))
           {
               fs.Write(formWordData, 0, formWordData.Length);
               fs.Flush();
           }

           try
           {
               string gsArguments;
               string gsLocation;
               ProcessStartInfo gsProcessInfo;
               Process gsProcess;

               gsArguments = string.Format("/h /t \"" + ddlprint.SelectedItem.ToString() + "\" \"{0}\"", tempFile);
               gsLocation = @"C:\Program Files\Microsoft Office\OFFICE11\WINWORD.exe";

               gsProcessInfo = new ProcessStartInfo();
               gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
               gsProcessInfo.FileName = gsLocation;
               gsProcessInfo.Arguments = gsArguments;

               gsProcess = Process.Start(gsProcessInfo);
               gsProcess.WaitForExit();
               gsProcess.Kill();
           }
           finally
           {

               File.Delete(tempFile);
           }
       }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900