Prevent from loading styles when pasting text that includes Word styles.

4 posts, 0 answers
  1. Jakub
    Jakub avatar
    35 posts
    Member since:
    Sep 2015

    Posted 23 Apr Link to this post

    Hello,

    I am using RadRichTextBox with 3 basic style definitions only. I dont want allow users to use more, this is requirement. I achieved the expected behavior except one situation, pasting content that contains styles from MS Word. I would like to not load Style Definitions from pasted content to RadRichTextBox. My idea was to reload my 3 basic style definitions after paste command executed, because then StyleRepository contains more of them:

    private void Editor_CommandExecuted(object sender, Telerik.Windows.Documents.RichTextBoxCommands.CommandExecutedEventArgs e)
            {
                if (e.Command is PasteCommand)
                {
                    var styl1 = this.Editor.Document.StyleRepository.ElementAtOrDefault(0);
                    var styl2 = this.Editor.Document.StyleRepository.ElementAtOrDefault(1);
                    var styl3 = this.Editor.Document.StyleRepository.ElementAtOrDefault(2);
     
                    this.Editor.Document.StyleRepository.Clear();
     
                    this.Editor.Document.StyleRepository.Add(styl1);
                    this.Editor.Document.StyleRepository.Add(styl2);
                    this.Editor.Document.StyleRepository.Add(styl3);
                }
            }

     

    and this works almost ok. When saving this and opening again text is not styled.

    But right after paste, after removing this additional styles from pasted content, the pasted content still looks like it has this Style Definitions applied.

    Document doesn't have style definitions in collection and RadRichTextBox still shows styled content.

    Is it problem with refreshing RadRichTextBox after manual operations on StyleRepository?

  2. Dimitar
    Admin
    Dimitar avatar
    2910 posts

    Posted 23 Apr Link to this post

    Hi Jakub,

    You can use the following method for this:

     radRichTextBox.UpdateEditorLayout();

    Another approach is to set the paste options: Paste Options.

    I hope this helps. Should you have any other questions do not hesitate to ask.

    Regards,
    Dimitar
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  3. Jakub
    Jakub avatar
    35 posts
    Member since:
    Sep 2015

    Posted 23 Apr Link to this post

    Ok so I change my code a little and have this:

            private void DescEditor_CommandExecuting(object sender, CommandExecutingEventArgs e)
            {
                if (e.Command is PasteCommand)
                {
                    e.Cancel = true;
     
                    RadDocument document = ClipboardEx.GetDocument().ToDocument();
                    document.StyleRepository.Clear();
                    this.DescEditor.InsertFragment(new DocumentFragment(document));
                }
            }

     

    looks like it work correct.

     

    @Dimitar: how can I use this options while pasting? The only thing I found is PasteOptionsPopup that have this options listed to select. For me its too late, I want to prevent from using style definitions from MS Word while pasting text, not just allow user to choose if use source styles or no.

     

  4. Dimitar
    Admin
    Dimitar avatar
    2910 posts

    Posted 24 Apr Link to this post

    Hi Jakub,

    You need to set it when initializing the control. For example: 

    radRichTextBox.PasteSettings.SetDefaultPasteOptionForPasteSource(PasteSource.AnotherRichTextDocument, PasteOption.KeepTextOnly);
    radRichTextBox.PasteSettings.SetDefaultPasteOptionForPasteSource(PasteSource.DocumentWithConflictingStyles, PasteOption.KeepTextOnly);
    
    radRichTextBox.IsPasteOptionsPopupEnabled = false;
    

    This way you do not need to intercept the paste command. 

    Let me know if you have any other questions.

    Regards,
    Dimitar
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
Back to Top