1) How to convert FlowDocument to string?
2) How to convert my string to FlowDocument?
FlowDocument contains text and pictures.
The text in the string should be preserved. Text12345
Pictures must be converted. For example, encoded: iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAABHNCSV
Text12345
iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAABHNCSV
Text67890
<text>
Text12345
<encoded_image>
iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAABHNCSV
</encoded_image>
Text67890
</text>
XAML:
<RichTextBox x:Name="textrich" >
<FlowDocument >
<Paragraph/>
</FlowDocument>
</RichTextBox>
The usual way for getting the text of a RichTextBox is by using a TextRange:
string rtb = new TextRange(rtb .Document.ContentStart, rtb .Document.ContentEnd).Text;
In your case, you want to deal with different Blocks and Inlines, differently. You can enumerate them. Something like this:
var res = new StringBuilder();
foreach (Block r in rtb.Document.Blocks)
{
if (r is Paragraph)
foreach (Inline i in ((Paragraph)r).Inlines)
if (i is Run)
{
res.Append(r.ToString());
}
else if (i is InlineUIContainer)
{
res.Append(ConvertToString((Image)((InlineUIContainer)i).Child)); // you should create ConvertToString() method
}
}