Unable to Format the contents of RichEditBox once RTF document has been opened via LoadFromStream method RRS feed

  • Question

  • Once I read files from rtf document and write it to the RichEditBox and then try to align them left,right,center, it does not happen....i.e the default text alignment of document prevails....meaning if originally, the rtf document was aligned to center, then it remains aligned to center itself...if i try to change its allignment, it does not change.... help

    Here is some code samples----- editor is RichEditBox

    private async void import_Tapped(object sender, TappedRoutedEventArgs e)
                FileOpenPicker fop = new FileOpenPicker();
                fop.SuggestedStartLocation = PickerLocationId.Desktop;
                StorageFile fileStream = await fop.PickSingleFileAsync();
                if (null == fileStream)
                    MessageDialog dialog = new MessageDialog("File couldn't be Read.Please try again later!", "Oops! Something went wrong");
                    await dialog.ShowAsync();
                IRandomAccessStream stream = await fileStream.OpenAsync(FileAccessMode.Read);
                editor.Document.LoadFromStream(TextSetOptions.FormatRtf, stream);
    private void align_left_radio_Checked(object sender, RoutedEventArgs e)
                editor.TextAlignment = TextAlignment.Left;
            private void align_center_radio_Checked(object sender, RoutedEventArgs e)
                editor.TextAlignment = TextAlignment.Center;
            private void align_right_radio_Checked(object sender, RoutedEventArgs e)
                editor.TextAlignment = TextAlignment.Right;

    Friday, January 2, 2015 8:14 AM


All replies

  • This is driving me nuts. Why ain't anybody replying?
    Sunday, January 4, 2015 4:56 PM
  • It is not only a weekend: it is a holiday weekend. Most people probably aren't watching the forums. 

    It would also help if you'd simplify the code snippet to just the minimal relevant and if you'd use the Insert Code Block tool so the code was  more readable. If the repro requires multiple files or significant amounts of code then please share a repro project on your OneDrive.

    In this case the problem is probably that the RTF you are loading sets paragraph alignment internally. The TextAlignment property sets the control's overall alignment. Since the main point of the RichEditBox is to allow the text to have different formatting for different parts this isn't very useful. Instead use the ITextDocument from the RichEditBox's Document property to set the character or paragraph formatting within rich text.

    To adjust the alignment set the paragraph's  Alignment property

    Sunday, January 4, 2015 6:08 PM
  • I wish to change the whole document's alignment to center, left or right and not just a simple paragraph....

    I think since I am using TextSetOptions.FormatRtf and while loading too i use FormatRtf....that's why the text is getting formatted permanently

    Thursday, January 8, 2015 8:57 PM
  • can you tell me how to use ITextDocument to change the TextAlignment of whole document because ITextDocument does not contain TextAlignment Property
    Thursday, January 8, 2015 9:00 PM
  • Hi Rahul,

    As I mentioned in my previous post to adjust the alignment set the paragraph's  Alignment property.

    You can get the default paragraph format with the GetDefaultParagraphFormat method, modify it, then set the new format with SetDefaultParagraphFormat method

    • Marked as answer by RahulRanjan123 Saturday, January 10, 2015 11:05 PM
    Saturday, January 10, 2015 2:05 AM
  • Thanks man..finally the issue is solved!
    Saturday, January 10, 2015 11:05 PM