iTextSharp使用Memorystream将PDF作为附件发送

时间:2014-04-18 20:09:18

标签: c# asp.net-mvc itextsharp

一直在努力争取这个并且已经如此接近,但并不完全存在。我正在尝试使用iTextSharp V 4.1.2将PDF作为电子邮件附件发送。我在下面提供的代码是同时使用文件流和内存流,文件流可以向我自己证明PDF模板正在加盖数据(它是)。此外,我收到了电子邮件,但没有附件。有人能看到我想念的东西吗?

public class BasePDFController : BaseController

{     protected ActionResult eMailPdf(对象模型)     {         string pdf = Path.Combine(Server.MapPath("〜/ Infrastructure / PDFTemplates / fw9.pdf"));         string outputFilePath = @" C:\ Projects \ Temp \ test_template_filled.pdf&#34 ;;

    MemoryStream memoryStream = new MemoryStream();

    PdfReader pdfFileReader = null;
    PdfReader pdfMemoryReader = null;

    try
    {
        pdfFileReader = new PdfReader(pdf);
        pdfMemoryReader = new PdfReader(pdf);

        using (FileStream pdfOutputFile = new FileStream(outputFilePath, FileMode.Create))
        {
            PdfStamper pdfFileStamper = null;
            PdfStamper pdfMemoryStamper = null;
            try
            {
                pdfFileStamper = new PdfStamper(pdfFileReader, pdfOutputFile);
                pdfMemoryStamper = new PdfStamper(pdfMemoryReader, memoryStream);

                AcroFields acroFileFields = pdfFileStamper.AcroFields;
                AcroFields acroMemoryFields = pdfMemoryStamper.AcroFields;

                acroFileFields.SetField("topmostSubform[0].Page1[0].f1_01_0_[0]", "Batman");

                pdfFileStamper.FormFlattening = true;
                pdfMemoryStamper.FormFlattening = true;
                pdfMemoryStamper.Writer.CloseStream = false;
                if (pdfMemoryStamper != null)
                {
                    pdfMemoryStamper.Close();
                }
                memoryStream.Position = 0;

                EmailProvider.Email email = new EmailProvider.Email();
                email = new EmailProvider.Email
                {
                    To = "someone@gamil.net",
                    Subject = "Scholars Attached PDF",
                    Body = "A PDF!",
                    Attachment = new Attachment(memoryStream, new System.Net.Mime.ContentType("application/pdf"))
                };
                EmailProvider.SendEmail(email);                    
                }
            finally
            {
                if (pdfFileStamper != null)
                {
                    pdfFileStamper.Close();
                }
            }
        }
    }
    finally
    {
        pdfFileReader.Close();
        pdfMemoryReader.Close();
    }
    return File(outputFilePath, "application/pdf", "Returned.pdf");
}

}

修改   - 是的,邮件提供商有问题。我不知道如何修复它,或者这是否是最好的发送方式?写这篇文章的人显然放弃了。 的修改 修复了附件。

    public static class EmailProvider
{
    public class Email
    {
        public String To { get; set; }

        public String Subject { get; set; }

        public String Body { get; set; }

        **public Attachment Attachment { get; set; }**
    }

    public static void SendEmail(Email email)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(email.To);
        mail.Subject = email.Subject;
        mail.Body = email.Body;
        mail.IsBodyHtml = true;
        mail.Attachments.Add(email.Attachment);
        SmtpClient smtp = new SmtpClient();

        smtp.Send(mail);
    }
}

随着电子邮件的修复,我收到了PDF!但是,当我尝试从电子邮件中打开PDF时出现错误: 打开此文档时出错。文件已损坏,无法修复。 想法?

1 个答案:

答案 0 :(得分:2)

您应该在pdfMemoryStamper.Close()

之后致电pdfMemoryStamper.Writer.CloseStream = false;

像这样:

// *snip*

pdfFileStamper.FormFlattening = true;
pdfMemoryStamper.FormFlattening = true;
pdfMemoryStamper.Writer.CloseStream = false;

pdfMemoryStamper.Close();

memoryStream.Position = 0;

// *snip*