如何将FlowDocument转换为字符串?

时间:2017-02-07 02:49:20

标签: c# wpf converter flowdocument

enter image description here

1)如何将FlowDocument转换为字符串?

2)如何将我的字符串转换为FlowDocument?

FlowDocument包含文字和图片。

应保留字符串中的文本。 Text12345

必须转换图片。例如,编码:iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAABHNCSV

Text12345
iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAABHNCSV
Text67890


<text>
    Text12345
    <encoded_image>
        iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAABHNCSV
    </encoded_image>
    Text67890
</text>

XAML:

<RichTextBox x:Name="textrich" >
    <FlowDocument >
        <Paragraph/>
    </FlowDocument>
</RichTextBox>

1 个答案:

答案 0 :(得分:2)

获取 RichTextBox 文本的常用方法是使用 TextRange

string rtb = new TextRange(rtb .Document.ContentStart, rtb .Document.ContentEnd).Text;

在您的情况下,您希望以不同方式处理不同的块和内联。你可以枚举它们。像这样:

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 
                } 

   }