Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have 10 .pdf files bind to grid view. In grid view two rom_commands i.e(view and modify).
click on View open PDF form in I-frame. modify the PDF Text with in the IFrame and click on Save Button but i got the error. Here every form open new page by using Iframe.

Cannot access the file because it is being used by another process.

how to solve my error. please help me friends.

my code like this

C#
//Page_Load
                string olddoc = Server.MapPath("../EmployeeAgreements/I-9 form" + Session["empid"] + ".pdf");                    
              

                string pdfTemplate = olddoc;

                PdfReader pdfReader = new PdfReader(pdfTemplate);
                System.Threading.Thread.Sleep(5000);
               
                PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
                         pdfTemplate, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite));
               

               
                AcroFields pdfFormFields = pdfReader.AcroFields;
                string validpath = "";
                string Identity = pdfFormFields.GetField("txtIdentify");


                txtLname.Value = pdfFormFields.GetField("txtLname");
                txtFname.Value = pdfFormFields.GetField("txtFname");
                txtMname.Value = pdfFormFields.GetField("txtMname");

<pre lang="cs">pdfReader.Close();

             System.Threading.Thread.Sleep(1000);

             //  bool x=  IsFileLocked(FileInfo);


             pdfStamper.FormFlattening = false;
             try
             {

                 pdfStamper.Close();
             }
             catch
             {
                 System.Threading.Thread.Sleep(4000);
                 pdfStamper.Close();
             }



C#
//Button click
   string source_file = Server.MapPath("../EmployeeAgreements/I-9 form" + Session["empid"] + ".pdf");
               
                string pdfTemplate = Server.MapPath("../EmployeeAgreements/OriganlDocs/I-9 form.pdf");
            
                string newFile = source_file;

                PdfReader pdfReader = new PdfReader(pdfTemplate);

                System.Threading.Thread.Sleep(5000);


                PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
                           newFile, FileMode.OpenOrCreate, FileAccess.ReadWrite));



                AcroFields pdfFormFields = pdfStamper.AcroFields;


                pdfFormFields.SetField("txtIdentify", "I-9 form" + Session["empid"] + ".pdf");

                pdfFormFields.SetField("txtIdentity", "I-9 form" + Session["empid"] + ".pdf");
                pdfFormFields.SetField("txtLname", txtLname.Value);
                pdfFormFields.SetField("txtFname", txtFname.Value);
                pdfFormFields.SetField("txtMname", txtMname.Value);
               
<pre lang="cs">pdfReader.Close();

pdfStamper.FormFlattening = false;


pdfStamper.Close();


}

Under page_load code working fine. but when click on Save Button i got the error Please help me..

thanks and Regards
Posted
Updated 7-Aug-13 19:54pm
v2

1 solution

Looks like you are nor Dispose()-ing correctly. Do PdfReader and PdfStamperimplement IDisposable? If so, use some using blocks with those variables. I.e.
C#
using(PdfReader pdfReader = new PdfReader(pdfTemplate))
{
   ...
   using(PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)))
   {
        ...
   }
}
 
Share this answer
 
Comments
[no name] 8-Aug-13 2:34am    
+5 from me.
Santhosh23 8-Aug-13 5:37am    
Hi Bernhard Hiller,

I am modify the code with your syntax
but i got the this type Error Error
iTextSharp.text.pdf.PdfReader': type used in a using statement must be implicitly convertible to 'System.IDisposable'
Bernhard Hiller 8-Aug-13 9:36am    
Then that means: PdfReader does not implement IDisposable. Very bad... What about PdfStamper?

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