0

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

  • 2
    The problem isn't missing spaces, it's the fact that Notepad++ displays the data in a monospace font (where all characters have the same width) but the `RichTextBlock` displays the data using a proportional font (where characters can have different widths). – Luke Woodward Sep 23 '22 at 19:35
  • Thank you, @LukeWoodward. Please move this to answer sections, and I will mark it. By the way, is there any way to create such a table in UWP RichTextBlock for printing? When I used the DataGrid from Toolkit, the rows were contained within the grid with a scrollbar, thus not overflowing to create a multi-page print. – Krishna Katamneni Sep 24 '22 at 08:48
  • I've now added an answer, as requested. Please ask a separate question for how to get your table to print successfully over multiple pages. – Luke Woodward Sep 24 '22 at 13:05

1 Answers1

0

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.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104