This is a migrated thread and some comments may be shown as answers.

[Solved] RadGrid1 PdfExporting into memorystream

6 Answers 328 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 23 May 2013, 02:41 PM
I am able to create an excel attachment by dumping it into a memorystream and attach it to an email. 

However, when I try it as a PDF it doesn't work and throws an error that it's not a supported file type or has been damaged (for example it was sent as an email attachment and wasn't correctly decoded).

I am certain that I did not decode it correctly.  Here is what I have, the export works great, the email attachment does not.

How can I get the PDF into a memory stream to add it as an attachment to an email? 
protected void RadGrid1_PdfExporting(object sender, GridPdfExportingArgs e)
    {     
        StringBuilder customHTML = new StringBuilder();
        // customHTML.Append("<p style='color: red; font-weight: bold; font-size: 11pt;'>Offer Summary</p>");
        customHTML.Append("<table style='font-size: 10px; font-family:Arial Unicode MS;width:500px;'>");
        customHTML.Append("<colgroup><col style='width: 200px; white-space:nowrap;' /><col style='text-align:left;' /></colgroup>");
        // ColGroup = Number of <td> in a row is needed for Telerik export to PDF
        customHTML.Append("<tr><td>Customer: " + LabelCustomerName.Text + "</td><td>Agent: " + LabelAgentName.Text + " </td></tr>");
        customHTML.Append("<tr><td>Customer ID: " + LabelCustomerId.Text + "</td><td>Group ID: " + LabelGroupId.Text + " </td></tr>");
        customHTML.Append("<tr><td>Account Executive: " + LabelAccountExecutive.Text + "</td><td>Credit Status: " + LabelCreditStatus.Text + " </td></tr>");
        customHTML.Append("<tr><td>Account Manager: " + LabelAccountManager.Text + "</td><td>Review Date: " + LabelReviewDate.Text + " </td></tr>");
        customHTML.Append("</table>");
        customHTML.Append("<p></p>");
        e.RawHTML = customHTML + e.RawHTML;
 
        MemoryStream attachmentMemoryStream = new MemoryStream(new ASCIIEncoding().GetBytes(e.RawHTML));
 
        // Test Email
        SmtpClient smtpClient = new SmtpClient(Convert.ToString(ConfigurationManager.AppSettings["SERVER_EMAIL"]));
        string authType = "Basic";
        MailMessage email = new MailMessage();
        StringBuilder body = new StringBuilder();
        string emaURL = Convert.ToString(ConfigurationManager.AppSettings["BASE_URL"]);
        email.From = new MailAddress("someemail@gmail.com");
        email.CC.Add("myemail@gmail.com");
        email.Subject = "File_" + LabelCustomerName.Text + " - " + Convert.ToString(ConfigurationManager.AppSettings["SERVER_NAME"]) + " - Group " + LabelGroupId.Text;
        email.Attachments.Add(new Attachment(attachmentMemoryStream, "File_" + LabelCustomerName.Text + "_" + DateTime.Today.ToShortDateString() + ".pdf"));
        email.Body = body.ToString();
        email.IsBodyHtml = true;
        NetworkCredential nc = new NetworkCredential(RijndaelAlgorithm.Decrypt(Convert.ToString(ConfigurationManager.AppSettings["CredentialU"])), RijndaelAlgorithm.Decrypt(Convert.ToString(ConfigurationManager.AppSettings["CredentialP"])));
        smtpClient.Credentials = nc.GetCredential(Convert.ToString(smtpClient), 25, authType);
        smtpClient.Send(email);
    }

6 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 28 May 2013, 08:41 AM
Hello Mike,

Attachment class has a constructor that accepts a content type parameter. You have to specify the correct content type (application/pdf) inside.
Let me know whether this helps.

Regards,
Daniel
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Mike
Top achievements
Rank 1
answered on 28 May 2013, 12:47 PM
Daniel,

How do I set that?
"Attachment class has a constructor that accepts a content type parameter. You have to specify the correct content type (application/pdf) inside"
0
Daniel
Telerik team
answered on 28 May 2013, 01:40 PM
Hi Mike,

You can find some examples in the following link:
MSDN Attachment Class

More examples:
new Attachment(stream, "test.pdf", "application/pdf")...
 
new Attachment(stream, new ContentType() { MediaType = MediaTypeNames.Application.Pdf, Name = "test.pdf" })...

Regards,
Daniel
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Mike
Top achievements
Rank 1
answered on 28 May 2013, 01:43 PM
Thank you Daniel,

But I am already doing that and I am so close.  The grid size rows are right but there is no data! or the font is white and in Acrobat Editor is says that the font Arial Unicode MS may not display correctly
0
Daniel
Telerik team
answered on 29 May 2013, 06:33 AM
Hi Mike,

Could you please try to set the FontType property to either Embed or Subset as shown in the code-snippet below?
<ExportSettings>
    <Pdf FontType="Embed" ***OR****  FontType="Subset"></Pdf>
</ExportSettings>

Regards,
Daniel
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Mike
Top achievements
Rank 1
answered on 29 May 2013, 12:30 PM

I ended up just making an example for everyone click here

Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Mike
Top achievements
Rank 1
Share this question
or