尝试使用 PdfStamper 和 MemoryStream 将数据添加到现有 PDF,然后通过电子邮件发送
Trying to use PdfStamper and MemoryStream to add data to existing PDF then email it
这是我的代码块。它编译得很好,当我触发事件时,我收到了电子邮件,但随后我收到了这个错误
打开时 Adobe 上的电子邮件附件错误(Acrobat 无法打开 'Att00002.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("myemail@myemail.com", "mjones@globusfamily.com", null, "", "Avalon Cert", "Hope this works", EmailHelper.EmailFormat.Html,attach);
}
已编辑 *************************************
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);
}
您正在 using (PdfStamper stamper = new PdfStamper(reader, _output))
中调用 stamper.close()
。 using
除了手动关闭 () 之外,还会在退出压模时自动关闭压模,因此从技术上讲,压模试图关闭两次。因此,它还不止一次尝试关闭 MemoryStream
。这就是异常的来源。
我会为您的 MemoryStream
和 PdfStamper
使用此处答案中的技术(修改并取自:Getting PdfStamper to work with MemoryStreams (c#, itextsharp)):
using (MemoryStream _output = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, _output)) {
// do stuff
}
}
这是我的代码块。它编译得很好,当我触发事件时,我收到了电子邮件,但随后我收到了这个错误 打开时 Adobe 上的电子邮件附件错误(Acrobat 无法打开 'Att00002.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("myemail@myemail.com", "mjones@globusfamily.com", null, "", "Avalon Cert", "Hope this works", EmailHelper.EmailFormat.Html,attach);
}
已编辑 *************************************
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);
}
您正在 using (PdfStamper stamper = new PdfStamper(reader, _output))
中调用 stamper.close()
。 using
除了手动关闭 () 之外,还会在退出压模时自动关闭压模,因此从技术上讲,压模试图关闭两次。因此,它还不止一次尝试关闭 MemoryStream
。这就是异常的来源。
我会为您的 MemoryStream
和 PdfStamper
使用此处答案中的技术(修改并取自:Getting PdfStamper to work with MemoryStreams (c#, itextsharp)):
using (MemoryStream _output = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, _output)) {
// do stuff
}
}