【问题标题】:How do I edit/send a pdf template in an email?如何在电子邮件中编辑/发送 pdf 模板?
【发布时间】:2011-06-02 19:27:35
【问题描述】:

我有一个模板,我想对其进行编辑并通过电子邮件发送。我有以下代码,但编辑后的 ​​pdf 显示我的数据“已损坏且无法修复”。我不确定我是否正在提取要发送的编辑后的 ​​pdf。任何帮助表示赞赏。

   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@email.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;
    }

【问题讨论】:

  • 如果去掉邮件逻辑,直接编辑保存PDF,还损坏吗?
  • 我想这就是我想做的……但我不知道该怎么做。

标签: c# .net asp.net email pdf


【解决方案1】:

我认为,当您将数据写入 MemoryStream 后,您需要将流的位置重置为 0,然后再再次读取。

尝试使用 FileStream 而不是 MemoryStream 将其保存到临时文件中,以便缩小问题范围。

【讨论】:

  • 我有 ms.Position = 0;不错的收获,但不幸的是不是这样:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 2019-09-06
  • 2022-08-17
相关资源
最近更新 更多