其使用iTextSharp的从内存流附加PDF文件问题 [英] having issue with attaching the PDF file from memory stream using ITextSharp

查看:202
本文介绍了其使用iTextSharp的从内存流附加PDF文件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有附加的PDF文件在内存中创建和连接它以电子邮件模板的问题。

I am having problem with attaching the PDF file created in-memory and attaching it to email template.

电子邮件不用任何problem..BUT没有附件。我不明白为什么会这样。

email goes without any problem..BUT there is no attachment. I don't understand why this is happening.

下面是完整的code的过程。

here is the complete code for the process.

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


MAILMESSAGE生成

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

我不知道这个code去错了...电子邮件不用任何附件。

I don't know what going wrong with this code...email goes without any attachment.

帮助AP preciated。

help appreciated.

推荐答案

我不明白你在做什么错了,但我做类似的东西,虽然通过解析一个网页创建我的PDF。
这是我做的:

I can't see what you are doing wrong but I am doing something similar, although create my pdf by parsing a web page. Here's what I do:

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

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

Then in a separate place I send it like so:

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

这是否帮助呢?

这篇关于其使用iTextSharp的从内存流附加PDF文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆