RtfEncapsulation Class

Extracts the HTML and plain text bodies that Outlook encapsulates in RTF bodies, and encapsulates bodies in RTF ([MS-OXRTFEX]). Outlook often stores the HTML body of a message only in its compressed RTF body.

public static class RtfEncapsulation

Example

var rtf = RtfEncapsulation.EncapsulateHtml("<p>Hello {world}</p>\r\n");
Assert.Equal(RtfEncapsulationFormat.Html, RtfEncapsulation.GetFormat(rtf));
Assert.Equal("<p>Hello {world}</p>\r\n", RtfEncapsulation.Decapsulate(rtf));

// An RTF body that does not encapsulate another format
Assert.Equal(RtfEncapsulationFormat.None, RtfEncapsulation.GetFormat(@"{\rtf1\ansi Hello}"u8));
Assert.Null(RtfEncapsulation.Decapsulate(@"{\rtf1\ansi Hello}"u8));

Methods

NameDescription
Decapsulate(ReadOnlySpan<byte>)Extracts the HTML or the plain text an RTF body encapsulates.
EncapsulateHtml(string)Encapsulates an HTML body in RTF, which RtfEncapsulation.Decapsulate extracts unchanged.
EncapsulateText(string)Encapsulates a plain text body in RTF, which RtfEncapsulation.Decapsulate extracts unchanged.
GetFormat(ReadOnlySpan<byte>)Returns the format an RTF body encapsulates, from the control words of its header.

Decapsulate(ReadOnlySpan<byte>)

RtfEncapsulation.cs

Extracts the HTML or the plain text an RTF body encapsulates.

public static string? Decapsulate(ReadOnlySpan<byte> rtf)

Parameters

rtf ReadOnlySpan<byte>
The RTF, decompressed (see MapiMessage.GetRtfBody).

Returns

string? The HTML (RtfEncapsulationFormat.Html) or the plain text (RtfEncapsulationFormat.Text), or null when the RTF does not encapsulate another format.

EncapsulateHtml(string)

RtfEncapsulation.cs

Encapsulates an HTML body in RTF, which RtfEncapsulation.Decapsulate extracts unchanged.

public static byte[] EncapsulateHtml(string html)

Parameters

html string
The HTML.

Returns

byte[] The RTF. It contains only the encapsulated HTML: a reader that does not support the encapsulation shows an empty document.

EncapsulateText(string)

RtfEncapsulation.cs

Encapsulates a plain text body in RTF, which RtfEncapsulation.Decapsulate extracts unchanged.

public static byte[] EncapsulateText(string text)

Parameters

text string
The plain text.

Returns

byte[] The RTF, which any RTF reader shows as the text.

GetFormat(ReadOnlySpan<byte>)

RtfEncapsulation.cs

Returns the format an RTF body encapsulates, from the control words of its header.

public static RtfEncapsulationFormat GetFormat(ReadOnlySpan<byte> rtf)

Parameters

rtf ReadOnlySpan<byte>
The RTF, decompressed (see MapiMessage.GetRtfBody).

Returns

RtfEncapsulationFormat The format of the encapsulated body, or RtfEncapsulationFormat.None for an RTF body.