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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Santoshpdf
This post was updated on .

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

I am getting the above error in finally block .

public static void IncreaseTopMargin(string inputFile, int numOfLinesToIncrease)
        {
            int increaseMargin = (numOfLinesToIncrease * 10);
            string tempFile = inputFile;
           
            //We are getting the complete path and extention of the file(inputfile), so i have commented the below four lines  

           /* string tempFile = Path.GetDirectoryName(inputFile);
            tempFile = tempFile + @"\" + Path.GetFileNameWithoutExtension(inputFile) + Guid.NewGuid() + Path.GetExtension(inputFile);
            File.Copy(inputFile, tempFile);
            inputFile = tempFile;*/
            PdfReader reader = new PdfReader(inputFile);
            int n = reader.NumberOfPages;
            Document document = new Document(PageSize.A4);
            try
            {
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(inputFile, FileMode.Create));              
                document.Open();
                PdfContentByte directcontent = writer.DirectContent;
                PdfImportedPage page;
                Image image;
                for (int i = 1; i <= n; i++)
                {
                    page = writer.GetImportedPage(reader, i);
                    image = Image.GetInstance(page);
                    image.ScaleAbsolute(PageSize.A4.Width - 25, PageSize.A4.Height - increaseMargin);
                    //image.ScaleAbsolute(PageSize.A4.Width-72, PageSize.A4.Height - 72);
                    image.SetAbsolutePosition(0, 0);
                    directcontent.AddImage(image);
                    document.NewPage();
                }
               
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
           finally
            {
                document.Close();
                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
            }

        }

I am not getting what the problem . Plese Help

Thanks
Santosh

   
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: The process cannot access the file ' .pdf' because it is being used by another process + itextsharp

Fabrizio Accatino
The error is not related to iTextSharp.

Solution: close the PdfReader  before calling  File.Delete.


   fabrizio


------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
itextsharp-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: The process cannot access the file ' .pdf' because it is being used by another process

amvx86
In reply to this post by Santoshpdf
Hey...

This is the only form I have found and I need some help type quick This is killing me. I've redone my project because the previous one was babbling about assembly version conflicts so I said screw it and started programming it again. Now, I'm being plagued by this same exact error. Here is part of my code that was working previously and now is bombing left and right. Can someone please help me? This is in vb 2010 here is the code:

            For b As Integer = 0 To lboxconv.Items.Count - 1
                '//GRAB THE EXTENSION WE ARE ADDING
                Dim extension = Microsoft.VisualBasic.Right(b, 3)
                '//PROCESS THIS REQUEST
                '//PROCESS THIS AS PDF FILE

                Dim doc As New Document()
                Dim File_Type As String
                File_Type = Replace(lboxconv.Items(b), ".png", ".pdf", vbTextCompare)
                Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(File_Type, FileMode.Create))
                doc.Open()
                Dim imgpath As String = lboxconv.Items(b)
                Dim png As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imgpath)
                png.ScaleToFit("500", "500")
                png.Alignment = Element.ALIGN_CENTER
                doc.Add(png)
                For ax = 0 To 10
                    doc.Close()
                Next ax
            Next
        End If

I have also added an event to delete the old pdf files which it still creates the problem. If i use an on error resume next function what happens is that it will copy into the existing PDF file (as it used to) and it will only create 15 bytes of the file and then move on to the next one. Can anyone PLEASE help me with this problem?

./x86

Santoshpdf wrote
The process cannot access the file ' .pdf' because it is being used by another process

I am getting the above error in finally block .

public static void IncreaseTopMargin(string inputFile, int numOfLinesToIncrease)
        {
            int increaseMargin = (numOfLinesToIncrease * 10);
            string tempFile = inputFile;
           
            //We are getting the complete path and extention of the file(inputfile), so i have commented the below four lines  

           /* string tempFile = Path.GetDirectoryName(inputFile);
            tempFile = tempFile + @"\" + Path.GetFileNameWithoutExtension(inputFile) + Guid.NewGuid() + Path.GetExtension(inputFile);
            File.Copy(inputFile, tempFile);
            inputFile = tempFile;*/
            PdfReader reader = new PdfReader(inputFile);
            int n = reader.NumberOfPages;
            Document document = new Document(PageSize.A4);
            try
            {
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(inputFile, FileMode.Create));              
                document.Open();
                PdfContentByte directcontent = writer.DirectContent;
                PdfImportedPage page;
                Image image;
                for (int i = 1; i <= n; i++)
                {
                    page = writer.GetImportedPage(reader, i);
                    image = Image.GetInstance(page);
                    image.ScaleAbsolute(PageSize.A4.Width - 25, PageSize.A4.Height - increaseMargin);
                    //image.ScaleAbsolute(PageSize.A4.Width-72, PageSize.A4.Height - 72);
                    image.SetAbsolutePosition(0, 0);
                    directcontent.AddImage(image);
                    document.NewPage();
                }
               
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
           finally
            {
                document.Close();
                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
            }

        }

I am not getting what the problem . Plese Help

Thanks
Santosh

   
Loading...