我使用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, "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();
}
}
答案 0 :(得分:1)
这永远不会奏效:
FIRDataEventTypeChildChanged
您创建一个写入文件PdfWriter writer = PdfWriter.GetInstance(myDocument, new FileStream(filename1, FileMode.Create));
builder.Attachments.Add(filename1);
System.IO.File.Delete(filename1);
的{{1}}对象。该流需要使用iText创建的PDF字节。
目前还不清楚你在这一行中做了什么:writer
我们不知道filename1
是什么,但我认为这与你的问题完全无关。
但是:即使在添加任何PDF字节之前,甚至在文件流关闭之前,,您已经尝试删除文件builder.Attachments.Add(filename1);
。这是不可能的,因为该文件仍在使用中:您从未添加任何PDF内容而您没有关闭它。
相关问题:
<强>要点:强>
该错误说明您需要在删除文件之前关闭builder
。关闭那条小溪!
答案 1 :(得分:0)
我终于找到了使用using语句的方法。
FileStream类在%temp%文件夹中创建临时对象,该对象必须被释放,并且可以通过几种方法来完成...一种方法是通过 using语句,在使用结束时语句从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);
}
/************************************************************************************/