iTextSharp - MemoryStream problem sending pdf as attachment

Last post 11-11-2009 6:21 PM by soulfly1973. 3 replies.

Sort Posts:

  • iTextSharp - MemoryStream problem sending pdf as attachment

    03-29-2007, 11:24 PM
    • Member
      3 point Member
    • artmasa
    • Member since 02-20-2007, 4:09 AM
    • Dallas, Texas, US
    • Posts 15

    Can somebody help me with this.

    Ive been trying to do something and no success. It seems like the stream is not well formed and I receive an error in adobe reader that says the file is corrupted.

     Here is my code:

    1    string formFile = this.Server.MapPath(@"\library\doctemp\") + "cmoinmail.pdf";
    2    PdfReader reader = new PdfReader(formFile);
    3    MemoryStream ms = new MemoryStream();
    4    PdfStamper stamper = new PdfStamper(reader, fs);
    5    AcroFields fields = stamper.AcroFields;
    6                            
    7                            
    8    fields.SetField("subtotal", this.Order.SubTotal.ToString());
    9    fields.SetField("discount", this.Order.Discount.ToString());
    10  fields.SetField("tax", this.Order.Tax.ToString());
    11  fields.SetField("shipping", this.Order.Shipping.ToString());
    12  fields.SetField("total", this.Order.Total.ToString());
    13   
    14  stamper.Writer.CloseStream = false;
    15                           
    16  stamper.FormFlattening = true;
    17  stamper.Close(); 
    18   
    19  MailAddress maFrom = new MailAddress("some@domain.com")
    20  MailAddress maTo = new MailAddress(another@domain.com);
    21  MailMessage email = new MailMessage(maFrom, maTo);
    22  email.Subject = "Your new order at EvanTell";
    23  Attachment attch = new Attachment(ms, new System.Net.Mime.ContentType("application/pdf"));
    24  email.Attachments.Add(attch);
    25   
    26  SmtpClient mailSender = new SmtpClient("server");
    27  mailSender.Send(email);
    
     Thanks for your help in advance
    Filed under:
  • Re: iTextSharp - MemoryStream problem sending pdf as attachment

    04-03-2007, 8:20 AM
    Answer
    • Member
      12 point Member
    • mcools
    • Member since 04-03-2007, 12:16 PM
    • Posts 1

    Hi!

     

    I was struggling with this exact problem, and ultimately I found a solution. The position of the memory stream needs to be reset before the attachment is made, so you need to insert 

    ms.Position = 0
    (sorry, don't know the C# equivalent... Probably ms.position(0) or something) between lines 22 and 23 in your source code, and possibly flush the stream.
     
    Hope this helps! 
      
  • Re: iTextSharp - MemoryStream problem sending pdf as attachment

    05-15-2007, 4:23 PM
    • Member
      3 point Member
    • artmasa
    • Member since 02-20-2007, 4:09 AM
    • Dallas, Texas, US
    • Posts 15

    Thanks for your reply

    This is a great solution

  • Re: iTextSharp - MemoryStream problem sending pdf as attachment

    11-11-2009, 6:21 PM
    • Member
      27 point Member
    • soulfly1973
    • Member since 08-17-2009, 5:46 AM
    • Posts 89

    Hi there,

    does anyone no the equalivent of writer.CloseStream  for the pdfwriter in vb.net?

     

    regards,

     

    peter

     

     

     

     

Page 1 of 1 (4 items)