Spire.Email for .NET is a professional .NET Email library specially designed for developers to create, read and manipulate emails from any .NET (C#, VB.NET, ASP.NET) platform with fast and high quality performance.

Mon Dec 12, 2022 2:25 am

Hello,
I am trying to convert a .eml file to pdf. Does your product support this in .net c#? If so, can you please provide directions or some sample code to achieve this.
The goal is to convert email to pdf and any other attachments to pdf and merge all this into one pdf. Any suggestions or help regarding this would be appreciated.

Thanks

rakeshjaidi
 
Posts: 2
Joined: Mon Dec 12, 2022 2:21 am

Mon Dec 12, 2022 9:18 am

Hi,

Thanks for your inquiry.
Please refer to the following code to achieve your requirement.
Code: Select all
            MailMessage mail = MailMessage.Load("Sample.eml");
            string textBody = mail.BodyHtml;
            Document doc = new Document();
            Section section = doc.AddSection();
            Paragraph p = section.AddParagraph();
            p.AppendHTML(textBody);
            string result = "result.pdf";
            doc.SaveToFile(result, FileFormat.PDF);

            string[] list = new string[mail.Attachments.Count];
            int i = 0;
            foreach (Attachment attach in mail.Attachments)
            {
           
                string fileName = attach.ContentType.Name;
                if (fileName.Contains(".docx"))
                {
                    Document doc2 = new Document();
                    doc2.LoadFromStream(attach.Data, FileFormat.Docx);
                    doc2.SaveToFile(fileName,FileFormat.PDF);
                    list[i++] = fileName;
                }

                if (fileName.Contains(".xlsx"))
                {
                    Workbook wb = new Workbook();
                    wb.LoadFromStream(attach.Data);
                    wb.SaveToFile(fileName, Spire.Xls.FileFormat.PDF);
                    list[i++] = fileName;
                }

                if (fileName.Contains(".pdf"))
                {
                    PdfDocument pdf = new PdfDocument();
                    pdf.LoadFromStream(attach.Data);
                    pdf.SaveToFile(fileName, Spire.Pdf.FileFormat.PDF);
                    list[i++] = fileName;
                }

            }


            PdfDocumentBase doc3 = PdfDocument.MergeFiles(list);
            doc3.Save("mergeOutput.pdf",Spire.Pdf.FileFormat.PDF);


If you have any questions, just feel free to contact us.

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Dec 12, 2022 2:41 pm

Hello Triste,
Thank you for the quick reply. This helps. I am looking for something generic to convert the attachments to pdf instead of checking by the doc types. At this point, the attachments can be any type of doc. If I go by your approach and I receive a doc type that I didn't code for then I will be missing that doc. Just want to make sure I covered all my bases.

rakeshjaidi
 
Posts: 2
Joined: Mon Dec 12, 2022 2:21 am

Tue Dec 13, 2022 7:50 am

rakeshjaidi wrote:Hello Triste,
Thank you for the quick reply. This helps. I am looking for something generic to convert the attachments to pdf instead of checking by the doc types. At this point, the attachments can be any type of doc. If I go by your approach and I receive a doc type that I didn't code for then I will be missing that doc. Just want to make sure I covered all my bases.


Hi,

Thanks for your feedback.
Our products don’t provide such a function. Since the attachments include various of formats, some are not able to be converted or be compatible with, when you try to merge, our products do not know the type of the document, obviously, you are supposed to handle it manually.
If you have any questions, just feel free to contact us.

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Email