1
When trying to generate PDF files, I often come across as the error:
Mensage: The process cannot access the file’D:.... 23381708.pdf' because it is being used by Another process.
Which tells me that the file cannot be created because it is already being used by another process.
This problem occurs when I am trying to generate a document with the same name as another one that I created just before.
To try to get around this situation, I’m trying:
//Definir tipo de documento
Document doc1 = new Document();
doc1.SetPageSize(iTextSharp.text.PageSize.A4);
doc1.SetMargins(0f, 0f, 0f, 0f);
doc1.NewPage();
var ficheiroAbs = Path.Combine((pastasLer.FirstOrDefault().PastaDestGARTratada + "/2Via"), nomeDoc);
//abrir documento para escrever
PdfWriter writer = PdfWriter.GetInstance(doc1, new FileStream(ficheiroAbs, FileMode.Create));//Erro ocorre nesta linha
doc1.Open();
//código criar documento
doc1.Close();
doc1 = new Document();
However, sometimes (not always) return always that error.
With the use of
.Dispose();
the same happens, that is, sometimes gives the error. I will try the second suggestion– CesarMiguel
The second way also happens the same
– CesarMiguel
What I am doing is creating the file and sending it by email as an attachment. You may have something to do with sending the email and getting blocked in the process?
– CesarMiguel
No, I think it’s the
FileStream
anonymous that’s causing this.– Leonel Sanches da Silva
So I have to start/create it before? Or match something before write?
– CesarMiguel
Yeah, and do the
Dispose()
shortly after.– Leonel Sanches da Silva
I’m putting the
using
and theDispose()
after closing the document. That’s what you mean?– CesarMiguel
I’d better edit the answer.
– Leonel Sanches da Silva
Let’s go continue this discussion in chat.
– CesarMiguel