ByteScout PDF Suite - C# - Convert text email to pdf with pdf sdk - ByteScout

ByteScout PDF Suite – C# – Convert text email to pdf with pdf sdk

  • Home
  • /
  • Articles
  • /
  • ByteScout PDF Suite – C# – Convert text email to pdf with pdf sdk

How to convert text email to pdf with pdf sdk in C# and ByteScout PDF Suite

Learn to convert text email to pdf with pdf sdk in C#

An easy to understand sample source code to learn how to convert text email to pdf with pdf sdk in C# ByteScout PDF Suite: the bundle that provides six different SDK libraries to work with PDF from generating rich PDF reports to extracting data from PDF documents and converting them to HTML. This bundle includes PDF (Generator) SDK, PDF Renderer SDK, PDF Extractor SDK, PDF to HTML SDK, PDF Viewer SDK and PDF Generator SDK for Javascript. It can convert text email to pdf with pdf sdk in C#.

This prolific sample source code in C# for ByteScout PDF Suite contains various functions and other necessary options you should do calling the API to convert text email to pdf with pdf sdk. This C# sample code is all you need for your app. Just copy and paste the code, add references (if needs to) and you are all set! Enjoy writing a code with ready-to-use sample C# codes.

If you want to try other source code samples then the free trial version of ByteScout PDF Suite is available for download from our website. Just try other source code samples for C#.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

Program.cs
      
using Bytescout.PDF.Converters; using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing.Printing; using System.Text; namespace EmailToPDF_TextEmail { class Program { static void Main(string[] args) { try { Console.WriteLine("Please wait while PDF is being created..."); // Parse MessageContents using MsgReader Library // MsgReader library can be obtained from: https://github.com/Sicos1977/MSGReader using (var msg = new MsgReader.Outlook.Storage.Message("TxtSampleEmail.msg")) { // Get Sender information var from = msg.GetEmailSender(false, false); // Message sent datetime var sentOn = msg.SentOn; // Recipient To information var recipientsTo = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.To, false, false); // Recipient CC information var recipientsCc = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.Cc, false, false); #region Generate and save html // Get Html HtmlGenerator oHtmlGenerator = new HtmlGenerator(); oHtmlGenerator.Title = {code}quot;Subject: {msg.Subject}"; oHtmlGenerator.AddParagraphBodyItem({code}quot;File Name: {msg.FileName}"); oHtmlGenerator.AddParagraphBodyItem({code}quot;From: {from}"); oHtmlGenerator.AddParagraphBodyItem({code}quot;Sent On: {(sentOn.HasValue ? sentOn.Value.ToString("MM/dd/yyyy HH:mm") : "")}"); oHtmlGenerator.AddParagraphBodyItem({code}quot;To: {recipientsTo}"); oHtmlGenerator.AddParagraphBodyItem({code}quot;Subject: {msg.Subject}"); if (!string.IsNullOrEmpty(recipientsCc)) { oHtmlGenerator.AddParagraphBodyItem({code}quot;CC: {recipientsCc}"); } oHtmlGenerator.AddRawBodyItem("<hr/>"); var msgBodySplitted = msg.BodyText.Split("\n".ToCharArray()); foreach (var itmBody in msgBodySplitted) { oHtmlGenerator.AddParagraphBodyItem(itmBody); } // Generate Html oHtmlGenerator.SaveHtml("result.html"); #endregion using (HtmlToPdfConverter converter = new HtmlToPdfConverter()) { converter.PageSize = PaperKind.A4; converter.Orientation = Bytescout.PDF.Converters.PaperOrientation.Portrait; converter.ConvertHtmlToPdf("result.html", "result.pdf"); // Open result document in default associated application (for demo purpose) ProcessStartInfo processStartInfo = new ProcessStartInfo("result.pdf"); processStartInfo.UseShellExecute = true; Process.Start(processStartInfo); } } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("Press enter key to exit..."); Console.ReadLine(); } } } /// <summary> /// Html Generator class /// </summary> class HtmlGenerator { #region Constructor public HtmlGenerator() { this.StrBodyItems = new List<string>(); } #endregion #region Variable Declarations public string Title { get; set; } private List<string> StrBodyItems { get; set; } #endregion #region Methods /// <summary> /// Add Body Item /// </summary> public void AddRawBodyItem(string strBodyItem) { StrBodyItems.Add(strBodyItem); } /// <summary> /// Add Paragraph body item /// </summary> /// <param name="strText"></param> public void AddParagraphBodyItem(string strText) { StrBodyItems.Add(string.Format("<p style=''>{0}</p>", strText)); } /// <summary> /// Gets Generated Html /// </summary> /// <returns></returns> public string GetHtml() { StringBuilder oRetHtml = new StringBuilder(string.Empty); oRetHtml.Append("<html>"); oRetHtml.Append("<head>"); oRetHtml.AppendFormat("<title>{0}</title>", Title); oRetHtml.Append(@"<style>p { line-height: 107.9 %; margin-bottom: 13pt; font-family: 'Arial', 'sans-serif'; font-size: 15pt; margin-top: 0; margin-left: 0; margin-right: 0; }</style>"); oRetHtml.Append("</head>"); oRetHtml.Append("<body>"); foreach (var itemBody in StrBodyItems) { oRetHtml.Append(itemBody); } oRetHtml.Append("</body>"); oRetHtml.Append("</html>"); return oRetHtml.ToString(); } /// <summary> /// Save all Html /// </summary> /// <param name="fileName"></param> public void SaveHtml(string fileName) { var allHtml = GetHtml(); System.IO.File.WriteAllText(fileName, allHtml); } #endregion } }

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF Suite Home Page

Explore ByteScout PDF Suite Documentation

Explore Samples

Sign Up for ByteScout PDF Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

packages.config
      
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="MsgReader" version="3.4.0" targetFramework="net45" /> <package id="OpenMcdf" version="2.2.1.3" targetFramework="net45" /> </packages>

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF Suite Home Page

Explore ByteScout PDF Suite Documentation

Explore Samples

Sign Up for ByteScout PDF Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

VIDEO

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF Suite Home Page

Explore ByteScout PDF Suite Documentation

Explore Samples

Sign Up for ByteScout PDF Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Tutorials:

prev
next