Click here to Skip to main content
15,905,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..Friends,

In my web application there is a page for invoicing...The invoice format is in CK Editor.

At submit button i want to save the information in database and convert the CK content in .pdf format and attach it with mail. So that i can send invoice to clients through mail as an attachment in .pdf format....


Hi...All

I have converted CK content dynamically using iTextsharp.dll through the memory stream, It converts content into pdf and successfully attach it with mail but when i download the invoice pdf from mail it downloaded but not opened. It shows the error like "There was an error opening this document. The file is damaged and could not be repaired."

I have used following code (called the function at button click) :



private void SendMail()
    {
        #region Code to convert ck content in pdf memory stream

        MemoryStream mstream = new MemoryStream();
        Document document = new Document(PageSize.A4, 20f, 20f, 20f, 20f);
        PdfWriter writer = PdfWriter.GetInstance(document, mstream);



        document.Open();
        document.Add(new Paragraph("INVOICE"));

        document.Add(new Paragraph(txt_invoice.Text));

        #endregion


        string mailTo = txt_mailTo.Text.Trim().Replace("'", "''").ToString();


        string msgSubject = txt_mailSubject.Text.ToString();
        string msgBody = "This invoice is generated for you by impact infomedia services";

        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("xxxxxxxxxxxx.in", mailTo, msgSubject, msgBody);

        mstream.Position = 0;
        mail.Attachments.Add(new Attachment(mstream, "Invoice.pdf"));

        mail.IsBodyHtml = true;
        SmtpClient client = new SmtpClient();
        client.Host = "host name is here";
        client.Port = 25;
        client.Credentials = new NetworkCredential("xxxxxxxxxxx", "xxxxxxxxxxxx");
        client.Send(mail);

        document.Close();
        mstream.Flush();
        mstream.Close();


        lbl_msg.Visible = true;
        lbl_msg.Text = "E-mail has been sent successfully";
        lbl_msg.ForeColor = System.Drawing.Color.Green;
        txt_mailSubject.Text = "";
        txt_mailTo.Text = "";
    }



Please trace my error or help me...
Posted
Updated 21-Mar-16 7:53am
v2
Comments
sonusharma65 8-Feb-14 5:24am    
I am able to open the attached pdf file but the text in pdf file is in html format.
How may i convert html text into plain text before attaching the memory stream with mail...

1 solution

you can use ITextsharp dll and use it for converting HTML to PDF.



XML
<a href="http://www.codeproject.com/Questions/341142/itextsharp-read-pdf-file">itextsharp read pdf file</a>[<a href="http://www.codeproject.com/Questions/341142/itextsharp-read-pdf-file" target="_blank" title="New Window">^</a>]
<a href="http://www.codeproject.com/Articles/686994/Create-Read-Advance-PDF-Report-using-iTextSharp-in">Create/Read Advance PDF Report using iTextSharp in C# .NET: Part I</a>[<a href="http://www.codeproject.com/Articles/686994/Create-Read-Advance-PDF-Report-using-iTextSharp-in" target="_blank" title="New Window">^</a>]
<a href="http://www.codeproject.com/Questions/645392/Extract-text-by-line-from-PDF-using-iTextSharp-csh">Extract text by line from PDF using iTextSharp c#</a>[<a href="http://www.codeproject.com/Questions/645392/Extract-text-by-line-from-PDF-using-iTextSharp-csh" target="_blank" title="New Window">^</a>]
<a href="http://www.codeproject.com/Questions/168290/PDF-Document-Reading-in-C-net-using-itext-sharp">PDF Document Reading in C#.net using itext sharp.</a>[<a href="http://www.codeproject.com/Questions/168290/PDF-Document-Reading-in-C-net-using-itext-sharp" target="_blank" title="New Window">^</a>]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900