Reputation: 330
I have created PDF file using iTextsharp. while attaching to the email it gives this error :
IOException: The process cannot access the pdf file because it is being used by another process.
I tried dispose() and using statement to dispose the object but it didn't worked.
var font8 = FontFactory.GetFont(FontFactory.HELVETICA, 8);
var font9 = FontFactory.GetFont(FontFactory.HELVETICA, 9);
var boldfont8 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8);
var boldfont9 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 9);
var boldfont10 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10);
bool pageset = false;
Document myDocument = null;
var mypagesize = new iTextSharp.text.Rectangle(595f, 421f);
myDocument = new Document(mypagesize, 36, 36, 24, 24);
PdfWriter writer = PdfWriter.GetInstance(myDocument, new FileStream(filename1, FileMode.Create));
PdfPTable tablehead = new PdfPTable(1);
tablehead.TotalWidth = 530f;
tablehead.LockedWidth = true;
float[] widthshead = new float[] { 1f };
tablehead.SetWidths(widthshead);
tablehead.SpacingBefore = 2f;
myDocument.Open();
if (email == "email")
{
makeslip(myDocument, _payslip, _payroll.date2, notes);
myDocument.Close();
((IDisposable)myDocument).Dispose(); // tried this but didn't work
EmailController Sendmail = new EmailController(_contextAccessor, _env);
Sendmail.SendEmail(1, "[email protected]", "", "", "TESTSubject", "TEST", filename1);
}
// Email Method
public IActionResult SendEmail(int id, string Email1, string Email2, string Email3, string EmailSubject, string EmailMessage, [Optional] string filename1)
{
mailMessage.Subject = EmailSubject;
mailMessage.Body = EmailMessage;
mailMessage.IsBodyHtml = true;
if (filename1 != null)
{
Attachment data = new Attachment(filename1); // At this point it is giving IOException
mailMessage.Attachments.Add(data);
client.Send(mailMessage);
data.Dispose();
}
}
Upvotes: 0
Views: 2090
Reputation: 330
I finally found a way through using statement.
public ActionResult test()
{
using (FileStream fileStream = new FileStream("TESTPDF", FileMode.Create, FileAccess.ReadWrite))
{
return File(fileStream, "application/pdf", "abc.pdf");
}
}
Upvotes: 2
Reputation: 77546
This can never work:
PdfWriter writer = PdfWriter.GetInstance(myDocument, new FileStream(filename1, FileMode.Create));
builder.Attachments.Add(filename1);
System.IO.File.Delete(filename1);
You create a writer
object that writes to a file filename1
. That stream expects PDF bytes created with iText.
It is unclear what you do in this line: builder.Attachments.Add(filename1);
We have no idea what builder
is about, but I assume that it is totally irrelevant to your question.
However: even before you have added any PDF bytes, and more specifically: even before the file stream is closed, you already try to delete the file filename1
. That isn't possible because that file is still in use: you never added any PDF content and you didn't close it.
Related questions:
Summary:
The error explains that you need to close the FileStream
before you delete the file. Close that stream!
Upvotes: 0