开发者

How do I edit/send a pdf template in an email?

开发者 https://www.devze.com 2023-03-10 13:04 出处:网络
I have a template that I would like to make edits and send via email.I have the following code, but the edited pdf says my data is \"damaged and cannot be repaired\". I am not sure I am pulling the ed

I have a template that I would like to make edits and send via email. I have the following code, but the edited pdf says my data is "damaged and cannot be repaired". I am not sure I am pulling the edited pdf to be sent. Any help is appreciated.

   using (MemoryStream ms = new MemoryStream())  
    {    
        PdfStamper formFiller = new PdfStamper(reader, ms);
        AcroFields formFields = formFiller.AcroFields;
        formFields.SetField("Name", formData.Name);
        formFields.SetField("Location", formData.Address);
        formFields.SetField("Date", DateTime.Today.ToShortDateString());
        formFields.SetField("Email", formData.Email);
        formFiller.FormFlattening = true;
        formFiller.Close();

        MailMessage msg = new MailMessage();

        msg.To.Add(new MailAddress("to@em开发者_运维技巧ail.com"));
        msg.From = new MailAddress("from@email.com");
        msg.Subject = "Application Form";
        msg.Body = "TEST";
        msg.IsBodyHtml = true;
        ms.Position = 0;
        msg.Attachments.Add(new Attachment(ms, "Application.pdf", "application/x-pdf"));
        SmtpClient client = new SmtpClient("10.1.1.15");
        client.UseDefaultCredentials = true;
    }


I think when you finish writting data into a MemoryStream you need to reset the position of the stream to 0 before reading from it again.

Try using a FileStream instead of MemoryStream to save into a temporary file so you can narrow down the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消