这是我的代码块。它编译得很好,当我关闭事件我收到电子邮件,但我然后得到这个错误 电子邮件附件错误在Adobe打开时(Acrobat无法打开'Att00002.pdf',因为它不是支持的文件类型或因为该文件已被损坏(例如,它被发送作为电子邮件的附件和wasnt正确地解码。)试图使用PdfStamper和MemoryStream将数据添加到现有的PDF然后发电子邮件
string agentName = "My Name";
MemoryStream _output = new MemoryStream();
PdfReader reader = new PdfReader("/pdf/Agent/Specialist_Certificate.pdf");
using (PdfStamper stamper = new PdfStamper(reader, _output))
{
AcroFields fields = stamper.AcroFields;
// set form fields
fields.SetField("FIELD_AGENT_NAME", agentName);
fields.SetField("FIELD_DATE", AvalonDate);
// flatten form fields and close document
stamper.FormFlattening = true;
SendEmail(_output);
DownloadAsPDF(_output);
stamper.Close();
}
private void SendEmail(MemoryStream ms)
{
Attachment attach = new Attachment(ms, new System.Net.Mime.ContentType("application/pdf"));
EmailHelper.SendEMail("[email protected]", "[email protected]", null, "", "Avalon Cert", "Hope this works", EmailHelper.EmailFormat.Html,attach);
}
EDITED ******************* ******************
using (MemoryStream _output = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(reader, _output))
{
AcroFields fields = stamper.AcroFields;
// set form fields
fields.SetField("FIELD_AGENT_NAME", agentName);
fields.SetField("FIELD_DATE", AvalonDate);
// flatten form fields and close document
stamper.FormFlattening = true;
}
SendEmail(_output);
}
尝试调用'SendEmail'之前关闭'PdfStamper'。您可能只附加了PDF字节流的一部分,因为并非所有内容都被刷新为输出。 – rhens
当我将其更改为stamper.Close(); SendEmail(_output);发送电子邮件时出现此错误 System.ObjectDisposedException:无法访问已关闭的流 – user1566783