1

Following btnTest_Click(...) event in my WPF .NET5 app successfully displays the content of a RichTextBox into a FlowDocumentReader. But, as shown in the images below, the different page viewing modes of the FlowDocumentReader create excessive amounts of whitespace on lines. Question: Why it is happening, what I may be missing here, and how can we resolve the issue?

MainWindow.xaml

<Window x:Class="Wpf_RTBFlowDocTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Wpf_RTBFlowDocTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DockPanel Name="mainPanel">
            <ToolBar Name="mainToolBar" Height="30" DockPanel.Dock="Top">
                <Button x:Name="btnTest" Content="Test" Click="btnTest_Click"/>
            </ToolBar>
            <RichTextBox Name="rtbTest" AcceptsTab="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"/>
            <FlowDocumentReader x:Name="fdReader" ScrollViewer.VerticalScrollBarVisibility="Auto" IsScrollViewEnabled="True">
                <FlowDocument IsOptimalParagraphEnabled="True" IsHyphenationEnabled="True" TextAlignment="Left"></FlowDocument>
            </FlowDocumentReader>
        </DockPanel>
    </Grid>
</Window>

MainWindow.xaml.cs

private void btnTest_Click(object sender, RoutedEventArgs e)
{
    var range = new TextRange(rtbTest.Document.ContentStart, rtbTest.Document.ContentEnd);
    if (!range.IsEmpty)
    {
        if(fdReader.Document.Blocks.Count > 0)
            fdReader.Document.Blocks.Clear();

        using (var stream = new MemoryStream())
        {
            range.Save(stream, DataFormats.XamlPackage);
            var copyto = new TextRange(fdReader.Document.ContentEnd, fdReader.Document.ContentEnd);
            copyto.Load(stream, DataFormats.XamlPackage);
        }
    }
    rtbTest.Visibility = Visibility.Collapsed;
    fdReader.Visibility = Visibility.Visible;
}

Original display of the app before clicking the Test button:

enter image description here

Single pageview display after the above code ran:

enter image description here

Single Scroll pageview display after above code ran:

enter image description here

Multiple pageview display after the above code ran:

enter image description here

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
nam
  • 21,967
  • 37
  • 158
  • 332
  • 1
    Perhaps the document content affects the document presentation. It’s difficult to understand what causes the problem without [MCVE](https://stackoverflow.com/help/minimal-reproducible-example). Can you leave only 2-3 paragraphs around the _**"break"**_ sentence, save the document to `*.rtf` file and include the file to the post (in text format)? – Jackdaw May 22 '21 at 18:53
  • @Jackdaw Your above comment resolved the issue. The document content indeed affects the document presentation. I had copied the original content from [this file](https://ufile.io/c3yw5x2x) that had too many spaces. After reading your comment, I deleted those spaces to create a new file [uploaded here](https://ufile.io/ldezcziu). Now the app displays the content properly in all three pageview modes. You may want to explain a reason for the issue in a `response` - and I'll mark that as an `Answer`. Note: The two uploaded files will be deleted automatically after 30 days. – nam May 24 '21 at 23:03

1 Answers1

1

That's exactly as expected: your source text file is including lots of NewLine characters for line feed inside the sentences and spaces used for lines content alignment:

enter image description here

In a flow document, the content adapts itself to fit the container, but NewLine characters inside sentences prevented the FlowDocument control to format the text correctly.

Therefore, it's necessary to make some source text processing before loading it to the FlowDocument control.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33