bulleted/number list format in *.rtf file in WPF RichTextBox is disordered

Scott Q-0525 121 Reputation points
2023-09-08T02:15:14.3+00:00

See below format in *.rft file, after load the *.rft file into WPF RichTextBox, the formats are disordered.

  1. string 1 AAA
  2. string 2
  • AB
  • BB

After load into RichTextBox, or using TextRange.Save to a new *.rtf again.

RichTextBox

The source codes:

 var file = @"D:\my.rtf";
 this.myTestRichTextBox.Document = new FlowDocument();
 using (var stream = File.OpenRead(file))
 {
     var textRange = new TextRange(myTestRichTextBox.Document.ContentStart, myTestRichTextBox.Document.ContentEnd);
    textRange.Load(stream, DataFormats.Rtf);
 
 }

Thanks

ScottQ

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,762 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,860 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,511 Reputation points Microsoft Vendor
    2023-09-08T09:45:44.2366667+00:00

    Hi,@Scott Q-0525. You could try the following code:

    
    <StackPanel>
        
            <Button Content="show" HorizontalAlignment="Left" Margin="355,60,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
    
            <RichTextBox Name="myTestRichTextBox"  AcceptsReturn="True"  Height="100">
                <FlowDocument>
                    <Paragraph>
                        <Run Text="RichTextBox"/>
                    </Paragraph>
                </FlowDocument>
            </RichTextBox>
    
    
        </StackPanel>
    
    
    
      string filePath = "C:\\...\\example.rtf";
           
            
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
               
                    TextRange range;
                    System.IO.FileStream fStream;
                        range = new TextRange(myTestRichTextBox.Document.ContentStart, myTestRichTextBox.Document.ContentEnd);
                        fStream = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate);
                        range.Load(fStream, System.Windows.DataFormats.Rtf);
    
                        fStream.Close();
                    
                
    
            }
    
    

    The result:

    enter image description here


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.