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

后端 未结 4 515
-上瘾入骨i
-上瘾入骨i 2020-12-17 00:47

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

4条回答
  •  孤街浪徒
    2020-12-17 01:47

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

提交回复
热议问题