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

社会主义新天地 提交于 2019-11-29 04:35:41

You're 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));

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);

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!