【问题标题】:having issue with attaching the PDF file from memory stream using ITextSharp使用 ITextSharp 从内存流中附加 PDF 文件时遇到问题
【发布时间】:2012-08-02 09:01:52
【问题描述】:

我在附加内存中创建的 PDF 文件并将其附加到电子邮件模板时遇到问题。

电子邮件没有任何问题..但是没有附件。我不明白为什么会这样。

这是该过程的完整代码。

ExtendedEmailTemplate emailTemp = new ExtendedEmailTemplate();
emailTemp.FromAddress = "ABC Ltd <info@abcTechnology.com>";
emailTemp.ToAddress = custEmail;
emailTemp.Body = "This is an Test Email"
emailTemp.IsHTML = true;

// getting the memorystream of cretaed PDF file in memory
MemoryStream pdfStream = MWProductGuaranteedHelper.CreateProductGuaranteeCertificatePDF(custName, guranteeCode, productName);

// getting the MailMessage by passing the memorystream and attach the PDF
MailMessage emailMessage = ExtendedEmailTemplate.GenerateMailMessage(emailTemp, pdfStream);

// sending an email with by passing the (MailMessage)
emailTemp.SendGuaranteeCertificateAttachmentEmail(emailMessage);

在内存中创建 PDF

public static MemoryStream CreateProductGuaranteeCertificatePDF(string custName, string guaranteeCode, string productName)
  {
      MemoryStream memoryStream = new MemoryStream();
      string guaranteedUntil = DateTime.Now.AddYears(3).ToString("dd-MM-yyyy");

      string fromFile = Server.MapPath(guaranteeCertificateFilePath);
       PdfReader reader = new PdfReader(fromFile);
       PdfStamper stamper = new PdfStamper(reader, memoryStream);
       AcroFields fields = stamper.AcroFields;

       // AcroFields setting CODE EMITTED 

      stamper.Writer.CloseStream = false;  // making sure that stream stays open after closing the stamper
      stamper.FormFlattening = false;
      stamper.Close();
      reader.Close();

      memoryStream.Position = 0;  // reset the position of the stream, so that attachment works right
      return memoryStream;
  }

生成邮件消息

public static MailMessage GenerateMailMessage(ExtendedEmailTemplate template, MemoryStream _ms)
    {
        MailMessage mailMessage = new MailMessage();
        mailMessage.From = new MailAddress(template.FromAddress);
        mailMessage.To.Add(template.ToAddress);
        mailMessage.Subject = template.Subject;
        mailMessage.Body = template.Body;
        mailMessage.Attachments.Add(new Attachment(_ms, "ABC-Certificate.Pdf", "application/pdf"));
        mailMessage.IsBodyHtml = template.IsHTML;

        return mailMessage;
    }

发送电子邮件

public void SendGuaranteeCertificateAttachmentEmail(MailMessage _message)
    {
        EmailClient.Send(_message);
    }

 public static void Send(MailMessage mailMessage) // SMTP Settings CODE Emitted. 
        {
                //SEND THE MAIL MESSAGE
                smtpClient.Send(mailMessage);
        }

我不知道这段代码出了什么问题...电子邮件没有任何附件。

感谢您的帮助。

【问题讨论】:

    标签: c# asp.net itextsharp memorystream


    【解决方案1】:

    我看不出您做错了什么,但我正在做类似的事情,尽管通过解析网页来创建我的 pdf。 这是我的工作:

      public static Attachment GetPDfAttachmentFromUrl(string url)
            {
                string download = new WebClient().DownloadString(url);
    
                MemoryStream ms = new MemoryStream();
                Document document = new Document(PageSize.A4, 80, 50, 30, 65);
                PdfWriter writer = PdfWriter.GetInstance(document, ms);
                try
                {
                    StringReader stringReader = new StringReader(download);
                    List<IElement> parsedList = HTMLWorker.ParseToList(stringReader, null);
                    document.Open();
                    foreach (object item in parsedList)
                    {
                        document.Add((IElement)item);
                    }
                    document.Close();
                    stringReader.Close();
    
                    MemoryStream pdfstream = new MemoryStream(ms.ToArray());
    //create attachment
                    Attachment attachment = new Attachment(pdfstream, "transaction.pdf");
    
                    return attachment;
                }
                catch (Exception exc)
                {
                    Console.Error.WriteLine(exc.Message);
                }
                return null;
            }
    

    然后在一个单独的地方我像这样发送它:

      MailMessage mm = new MailMessage(from, to);
            mm.Body = body;
            mm.Subject = subject;
            mm.IsBodyHtml = true;
            mm.Attachments.Add(attachment);
            SmtpClient smtp = new SmtpClient();
            smtp.Send(mm);
    

    这有帮助吗?

    【讨论】:

    • 如果我使用 PDFWriter,这肯定会有所帮助。就我而言,我使用的是 PDFStamper..
    • 我可以看到的主要区别是我正在这样做:MemoryStream pdfstream = new MemoryStream(ms.ToArray());然后使用该内存流。也许试试看?而不是你传递给压模的那个?
    • MemoryStream pdfstream = new MemoryStream(ms.ToArray()); 是关键。很高兴能理解为什么会这样,而另一个不会......
    猜你喜欢
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2021-08-10
    • 2014-07-06
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    相关资源
    最近更新 更多