I am trying to create a web form that users can fill out that will in-turn populate the fields in a PDF and email it to someone. I'm not using the FireMail, I'm just trying to do it using System.Net.Mail. I can get the form to populate the PDF fine when I display it on screen, but when I use the MemoryStream and attach it to the email, I get a blank pdf in the email.
Any ideas?
Here's my code:
MemoryStream outputStream = new MemoryStream();
// Create a document and set it's properties
MergeDocument document = new MergeDocument(MapPath("test2.pdf"));
document.Creator = "AcroFormFill.aspx";
document.Author = "Test";
document.Title = "Test Filler";
// Add content to the page
document.Form.Fields["fullname"].Value = txtfullname.Text;
document.Form.Fields["ssn"].Value = txtssn.Text;
document.Form.Fields["address"].Value = txtaddress.Text;
document.Form.Fields["phone"].Value = txtphone.Text;
document.Form.Fields["city"].Value = txtcity.Text;
document.Form.Fields["state"].Value = txtstate.Text;
document.Form.Fields["zip"].Value = txtzip.Text;
document.Form.Fields["email"].Value = txtemail.Text;
document.Form.Fields["major"].Value = txtmajor.Text;
document.Form.Fields["gradyear"].Value = txtgradyear.Text;
document.Form.Fields["gpa"].Value = txtgpa.Text;
// Uncomment to make form read only
document.Form.IsReadOnly = true;
// Outputs the W-9 to the current web page
// Email the form to someone...
// Create a memory stream object to save to
// Output the PDF to the MemoryStream
document.Draw(outputStream);
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("another@domain.com");
mail.To.Add("me@mydomain.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this content is in the body";
//create the attachment from a stream. Be sure to name the data with a file and
//media type that is respective of the data
mail.Attachments.Add(new Attachment( outputStream, "application.pdf", "application/pdf" ));
//send the message
SmtpClient smtp = new SmtpClient("mailserver");
smtp.Send(mail);
Response.End();
MemoryStream
Re: MemoryStream
Support · Dec 13, 2006Hello,
When you say you are getting the blank PDF, do you see the PDF document with empty form fields? If yes, you can set the document.Form.NeedsAppearances property equal to true right before calling the Draw method and that will fix your problem regarding viewing the filled forms.
document.Form.NeedsAppearances = true;
Thanks,
ceTe Software Support Team
Re: MemoryStream
Forum member · Dec 13, 2006Actually I'm recieving the PDF, but it is only 242 bytes in size and when I try to open the file, I get the error message:
Acrobat could not open 'application.pdf' because it is either not a supported file type or because the file has been damaged.
Re: MemoryStream
Support · Dec 13, 2006Hello,
I just realized that you will need to use the "Draw" method and save the PDF to an actual location on disk. Saving the file to an actual location on disk is required as the .NET Framework's emailing functionality does not have the ability to attach a file from memory.
You can however use our FireMail for .NET product. It will allow you to attach a file from a memory stream.
Thanks,
ceTe Software Support Team
Re: MemoryStream
Forum member · Dec 14, 2006Worked like a charm. :-) Thanks.
Re: MemoryStream
Forum member · Jul 17, 2008Actually you don't have to write it to disk.
Dim stream As System.IO.Stream
'Create your PDF
stream = New System.IO.MemoryStream(PDFOutputDoc.Draw())
'
Mailmsg.Attachments.Add(New System.Net.Mail.Attachment(stream, "MyFile.pdf", System.Net.Mime.MediaTypeNames.Application.Pdf))
Then send.