)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: PDF |
Published Date: January 27, 2017 |
Last Revised Date: May 3, 2019

The Syncfusion HTML to PDF converter for .NET 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 using C# and VB.NET.

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 code project sample, 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 new project.

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

Install the HTML to PDF NuGet package.

  1. Copy the QtBinaries folder from the installed HtmltoPdfConverter package and paste it into the folder, which contains the EML_MSG_To_PDF_Attachments.csproj file.

Copy the QtBinaries folder.

Paste the QtBinaries folder to the project folder.

  1. Include the following namespaces in the Form1.Designer.cs file.
    // [C# Code]
    using MsgReader;
    using System.IO;
    using Syncfusion.HtmlConverter;
    using Syncfusion.Pdf;
    using Syncfusion.Pdf.Graphics;
    
  1. Use the following code snippet to convert EML or MSG to PDF with attachments and cover page.
    string filePath = Path.GetFullPath(textBox1.Text);
    //Convert EML/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 WebKit rendering engine
    if (!string.IsNullOrEmpty(files[0]))
    {
    //Initialize WebKit HTML with WebKit rendering engine
    HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
    WebKitConverterSettings settings = new WebKitConverterSettings();         
    string WebKitBinaryPath = "../../QtBinaries/";  
    //WebKit assembly path
    settings.WebKitPath = WebKitBinaryPath;           
    //Assign converter settings
    htmlConverter.ConverterSettings = settings;       
    //Convert to PDF document
    PdfDocument document = htmlConverter.Convert(files[0]);
    if (checkBox1.Checked)
    {
    //Add cover page
    PdfPage coverPage = new PdfPage();
    document.Pages.Insert(0, coverPage);               
    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);
    }
    

 

private PdfAttachment AddAttachment(FileInfo f)
{
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;
}

A complete working sample can be downloaded 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 will find other options like HTML string to PDFpartial webpage to PDFHTML to single PDF page, and HTML to PDF conversion using IE Rendering with code examples.

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

To test the accuracy of HTML to PDF conversion in .NET using C#, try Syncfusion online demo.

A code project link for Reading an Outlook MSG File.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon