An easy to understand sample source code to learn how to convert rtf email with attachments to pdf with pdf sdk in VB.NET ByteScout PDF Suite is 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 rtf email with attachments to pdf with pdf sdk in VB.NET.
The SDK samples given below describe how to quickly make your application do convert rtf email with attachments to pdf with pdf sdk in VB.NET with the help of ByteScout PDF Suite. Simply copy and paste in your VB.NET project or application you and then run your app! Applying VB.NET application mostly includes various stages of the software development so even if the functionality works please test it with your data and the production environment.
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 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)
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("RtfSampleEmailWithAttachment.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.BodyRtf
' Convert RTF to HTML
msgBody = RtfPipe.Rtf.ToHtml(msgBody)
' 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 and attachments in next page.", font, brush, 20, topMargin)
' Convert Html body to PDF in order to retain all formatting.
Using converter As HtmlToPdfConverter = New HtmlToPdfConverter()
converter.PageSize = PaperKind.A4
converter.Orientation = Bytescout.PDF.Converters.PaperOrientation.Portrait
' 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
' Get attachments from message (if any, and append to document)
If msg.Attachments.Count > 0 Then
For Each itmAttachment As MsgReader.Outlook.Storage.Attachment In msg.Attachments
' Get Memory Stream
Dim attachmentMemoryStream As MemoryStream = New MemoryStream(itmAttachment.Data)
' Append Attachment
Dim docAttachment As Document = New Document(attachmentMemoryStream)
' Append all pages to main PDF
For Each item As Page In docAttachment.Pages
outputDocument.Pages.Add(item)
Next
Next
End If
' Save output file
outputDocument.Save("result.pdf")
' Open output file
Process.Start("result.pdf")
End Using
End Using
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
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.1" 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: