Articles in this section
Category / Section

How to convert EML or MSG to PDF?

2 mins read

The Syncfusion HTML-to-PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML files to PDF using C#.  It uses the popular rendering engine Blink (Google Chrome). 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 a PDF document using C# and VB.NET.

Steps to convert EML or MSG to PDF programmatically:

  1. Create a new C# Windows Forms application project. Create new windows forms application
  2. Install the Syncfusion.HtmlToPdfConverter.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.

  3. NuGet.png
  4. Include the following namespaces in Form1.cs file.

C#

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

 

VB.NET

Imports Syncfusion.HtmlConverter
Imports Syncfusion.Pdf
Imports MsgReader
Imports System.IO

 

  1. Use the following code example to convert EML or MSG to a PDF document.

C#

string filePath = @"../../Data/MsgReaderContent.msg";
//Convert EML/MSG to HTML using third party reader.
var msgReader = new Reader();
string tempFolder = GetTemporaryFolder();
//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();
    //Initialize the blink converter settings. 
BlinkConverterSettings settings = new BlinkConverterSettings();
//Set the page margin.
settings.Margin.All = 0;
//Assign blink converter settings to HTML converter.
htmlConverter.ConverterSettings = settings;     //Convert HTML file to PDF document.     PdfDocument document = htmlConverter.Convert(files[0]);     //Save and close the PDF document.     document.Save("Output.pdf");     document.Close(true);  }

 

VB.NET

Dim filePath As String = "../../Data/MsgReaderContent.msg"
'Convert EML/MSG to HTML using third party reader
Dim msgReader As Reader = New Reader
Dim tempFolder As String = GetTemporaryFolder()
'File contains the HTML file converted from MSG
Dim files() As String = msgReader.ExtractToFolder(filePath, tempFolder)
Dim error1 As String = msgReader.GetErrorMessage
If Not String.IsNullOrEmpty(error1) Then
      Throw New Exception(error1)
End If
'Converts the HTML to PDF using Blink rendering engine
If Not String.IsNullOrEmpty(files(0)) Then
      'Initialize HTML to PDF converter.
      Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()
'Initialize the blink converter settings.       Dim settings As BlinkConverterSettings = New BlinkConverterSettings       'Set page margins       settings.Margin.All = 0       'Assign blink converter settings to HTML converter.       htmlConverter.ConverterSettings = settings       'Convert HTML file to PDF document.       Dim document As PdfDocument = htmlConverter.Convert(files(0))       'Save and close the PDF document       document.Save("Output.pdf")       document.Close(True) End If

A complete working sample can be downloaded from EML_MSG_to_PDF.zip

By executing the program, you will get the PDF document as follows: Screenshot of output PDF document

Take a moment to peruse the documentation for converting HTML to PDF, where you will find other options for URL to PDF, preserving bookmarks, and hyperlinks, etc. 

Take a moment to peruse the documentation for converting HTML to PDF, where you will find other options for URL to PDF, preserving bookmarks, and hyperlinks, etc. 

A code project link for Reading an Outlook MSG File.

 

Conclusion

I hope you enjoyed learning about how to convert EML or MSG to PDF in WinForms.

You can refer to our WnForms FileFormat Libraries to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our demo 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
Please sign in to leave a comment
Access denied
Access denied