For example, to obtain the formatted text in RTF format, try this:
string rtf;
TextRange tr = new TextRange( richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd );
using( var ms = new MemoryStream( ) )
{
tr.Save( ms, DataFormats.Rtf );
rtf = Encoding.ASCII.GetString( ms.GetBuffer( ), 0, checked((int)ms.Length) );
}
Then store the value of rtf. The varchar(max) is a suitable database type.
To load it:
string rtf = ... // load from database
TextRange tr = new TextRange( richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd );
using( var ms = new MemoryStream( Encoding.ASCII.GetBytes( rtf ) ) )
{
tr.Load( ms, DataFormats.Rtf );
}
Or, you can use the DataFormats.Xaml or DataFormats.XamlPackage formats, or store the bytes in binary format (varbinary(max)) without conversions to strings.