Articles in this section
Category / Section

How to convert EML or MSG to PDF with attachment and cover page?

5 mins read

The Syncfusion HTML to PDF converter for .NET PDF Library is used to convert webpages, SVG, MHTML, and HTML to PDF using C#. It is reliable and accurate. The result preserves all graphics, images, text, fonts, and the layout of the original HTML document or webpage.

Using this library, you can convert EML or MSG to PDF with attachments and you can add cover page to the PDF. The EML or MSG files can be converted to HTML using the third-party reader, then Syncfusion HTML converter can be used to convert HTML to PDF.

Steps to convert EML or MSG to PDF programmatically:

  1. Create a new C# Windows Forms application project.

Create a new Windows Forms application project.

  1. Install the Syncfusion.HtmlToPdfConverter.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

NuGet package reference

  1. Include the following namespaces in Form1.cs file.

C#

using MsgReader;
using System.IO;
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;

 

  1. Use the following code snippet to convert EML or MSG to PDF document with attachments and cover page.

C#

//Convert EML or MSG to HTML using third party reader
var msgReader = new Reader();
string tempFolder = Path.GetTempPath();
 
//File contains the HTML file converted from MSG
var files = msgReader.ExtractToFolder(filePath, tempFolder);
var error = msgReader.GetErrorMessage();
 
if (!string.IsNullOrEmpty(error))
    throw new Exception(error);
 
//Converts the HTML to PDF using Blink rendering engine
if (!string.IsNullOrEmpty(files[0]))
{
    //Initialize HTML to PDF converter
    HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
    
    //Convert HTML to PDF document
    PdfDocument document = htmlConverter.Convert(files[0]);
 
    if (checkBox1.Checked)
    {
        //Add cover page
        PdfPage coverPage = new PdfPage();
 
        //Insert page 
        document.Pages.Insert(0, coverPage);
        
        //Draw text in cover page 
        PdfFont font = new PdfTrueTypeFont(new Font("Helvetica", 16, FontStyle.Bold), true);
        PdfSolidBrush brush = new PdfSolidBrush(Color.FromArgb(44, 71, 120));
        coverPage.Graphics.DrawString("cover page", font, brush, new PointF());
    }
    if (checkBox2.Checked)
    {
        //Insert attachments
        for (int i = 1; i < files.Length; i++)
        {
            FileInfo file = new FileInfo(files[i]);
            document.Attachments.Add(AddAttachment(file));
        }
    }
    
    //Save and close the document
    document.Save("Output.pdf");
    document.Close(true);
 
    //Message box confirmation to view the created PDF document
    if (MessageBox.Show("Do you want to view the PDF file?", "PDF File Created",
        MessageBoxButtons.YesNo, MessageBoxIcon.Information)
        == DialogResult.Yes)
    {
        //Launching the PDF file using the default Application.[Acrobat Reader]
        System.Diagnostics.Process.Start("Output.pdf");
        this.Close();
    }
    else
    {
        // Exit
        this.Close();
    }
}

 

private PdfAttachment AddAttachment(FileInfo f)
{
    //Create attachment 
    PdfAttachment attachment = new PdfAttachment(f.FullName);
    attachment.ModificationDate = DateTime.Now;
    attachment.Description = f.Name;
    attachment.MimeType = "application/" + f.Extension;
 
    //Returns the attachment to the document
    return attachment;
}

 

You can download the work sample from EML_MSG_To_PDF_Attachments.zip.

By executing the program, you will get the PDF document as follows,

Output PDF screenshot with cover page and attachments.

Take a moment to peruse the documentation, where you can find converting HTML pages to PDF document along with respective customization options and features.

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link for Converting HTML to PDF.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Conclusion

I hope you enjoyed learning about how to convert EML or MSG to PDF with attachment and cover page in WF.

You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied