We made thousands of pre-made source code pieces for easy implementation in your own programming projects. What is ByteScout PDF Suite? It is the set that includes 6 SDK products 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 help you to convert html email with attachments to pdf with pdf sdk in your C# application.
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 html email with attachments to pdf with pdf sdk. Follow the instructions from scratch to work and copy the C# code. Complete and detailed tutorials and documentation are available along with installed ByteScout PDF Suite if you’d like to learn more about the topic and the details of the API.
All these programming tutorials along with source code samples and ByteScout free trial version are available for download from our website.
On-demand (REST Web API) version:
Web API (on-demand version)
On-premise offline SDK for Windows:
60 Day Free Trial (on-premise)
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_HTMLEmailWithAttachments
{
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("HtmlSampleEmailWithAttachment.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.BodyHtml;
// 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 and attachments 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
MemoryStream 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);
}
// Get attachments from message (if any, and append to document)
if (msg.Attachments.Count > 0)
{
foreach (MsgReader.Outlook.Storage.Attachment itmAttachment in msg.Attachments)
{
// Get Memory Stream
MemoryStream attachmentMemoryStream = new MemoryStream(itmAttachment.Data);
// Append Attachment
Document docAttachment = new Document(attachmentMemoryStream);
// Append all pages to main PDF
foreach (Page item in docAttachment.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();
}
}
}
}
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
Get Your API Key
Explore Web API Docs
Explore Web API Samples
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_HTMLEmailWithAttachments
{
class Program
{
static void Main(string[] args)
{
try
{
// Registering encoding provider for .net core solution
System.Text.EncodingProvider encodingProvider = System.Text.CodePagesEncodingProvider.Instance;
Encoding.RegisterProvider(encodingProvider);
// Parse MessageContents using MsgReader Library
// MsgReader library can be obtained from: https://github.com/Sicos1977/MSGReader
using (var msg = new MsgReader.Outlook.Storage.Message("HtmlSampleEmailWithAttachment.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.BodyHtml;
// 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 and attachments 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
MemoryStream 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);
}
// Get attachments from message (if any, and append to document)
if (msg.Attachments.Count > 0)
{
foreach (MsgReader.Outlook.Storage.Attachment itmAttachment in msg.Attachments)
{
// Get Memory Stream
MemoryStream attachmentMemoryStream = new MemoryStream(itmAttachment.Data);
// Append Attachment
Document docAttachment = new Document(attachmentMemoryStream);
// Append all pages to main PDF
foreach (Page item in docAttachment.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();
}
}
}
}
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
Get Your API Key
Explore Web API Docs
Explore Web API Samples
<?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>
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
Get Your API Key
Explore Web API Docs
Explore Web API Samples
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
Get Your API Key
Explore Web API Docs
Explore Web API Samples
also available as: