简体   繁体   English

iTextSharp使用Memorystream作为附件发送PDF

[英]iTextSharp send PDF as attachment using Memorystream

been struggling with this and have gotten so close, but not quite there. 一直在为此苦苦挣扎,距离很近,但还不完全到此。 I am trying to use iTextSharp V 4.1.2 to send a PDF as an email attachment. 我正在尝试使用iTextSharp V 4.1.2将PDF作为电子邮件附件发送。 The code I provided below is using both filestream and memorystream, filestream is there to prove to myself that the PDF template is being stamped with data (it is). 我在下面提供的代码同时使用了文件流和内存流,文件流在那里向我证明了PDF模板已经被数据加盖了。 Also, I am receiving the email, but without an attachment. 另外,我也收到电子邮件,但没有附件。 Can anyone see what I'm missing please? 谁能看到我想念的东西吗?

public class BasePDFController : BaseController

{ protected ActionResult eMailPdf(object model) { string pdf = Path.Combine(Server.MapPath("~/Infrastructure/PDFTemplates/fw9.pdf")); {受保护的ActionResult eMailPdf(对象模型){字符串pdf = Path.Combine(Server.MapPath(“〜/ Infrastructure / PDFTemplates / fw9.pdf”)); string outputFilePath = @"C:\\Projects\\Temp\\test_template_filled.pdf"; 字符串outputFilePath = @“ C:\\ Projects \\ Temp \\ test_template_filled.pdf”;

    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");
}

} }

EDIT: - yes, there is a problem with the mail provider. 编辑: -是的,邮件提供商有问题。 I don't know how to fix it though, or if this is even the best way to go about sending? 我不知道如何解决它,或者这是否是发送邮件的最佳方法? The person who wrote this obviously gave up. 写这个的人显然放弃了。 EDIT Fixed the attachment. 编辑固定附件。

    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);
    }
}

With the email fixed, I am receiving the PDF! 电子邮件固定后,我将收到PDF! However, when I try to open the PDF from email I get an error: There was an error opening this document. 但是,当我尝试从电子邮件中打开PDF时,出现错误:打开此文档时出错。 The file is damaged and could not be repaired. 该文件已损坏,无法修复。 Ideas? 想法?

You should call pdfMemoryStamper.Close() after pdfMemoryStamper.Writer.CloseStream = false; 您应该在pdfMemoryStamper.Writer.CloseStream = false;之后调用pdfMemoryStamper.Close() pdfMemoryStamper.Writer.CloseStream = false;

Like this: 像这样:

// *snip*

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

pdfMemoryStamper.Close();

memoryStream.Position = 0;

// *snip*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM