Cannot access the file because it is being used by another process
Asked Answered
U

4

6

My web method creates a pdf file in my %temp% folder and that works. I then want to add some custom fields (meta) to that file using the code below.

The class PdfStamper generates an IOException, whether I use its .Close() method or the using block just ends. The process that is still holding on to the file handle is the webdev web server itself (I'm debugging in VS2010 SP1).

private string AddCustomMetaData(string guid, int companyID, string filePath)
{
    try
    {
        PdfReader reader = new PdfReader(filePath);

        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            PdfStamper st = new PdfStamper(reader, fs);
            Dictionary<string, string> info = reader.Info;
            info.Add("Guid", guid);
            info.Add("CompanyID", companyID.ToString());

            st.MoreInfo = info;
            st.Close();
        }

        reader.Close();

        return guid;
    }
    catch (Exception e)
    {
        return e.Message;
    }
}

No matter what I try, it keeps throwing the exception at st.Close();, to be more precise:

The process cannot access the file 'C:\Users[my username]\AppData\Local\Temp\53b96eaf-74a6-49d7-a715-6c2e866a63c3.pdf' because it is being used by another process.

Either I'm overlooking something obvious or there's a problem with the PdfStamper class I'm as of yet unaware of. Versions of itextsharp used are 5.3.3.0 and 5.4.0.0, the issue is the same.

Any insight would be greatly appreciated.

EDIT: I'm currently "coding around" the issue, but I haven't found any solution.

Unanswerable answered 25/3, 2013 at 14:49 Comment(2)
are you sure that the file is not in use before the pdf reader opens it? (as in what are you doing with this file prior to this)Anderaanderea
better yet, try switching the order of your file opening (filestream before pdf reader)Anderaanderea
I
20

Your problem is that you are writing to a file while you are also reading from it. Unlike some file types (JPG, PNG, etc) that "load" all of the data into memory, iTextSharp reads the data as a stream. You either need to use two files and swap them at the end or you can force iTextSharp to "load" the first file by binding your PdfReader to a byte array of the file.

PdfReader reader = new PdfReader(System.IO.File.ReadAllBytes(filePath));
Illyrian answered 26/3, 2013 at 13:56 Comment(1)
I've been looking for this solution for hours. Works perfectly!Keldah
D
1

I suggest you to use the FileShare enumerator when you open the file, so Try to open a file with None sharing

   File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
Dogie answered 25/3, 2013 at 14:53 Comment(0)
D
0

Try to .Dispose() your PDF reader (or whatever you use for creating it) when you save the file for the first time

Defrock answered 25/3, 2013 at 14:51 Comment(1)
That's beyond my reach. The web service calls an ActiveX dll that accepts a file path. The first write op is out of my control.Unanswerable
F
0

Try this solution if you think its feasible for you - Once the webmethod creates file in Temp folder, you need to copy the file and paste it into other location or same location with different name and pass newly copied file path to your PDF reader.

Feathered answered 25/3, 2013 at 15:34 Comment(1)
Yes, but that doesn't explain what the reason is of this problemUnanswerable

© 2022 - 2024 — McMap. All rights reserved.