ByteScout PDF SDK - VB.NET - convert Email with attachemnts to PDF - ByteScout

ByteScout PDF SDK – VB.NET – convert Email with attachemnts to PDF

  • Home
  • /
  • Articles
  • /
  • ByteScout PDF SDK – VB.NET – convert Email with attachemnts to PDF

How to convert email with attachemnts to PDF in VB.NET with ByteScout PDF SDK

Write code in VB.NET to convert email with attachemnts to PDF with this step-by-step tutorial

The coding tutorials are designed to help you test the features without need to write your own code. Want to convert email with attachemnts to PDF in your VB.NET app? ByteScout PDF SDK is designed for it. ByteScout PDF SDK is the library for pdf documents generation, modification and updates. Can also generate and fill PDF forms. Provides support for text (fonts, style, size, font family), layers, pdf form fields, vector and raster drawings.

This code snippet below for ByteScout PDF SDK works best when you need to quickly convert email with attachemnts to PDF in your VB.NET application. Just copy and paste the code into your VB.NET application’s code and follow the instruction. Test VB.NET sample code examples whether they respond your needs and requirements for the project.

Download free trial version of ByteScout PDF SDK from our website with this and other source code samples for VB.NET.

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

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

Program.vb
      
Imports System.Drawing.Printing Imports System.IO Imports System.Text Imports Bytescout.PDF Imports Bytescout.PDF.Converters Imports Font = Bytescout.PDF.Font Imports SolidBrush = Bytescout.PDF.SolidBrush Module Program Sub Main() Try ' Parse MessageContents using MsgReader Library ' MsgReader library can be obtained from: https://github.com/Sicos1977/MSGReader Using msg = New MsgReader.Outlook.Storage.Message("EmailWithAttachments.msg") ' Get Sender information Dim from = msg.GetEmailSender(False, False) ' Message sent datetime Dim sentOn = msg.SentOn ' Recipient To information Dim recipientsTo = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.[To], False, False) ' Recipient CC information Dim recipientsCc = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.Cc, False, False) ' Recipient BCC information Dim recipientBcc = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.Bcc, False, False) ' Message subject Dim subject = msg.Subject ' Get Message Body Dim msgBody = msg.BodyHtml ' Prepare PDF docuemnt Using outputDocument As Document = New Document() ' Add registration keys outputDocument.RegistrationName = "demo" outputDocument.RegistrationKey = "demo" ' Add page Dim page As Page = New Page(PaperFormat.A4) outputDocument.Pages.Add(page) ' Default font and brush Dim font As Font = New Font(StandardFonts.Times, 12) Dim brush As Brush = New SolidBrush() ' Add Email contents Dim topMargin As Integer = 0 topMargin += 20 page.Canvas.DrawString({code}quot;File Name: {msg.FileName}", font, brush, 20, topMargin) topMargin += 20 page.Canvas.DrawString({code}quot;From: {from}", font, brush, 20, topMargin) topMargin += 20 page.Canvas.DrawString({code}quot;Sent On: {(If(sentOn.HasValue, sentOn.Value.ToString("MM/dd/yyyy HH:mm"), ""))}", font, brush, 20, topMargin) topMargin += 20 page.Canvas.DrawString({code}quot;To: {recipientsTo}", font, brush, 20, topMargin) If Not String.IsNullOrEmpty(recipientsCc) Then topMargin += 20 page.Canvas.DrawString({code}quot;CC: {recipientsCc}", font, brush, 20, topMargin) End If If Not String.IsNullOrEmpty(recipientBcc) Then topMargin += 20 page.Canvas.DrawString({code}quot;BCC: {recipientBcc}", font, brush, 20, topMargin) End If topMargin += 20 page.Canvas.DrawString({code}quot;Subject: {subject}", font, brush, 20, topMargin) topMargin += 20 page.Canvas.DrawString("Message body in next page.", font, brush, 20, topMargin) Using converter As HtmlToPdfConverter = New HtmlToPdfConverter() converter.PageSize = PaperKind.A4 converter.Orientation = Bytescout.PDF.Converters.PaperOrientation.Portrait ' Get all inline attachment, and replace them For Each itmAttachment As MsgReader.Outlook.Storage.Attachment In msg.Attachments If itmAttachment.IsInline Then Dim oData = itmAttachment.Data Dim dataBase64 = Convert.ToBase64String(oData) ' Replace within email msgBody = msgBody.Replace({code}quot;src=""{itmAttachment.FileName}""", {code}quot;src=""data:image/jpeg;base64,{dataBase64}""") End If Next ' Convert input HTML to stream Dim byteArrayBody As Byte() = Encoding.UTF8.GetBytes(msgBody) Dim inputStream As MemoryStream = New MemoryStream(byteArrayBody) ' Create output stream to store generated PDF file Using outputStream = New MemoryStream() ' Convert HTML to PDF converter.ConvertHtmlToPdf(inputStream, outputStream) ' Create new document from generated output stream Dim docContent As Document = New Document(outputStream) ' Append all pages to main PDF For Each item As Page In docContent.Pages outputDocument.Pages.Add(item) Next ' Apped all other attachments For Each itmAttachment As MsgReader.Outlook.Storage.Attachment In msg.Attachments If Not itmAttachment.IsInline Then ' Attachment is image, so adding accordingly Dim pageAttachment = New Page(PaperFormat.A4) Dim canvas As Canvas = pageAttachment.Canvas Dim oAttachmentStream = New MemoryStream(itmAttachment.Data) Dim imageAttachment As Image = New Image(oAttachmentStream) canvas.DrawImage(imageAttachment, 20, 20) ' Add attachment outputDocument.Pages.Add(pageAttachment) End If Next ' Save output file outputDocument.Save("result.pdf") End Using End Using ' Open output file Process.Start("result.pdf") End Using End Using Catch ex As Exception Console.WriteLine(ex.Message) Console.WriteLine("Press enter key to exit...") Console.ReadLine() End Try End Sub End Module

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF SDK Home Page

Explore ByteScout PDF SDK Documentation

Explore Samples

Sign Up for ByteScout PDF SDK 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.1" 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 SDK Home Page

Explore ByteScout PDF SDK Documentation

Explore Samples

Sign Up for ByteScout PDF SDK 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 SDK Home Page

Explore ByteScout PDF SDK Documentation

Explore Samples

Sign Up for ByteScout PDF SDK Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Tutorials:

prev
next