Convert Emails (MSG/EML) to PDF in C#
This article explains how to convert email files such as MSG and EML to PDF.
GdPicture.NET’s conversion from email files to PDFs offers the following benefits:
-
Conversion from EML and MSG files to PDF without external libraries or dependencies such as Microsoft Office interop.
-
Support for embedding email attachments into the output PDF or PDF/A document as PDF attachment annotations.
-
Outstanding Unicode support.
-
Support for documents with mixed encoding and mixed right-to-left (RTL) and left-to-right (LTR) text.
This guide uses MSG files as an example. The process is the same for EML files.
Creating a PDF from an Email File
To create a PDF from an MSG file, follow these steps:
-
Create a
GdPictureDocumentConverter
object. -
Load the source document by passing its path to the
LoadFromFile
method. Recommended: Specify the source document format with a member of theDocumentFormat
enumeration. -
Save the output in a new PDF document with the
SaveAsPDF
method.
The example below creates a PDF document from an MSG file:
using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter(); // Load the source document. gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.msg", GdPicture14.DocumentFormat.DocumentFormatMSG); // Save the output in a new PDF document. gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");
Using gdpictureDocumentConverter As GdPictureDocumentConverter = New GdPictureDocumentConverter() ' Load the source document. gdpictureDocumentConverter.LoadFromFile("C:\temp\source.msg", GdPicture14.DocumentFormat.DocumentFormatMSG) ' Save the output in a new PDF document. gdpictureDocumentConverter.SaveAsPDF("C:\temp\output.pdf") End Using
Used Methods
Related Topics
Email attachments are automatically added to the output document as PDF attachment annotations.
To convert an email file to PDF/A for long-term preservation, specify the conformance level of the output PDF/A document by passing a member of the PdfConformance
enumeration to the SaveAsPDF
method.
Optional Configuration Properties
Optionally, configure the conversion with the following properties of the GdPictureDocumentConverter
object:
-
PdfEnableLinearization
is a Boolean value that specifies whether to linearize the output PDF to enable Fast Web View mode.
The example below creates a PDF document from an MSG file with a custom configuration:
using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter(); // Load the source document. gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.msg", GdPicture14.DocumentFormat.DocumentFormatMSG); // Configure the conversion. gdpictureDocumentConverter.EmailPreferOnePage = true; // Save the output in a new PDF document. gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");
Using gdpictureDocumentConverter As GdPictureDocumentConverter = New GdPictureDocumentConverter() ' Load the source document. gdpictureDocumentConverter.LoadFromFile("C:\temp\source.msg", GdPicture14.DocumentFormat.DocumentFormatMSG); ' Configure the conversion. gdpictureDocumentConverter.EmailPreferOnePage = True ' Save the output in a new PDF document. gdpictureDocumentConverter.SaveAsPDF("C:\temp\output.pdf") End Using