locked
The process cannot access the file '*.pdf' because it is being used by another process. RRS feed

  • Question

  • User158566248 posted

    Hello 

    i create pdf Using itextsharp dll and use that pdf ,and export data from crystal report to pdf file it 'Gives me error.

    Below is my code.

    Please help me...

    ////////////////////////////

    ExportOptions exportOpts = doc.ExportOptions;

                        exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat;

                        exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;

                        exportOpts.DestinationOptions = new DiskFileDestinationOptions();

                        // Set the disk file options.

                        DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();

                     
                        ////////////extra
                        Document doc1 = new Document();

                        PdfWriter.GetInstance(doc1, new FileStream(HttpContext.

    Current.Request.PhysicalApplicationPath + "Leads\\LeadPdf\\" + System.DateTime.Now.Second + ".pdf", FileMode.Create));

                        PdfWriter.GetInstance(doc1, HttpContext.Current.Response.OutputStream);
                       
                        //doc1.Open();
                        //doc1.Close();
                        ((DiskFileDestinationOptions)doc.ExportOptions.DestinationOptions).DiskFileName = Server.MapPath("LeadPdf\\" + System.DateTime.Now.Second +".pdf");

                        ///extra end
                        //export the report to PDF rather than displaying the report in a viewer

                        doc.Export();

     

    //

     

     

    i use itextsharp dll for creating pdf file and use that pdf , i create  pdf name just like system.datetime.now +".pdf"   for differend pdf each time when code execute.

    /////////////////////////////

    Wednesday, May 25, 2011 11:30 PM

All replies

  • User181256233 posted

    Try wrapping the filestream in a using block, that will close the stream when its done writing. Just copied your code into the using block, not sure it is correct though.

    using (var fs = new FileStream(HttpContext.
    Current.Request.PhysicalApplicationPath + "Leads\\LeadPdf\\" + System.DateTime.Now.Second + ".pdf", FileMode.Create))
    {
       Document doc1 = new Document();
       PdfWriter.GetInstance(doc1, fs);
     
       PdfWriter.GetInstance(doc1, HttpContext.Current.Response.OutputStream);
                         
       //doc1.Open();
       //doc1.Close();
     
       ((DiskFileDestinationOptions)doc.ExportOptions.DestinationOptions).DiskFileName = Server.MapPath("LeadPdf\\" + System.DateTime.Now.Second +".pdf");
     
       ///extra end 
       //export the report to PDF rather than displaying the report in a viewer
     
       doc.Export();
    }
    Sunday, May 29, 2011 10:08 AM