IOException:进程无法访问pdf文件,因为它正由另一个进程使用

问题描述 投票:-3回答:2

我使用iTextsharp创建了PDF文件。附加到电子邮件时它会出现此错误:

IOException:进程无法访问pdf文件,因为它正由另一个进程使用。

我尝试使用dispose()和using语句来处理对象,但它没有用。

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();
                }
}
asp.net-mvc asp.net-core .net-core itext idisposable
2个回答
1
投票

这永远不会奏效:

PdfWriter writer = PdfWriter.GetInstance(myDocument, new FileStream(filename1, FileMode.Create));
builder.Attachments.Add(filename1);
System.IO.File.Delete(filename1);

您创建一个writer对象,写入文件filename1。该流需要使用iText创建的PDF字节。

目前还不清楚你在这一行中做了什么:builder.Attachments.Add(filename1);我们不知道builder是什么,但我认为它与你的问题完全无关。

但是:甚至在添加任何PDF字节之前,更具体地说:甚至在文件流关闭之前,您已经尝试删除文件filename1。这是不可能的,因为该文件仍在使用中:您从未添加任何PDF内容而您没有关闭它。

相关问题:

摘要:

该错误说明您需要在删除文件之前关闭FileStream。关闭那条小溪!


0
投票

我终于通过使用声明找到了一种方法。

FileStream类在%temp%文件夹中创建临时对象,该文件夹必须被释放,并且可以通过多种方式完成...其中一种方法是使用语句,在结束使用语句时,它会从FileStream类中释放内存。

/****************************** **My previous code** ********************************/

    /*  PdfWriter writer = PdfWriter.GetInstance(myDocument, new FileStream(filename1, FileMode.Create)); */ 

/************************************************************************************/

/****************************** **My solved code** **********************************/
 /* I instantiated FileStream in using statement and passed it's object to PdfWriter classes as done below: */

    using (FileStream fileStream = new FileStream(filename1, FileMode.Create, FileAccess.ReadWrite))
    {

       PdfWriter writer = PdfWriter.GetInstance(myDocument, fileStream);

    }

/************************************************************************************/
© www.soinside.com 2019 - 2024. All rights reserved.