Documentation - erdomke/RtfPipe GitHub Wiki
Getting Started
Below is a simple example of converting an RTF string to an HTML string.
NOTE: When using this in .Net Core, be sure to include the NuGet package
System.Text.Encoding.CodePages
. Also call the line marked in the region before calling any functions in the library.
#if NETCORE
// Add a reference to the NuGet package System.Text.Encoding.CodePages for .Net core only
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
var html = Rtf.ToHtml(rtf);
Custom image conversion
This example assumes you have access to the .Net Framework System.Drawing library.
var html = Rtf.ToHtml(rtf, new RtfHtmlSettings() {
ImageUriGetter = picture => {
if (picture.Type is EmfBlip || picture.Type is WmMetafile)
{
using (var source = new MemoryStream(picture.Bytes))
using (var dest = new MemoryStream())
{
var bmp = new System.Drawing.Bitmap(source);
bmp.Save(dest, System.Drawing.Imaging.ImageFormat.Png);
return "data:image/png;base64," + Convert.ToBase64String(dest.ToArray());
}
}
return "data:" + picture.MimeType() + ";base64," + Convert.ToBase64String(picture.Bytes);
}
});
API
Rtf Class
Convert a Rich Text Format (RTF) document to HTML
public static class Rtf
Inheritance System.Object 🡒 Rtf
Methods
Rtf.ToHtml(RtfSource, RtfHtmlSettings) Method
Convert a Rich Text Format (RTF) document to an HTML string
public static string ToHtml(RtfPipe.RtfSource source, RtfPipe.RtfHtmlSettings settings=null);
Parameters
source
RtfSource
The source RTF document (either a System.String, System.IO.TextReader, or System.IO.Stream)
settings
RtfHtmlSettings
The settings used in the HTML rendering
Returns
System.String
An HTML string that can be used to render the RTF
Rtf.ToHtml(RtfSource, TextWriter, RtfHtmlSettings) Method
Convert a Rich Text Format (RTF) document to HTML
public static void ToHtml(RtfPipe.RtfSource source, System.IO.TextWriter writer, RtfPipe.RtfHtmlSettings settings=null);
Parameters
source
RtfSource
The source RTF document (either a System.String, System.IO.TextReader, or System.IO.Stream)
writer
System.IO.TextWriter
System.IO.TextWriter that the HTML will be written to
settings
RtfHtmlSettings
The settings used in the HTML rendering
Rtf.ToHtml(RtfSource, XmlWriter, RtfHtmlSettings) Method
Convert a Rich Text Format (RTF) document to HTML
public static void ToHtml(RtfPipe.RtfSource source, System.Xml.XmlWriter writer, RtfPipe.RtfHtmlSettings settings=null);
Parameters
source
RtfSource
The source RTF document (either a System.String, System.IO.TextReader, or System.IO.Stream)
writer
System.Xml.XmlWriter
System.Xml.XmlWriter that the HTML will be written to
settings
RtfHtmlSettings
The settings used in the HTML rendering
Example
This overload can be used for creating a document that can be further manipulated
var doc = new XDocument();
using (var writer = doc.CreateWriter())
{
Rtf.ToHtml(rtf, writer);
}
RtfHtmlSettings Class
Settings used when converting RTF to HTML
public class RtfHtmlSettings : RtfPipe.HtmlWriterSettings
Inheritance System.Object 🡒 HtmlWriterSettings 🡒 RtfHtmlSettings
Constructors
RtfHtmlSettings.RtfHtmlSettings() Constructor
Create a new RtfHtmlSettings object
public RtfHtmlSettings();
Properties
RtfHtmlSettings.AttachmentRenderer Property
Callback used when building the HTML to render an e-mail attachment
public System.Action<int,System.Xml.XmlWriter> AttachmentRenderer { get; set; }
Property Value
System.Action<System.Int32,System.Xml.XmlWriter>
RtfHtmlSettings.ElementTags Property
Mapping of HTML tags to use for various document element types
public System.Collections.Generic.Dictionary<RtfPipe.Model.ElementType,RtfPipe.Model.HtmlTag> ElementTags { get; }
Property Value
System.Collections.Generic.Dictionary<RtfPipe.Model.ElementType,RtfPipe.Model.HtmlTag>
RtfHtmlSettings.ImageUriGetter Property
Callback used to get the URI for a picture stored in RTF. This could be
a data URI that contains the binary data of the picture, or a link to
an external file.
public System.Func<RtfPipe.Picture,string> ImageUriGetter { get; set; }
Property Value
System.Func<Picture,System.String>
RtfSource Class
Represents a source of RTF content. It auto-converts from either a
System.String, System.IO.TextReader, or System.IO.Stream.
Using a System.IO.Stream is preferred as an RTF file can switch binary
encodings in the middle of a file
public class RtfSource
Inheritance System.Object 🡒 RtfSource
Constructors
RtfSource.RtfSource(TextReader) Constructor
Create an RTF source from a reader
public RtfSource(System.IO.TextReader reader);
Parameters
reader
System.IO.TextReader
The System.IO.TextReader to use
Properties
RtfSource.Reader Property
A reader used to read text from the source
public System.IO.TextReader Reader { get; }
Property Value
Operators
RtfSource.implicit operator RtfSource(string) Operator
Implicitly convert strings containing RTF content to an RtfSource
public static RtfPipe.RtfSource implicit operator RtfSource(string value);
Parameters
value
System.String
RTF content
Returns
RtfSource.implicit operator RtfSource(Stream) Operator
Implicitly convert a System.IO.Stream containing RTF content to an RtfSource
public static RtfPipe.RtfSource implicit operator RtfSource(System.IO.Stream value);
Parameters
value
System.IO.Stream
RTF content
Returns
RtfSource.implicit operator RtfSource(TextReader) Operator
Implicitly convert a System.IO.TextReader containing RTF content to an RtfSource
public static RtfPipe.RtfSource implicit operator RtfSource(System.IO.TextReader value);
Parameters
value
System.IO.TextReader
RTF content
Returns
Picture Class
A picture store in a RTF document
public class Picture : RtfPipe.Model.Node
Inheritance System.Object 🡒 Node 🡒 Picture
Constructors
Picture.Picture(Group) Constructor
Create a new Picture object
public Picture(RtfPipe.Group group);
Parameters
group
RtfPipe.Group
An RTF token group
Properties
Picture.Attributes Property
Control tokens stored in the RTF document
public System.Collections.Generic.IEnumerable<RtfPipe.IToken> Attributes { get; }
Property Value
System.Collections.Generic.IEnumerable<RtfPipe.IToken>
Picture.Bytes Property
The binary data describing the picture
public byte[] Bytes { get; }
Property Value
System.Byte(https://docs.microsoft.com/en-us/dotnet/api/System.Array 'System.Array')
Picture.Height Property
The rendered height of the picture
public RtfPipe.UnitValue Height { get; }
Property Value
Picture.Type Property
The picture format
public RtfPipe.IToken Type { get; }
Property Value
Picture.Width Property
The rendered width of the picture
public RtfPipe.UnitValue Width { get; }
Property Value
Methods
Picture.MimeType() Method
The MIME type of the picture
public string MimeType();