ByteScout PDF SDK - C# - Convert RTF Email to PDF - ByteScout

ByteScout PDF SDK – C# – Convert RTF Email to PDF

  • Home
  • /
  • Articles
  • /
  • ByteScout PDF SDK – C# – Convert RTF Email to PDF

How to convert RTF email to PDF in C# using ByteScout PDF SDK

How to code in C# to convert RTF email to PDF with this step-by-step tutorial

Source code documentation samples provide quick and easy way to add a required functionality into your application. ByteScout PDF SDK: the pdf library that can create, update and modify PDF files. Supports text with fonts and style selections, layers, form fields, drawing lines and objects, automatic tables, images. Can be used to create and fill pdf forms. It can convert RTF email to PDF in C#.

This rich sample source code in C# for ByteScout PDF SDK includes the number of functions and options you should do calling the API to convert RTF email to PDF. Follow the instructions from the scratch to work and copy the C# code. Enjoy writing a code with ready-to-use sample codes in C#.

ByteScout PDF SDK free trial version is available on our website. C# and other programming languages are supported.

Try it today: Get 60 Day Free Trial or sign up for Web API

EmailToPDF_RTFEmail.csproj
      
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProjectGuid>{8B33CB1C-B6A2-4750-9D1C-EB963DC8A17D}</ProjectGuid> <OutputType>Exe</OutputType> <RootNamespace>EmailToPDF_RTFEmail</RootNamespace> <AssemblyName>EmailToPDF_RTFEmail</AssemblyName> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> <TargetFrameworkProfile /> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PlatformTarget>AnyCPU</PlatformTarget> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <Prefer32Bit>false</Prefer32Bit> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PlatformTarget>AnyCPU</PlatformTarget> <DebugType>pdbonly</DebugType> <Optimize>true</Optimize> <OutputPath>bin\Release\</OutputPath> <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <Prefer32Bit>false</Prefer32Bit> </PropertyGroup> <ItemGroup> <Reference Include="Bytescout.PDF, Version=1.8.2.254, Culture=neutral, PublicKeyToken=f7dd1bd9d40a50eb, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>c:\Program Files\Bytescout PDF SDK\net4.5\Bytescout.PDF.dll</HintPath> </Reference> <Reference Include="Bytescout.PDF.Converters"> <HintPath>c:\Program Files\Bytescout PDF SDK\net4.5\Bytescout.PDF.Converters.dll</HintPath> </Reference> <Reference Include="Microsoft.CSharp" /> <Reference Include="MsgReader, Version=3.4.0.0, Culture=neutral, PublicKeyToken=3ba01cf3434959fa, processorArchitecture=MSIL"> <HintPath>packages\MsgReader.3.4.0\lib\net45\MsgReader.dll</HintPath> </Reference> <Reference Include="OpenMcdf, Version=2.2.1.2, Culture=neutral, PublicKeyToken=fdbb1629d7c00800, processorArchitecture=MSIL"> <HintPath>packages\OpenMcdf.2.2.1.3\lib\net40\OpenMcdf.dll</HintPath> </Reference> <Reference Include="RtfPipe, Version=1.0.6787.26245, Culture=neutral, processorArchitecture=MSIL"> <HintPath>packages\RtfPipe.1.0.0.23\lib\net45\RtfPipe.dll</HintPath> </Reference> <Reference Include="System" /> <Reference Include="System.Data" /> <Reference Include="System.Drawing" /> <Reference Include="System.Security" /> <Reference Include="System.Web" /> <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> <Compile Include="Program.cs" /> </ItemGroup> <ItemGroup> <None Include="RtfSampleEmail.msg"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> <None Include="packages.config" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> </Project>

Try it today: Get 60 Day Free Trial or sign up for Web API

EmailToPDF_RTFEmail.sln
      
Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.27703.2026 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmailToPDF_RTFEmail", "EmailToPDF_RTFEmail.csproj", "{8B33CB1C-B6A2-4750-9D1C-EB963DC8A17D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {8B33CB1C-B6A2-4750-9D1C-EB963DC8A17D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8B33CB1C-B6A2-4750-9D1C-EB963DC8A17D}.Debug|Any CPU.Build.0 = Debug|Any CPU {8B33CB1C-B6A2-4750-9D1C-EB963DC8A17D}.Release|Any CPU.ActiveCfg = Release|Any CPU {8B33CB1C-B6A2-4750-9D1C-EB963DC8A17D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {88E4CD5D-0D2E-42FD-BD52-BFF59A042E25} EndGlobalSection EndGlobal

Try it today: Get 60 Day Free Trial or sign up for Web API

Program.cs
      
using System; using System.Diagnostics; using System.Drawing.Printing; using System.IO; using System.Text; using Bytescout.PDF; using Bytescout.PDF.Converters; using Font = Bytescout.PDF.Font; using SolidBrush = Bytescout.PDF.SolidBrush; namespace EmailToPDF_HTMLEmail { class Program { static void Main(string[] args) { try { // Parse MessageContents using MsgReader Library // MsgReader library can be obtained from: https://github.com/Sicos1977/MSGReader using (var msg = new MsgReader.Outlook.Storage.Message("RtfSampleEmail.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); // Message subject var subject = msg.Subject; // Get Message Body var msgBody = msg.BodyRtf; // COnvert RTF to HTML msgBody = RtfPipe.Rtf.ToHtml(msgBody); // Prepare PDF docuemnt using (Document outputDocument = new Document()) { // Add registration keys outputDocument.RegistrationName = "demo"; outputDocument.RegistrationKey = "demo"; // Add page Page page = new Page(PaperFormat.A4); outputDocument.Pages.Add(page); // Add sample content Font font = new Font(StandardFonts.Times, 12); Brush brush = new SolidBrush(); // Add Email contents int topMargin = 20; page.Canvas.DrawString({code}quot;File Name: {msg.FileName}", font, brush, 20, (topMargin += 20)); page.Canvas.DrawString({code}quot;From: {from}", font, brush, 20, (topMargin += 20)); page.Canvas.DrawString({code}quot;Sent On: {(sentOn.HasValue ? sentOn.Value.ToString("MM/dd/yyyy HH:mm") : "")}", font, brush, 20, (topMargin += 20)); page.Canvas.DrawString({code}quot;To: {recipientsTo}", font, brush, 20, (topMargin += 20)); if (!string.IsNullOrEmpty(recipientsCc)) { page.Canvas.DrawString({code}quot;CC: {recipientsCc}", font, brush, 20, (topMargin += 20)); } page.Canvas.DrawString({code}quot;Subject: {subject}", font, brush, 20, (topMargin += 20)); page.Canvas.DrawString("Message body in next page.", font, brush, 20, (topMargin += 20)); // Convert Html body to PDF in order to retain all formatting. using (HtmlToPdfConverter converter = new HtmlToPdfConverter()) { converter.PageSize = PaperKind.A4; converter.Orientation = Bytescout.PDF.Converters.PaperOrientation.Portrait; // Convert input HTML to stream byte[] byteArrayBody = Encoding.UTF8.GetBytes(msgBody); MemoryStream inputStream = new MemoryStream(byteArrayBody); // Create output stream to store generated PDF file using (var outputStream = new MemoryStream()) { // Convert HTML to PDF converter.ConvertHtmlToPdf(inputStream, outputStream); // Create new document from generated output stream Document docContent = new Document(outputStream); // Append all pages to main PDF foreach (Page item in docContent.Pages) { outputDocument.Pages.Add(item); } // Save output file outputDocument.Save("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(); } } } }

Try it today: Get 60 Day Free Trial or sign up for Web API

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" /> <package id="RtfPipe" version="1.0.0.23" targetFramework="net45" /> </packages>

Try it today: Get 60 Day Free Trial or sign up for Web API

MORE INFORMATION

Get 60 Day Free Trial or Visit ByteScout PDF SDK page

Explore ByteScout PDF SDK documentation

WEB API VERSION

Sign Up for free Web API key

Explore Web API Documentation

Tutorials:

prev
next