0

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, "saurabhnachankar@gmail.com", "", "", "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();
                }
}
Saurabh Nachankar
  • 320
  • 1
  • 2
  • 14
  • 2
    Can you show your codes? – Raptor Jan 18 '17 at 07:43
  • `PdfWriter writer = PdfWriter.GetInstance(myDocument, new FileStream(filename1, FileMode.Create)); builder.Attachments.Add(filename1); System.IO.File.Delete(filename1); ` – Saurabh Nachankar Jan 18 '17 at 07:47
  • 1
    Please [edit] your question and add the code there... – Jeremy Thompson Jan 18 '17 at 07:49
  • 2
    I didn't downvote you but its because this question doesn't show any research effort, is unclear or not useful. Read over [ask] to write better questions, things like including your code, what you've tried, etc. Also, unless you release the lock on your file you cant "free your pdf from the previous process". You should instead detect if the file is locked and then take action, see here http://stackoverflow.com/a/11060322/495455 – Jeremy Thompson Jan 18 '17 at 07:53
  • 1
    You are getting bad reputation because you didn't put your code in your question, you added it as a comment instead. Edit your question, add your code, and chances are that the downvoters will undo it. Or not. Meanwhile I flagged your "why the h***" comment as not constructive. – Amedee Van Gasse Jan 18 '17 at 07:53
  • 2
    Show us the code where you close your PDF file *after* you are done writing to it and *before* you send or delete it. – Amedee Van Gasse Jan 18 '17 at 07:59
  • Thanks for commenting my queries and my writing skills. – Saurabh Nachankar Sep 07 '18 at 05:16

2 Answers2

2

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");
            }
        }
Saurabh Nachankar
  • 320
  • 1
  • 2
  • 14
0

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!

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165