wpfuwprichtextblock

RichtTextBlock is not displaying multiple spaces present in the text


I'm trying to display some table data in RichTextBlock. The data contains several lines of text, and the text contains multiple spaces. I'm adding each line as Run objects to the Paragraph.

        string[] lines = tableData;
        Paragraph para = this.TextContent.Blocks.First() as Paragraph;
        para.Inlines.Add(new LineBreak());
        para.Inlines.Add(new LineBreak());
        para.Inlines.Add(new LineBreak());
        para.Inlines.Add(new LineBreak());

        for (int l = 0; l < lines.Length; l++)
        {

            Run r = new Run()
            {
                Text = lines[l],
                FontSize = 9,
            };
            para.Inlines.Add(r);
            para.Inlines.Add(new LineBreak());
        }

RichTextBlock is replacing multiple spaces and keeping one, as shown here RichTextBlock ouput Input text looks like this in Notepad++(enabled special characters to highlight spaces) Actual input/output displayed in Notepad++

I couldn't find any property in the Paragraph or Run classes to avoid this scenario.

What do I miss to get the RichTextBlock to display the exact text displayed in Notedpadd++ like this? Expected output

Thank you


Solution

  • As per the comment above, this problem isn't caused by missing spaces. Notepad++ displays the table in a monospace font, where all characters have the same width, but the RichTextBlock displays the text using a proportional font, where characters can have different widths. Spaces seem to be narrower than other characters, hence the rows of the table don't line up.