Copy table into WPF RichTextBox loosing borders

Viewed 497

I generate a FlowDocument with a Table and replace a Selection inside a WPF RTF box.

But the inserted table uses the last used BorderSettings for all cells. If I load the generated XAML inside a FlowDocumentPageViewer all looks fine.

Viewer:
enter image description here

Wrong RTF:
enter image description here

Please ignore the other formatting, only the Borders are my problem, the last Border is the underline, but in RTF it overwrites all other CellBorders and Color in the table.

Code:

//GetTable returns the Table
FlowDocument tmp_rtf_tabelle_test =new FlowDocument(((Datenblock_Mitte_Tabelle_Typ5)obj).GetTable());
              
TextRange tr_tabelle = new TextRange(tmp_rtf_tabelle_test.ContentStart,tmp_rtf_tabelle_test.ContentEnd);         

MemoryStream ms_tabelle = new MemoryStream();
                       
//if Dataformats XAML or rtf make no difference
tr_tabelle.Save(ms_tabelle, DataFormats.Xaml);                           
                   
rtf_vorschau.Selection.Load(ms_tabelle, DataFormats.Xaml);
ms_tabelle.Close();

Here's the generated XAML:

<Section
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve" TextAlignment="Justify" LineHeight="Auto" IsHyphenationEnabled="False" xml:lang="en-us" FlowDirection="LeftToRight" NumberSubstitution.CultureSource="Text" NumberSubstitution.Substitution="AsCulture" FontFamily="Georgia" FontStyle="Normal" FontWeight="Normal" FontStretch="Normal" FontSize="16" Foreground="#FF000000" Typography.StandardLigatures="True" Typography.ContextualLigatures="True" Typography.DiscretionaryLigatures="False" Typography.HistoricalLigatures="False" Typography.AnnotationAlternates="0" Typography.ContextualAlternates="True" Typography.HistoricalForms="False" Typography.Kerning="True" Typography.CapitalSpacing="False" Typography.CaseSensitiveForms="False" Typography.StylisticSet1="False" Typography.StylisticSet2="False" Typography.StylisticSet3="False" Typography.StylisticSet4="False" Typography.StylisticSet5="False" Typography.StylisticSet6="False" Typography.StylisticSet7="False" Typography.StylisticSet8="False" Typography.StylisticSet9="False" Typography.StylisticSet10="False" Typography.StylisticSet11="False" Typography.StylisticSet12="False" Typography.StylisticSet13="False" Typography.StylisticSet14="False" Typography.StylisticSet15="False" Typography.StylisticSet16="False" Typography.StylisticSet17="False" Typography.StylisticSet18="False" Typography.StylisticSet19="False" Typography.StylisticSet20="False" Typography.Fraction="Normal" Typography.SlashedZero="False" Typography.MathematicalGreek="False" Typography.EastAsianExpertForms="False" Typography.Variants="Normal" Typography.Capitals="Normal" Typography.NumeralStyle="Normal" Typography.NumeralAlignment="Normal" Typography.EastAsianWidths="Normal" Typography.EastAsianLanguage="Normal" Typography.StandardSwashes="0" Typography.ContextualSwashes="0" Typography.StylisticAlternates="0">
    <Table Padding="0,0,0,0" BorderThickness="1,1,1,1">
        <Table.Columns>
            <TableColumn Width="300" />
            <TableColumn Width="419" />
        </Table.Columns>
        <TableRowGroup>
            <TableRow>
                <TableCell ColumnSpan="2" BorderThickness="1,1,1,1" BorderBrush="#FFFF0000">
                    <Paragraph Background="#FFD3D3D3">
                        <Run> 5. Weitere Vorgehensweise</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell Padding="0,0,0,0" BorderThickness="1,0,0,0" BorderBrush="#FF0000FF">
                    <Paragraph LineHeight="1" Margin="0,0,0,0">
                        <Run> Haftung:</Run>
                    </Paragraph>
                </TableCell>
                <TableCell BorderThickness="0,0,0,1" BorderBrush="#FFFFFF00">
                    <Paragraph>
                        <Run>&lt;Bitte wählen&gt;</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell>
                    <Paragraph>
                        <Run> Deckung:</Run>
                    </Paragraph>
                </TableCell>
                <TableCell>
                    <Paragraph>
                        <Run>&lt;Bitte wählen&gt;</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell>
                    <Paragraph>
                        <Run> Plausibilität:</Run>
                    </Paragraph>
                </TableCell>
                <TableCell>
                    <Paragraph>
                        <Run>&lt;Bitte wählen&gt;</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell>
                    <Paragraph>
                        <Run> Zahlung in €:</Run>
                    </Paragraph>
                </TableCell>
                <TableCell>
                    <Paragraph>
                        <Run>0,00</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell>
                    <Paragraph>
                        <Run> Reserve in €:</Run>
                    </Paragraph>
                </TableCell>
                <TableCell>
                    <Paragraph>
                        <Run>0,00</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell>
                    <Paragraph>
                        <Run> Unterlagen folgen:</Run>
                    </Paragraph>
                </TableCell>
                <TableCell>
                    <Paragraph>
                        <Run>&lt;Bitte wählen&gt;</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell ColumnSpan="2">
                    <Paragraph Margin="3,3,3,3">
                        <Run></Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
        </TableRowGroup>
    </Table>
</Section>

UPDATE: I created a RTF file with the Tables and loaded them (FileStream), but the result is the same. BorderSettings are lost.

1 Answers

According to the problem description when loading the Rich Text Format (RTF) document, that includes table with colored borders, the borders loses color and displayed in black.

Let suppose the original .rtf document includes the following table with yellow cell borders:

enter image description here

After loading this document to the RichTextBox control the table will look like below:

enter image description here

Unfortunately this is a known limitation of displaying .rtf documents prepared with the Microsft Word in WPF RichTextBox control.

The MSDN Community Support person wrote:

I load the rtf document and displayed in WPF RitchTextBox, it lost part of the format (Open Normal with Word). If you want to open it with WPF RitchTextBox, I recommend using WPF RitchTextBox to save RTF files open to ensure consistent format. You can Load, Edit and Save Rich Text Format in WPF. Some styles may be lost through tool conversion.

For more information see article on the Microsoft site: rtf document displayed in RitchTextBox display wrong colors

Related