This is a migrated thread and some comments may be shown as answers.

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

3 Answers 96 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Jakub
Top achievements
Rank 1
Iron
Jakub asked on 23 Apr 2020, 08:00 AM

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?

3 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 23 Apr 2020, 10:38 AM

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.
0
Jakub
Top achievements
Rank 1
Iron
answered on 23 Apr 2020, 12:07 PM

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.

 

0
Dimitar
Telerik team
answered on 24 Apr 2020, 09:32 AM

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.
Tags
RichTextBox
Asked by
Jakub
Top achievements
Rank 1
Iron
Answers by
Dimitar
Telerik team
Jakub
Top achievements
Rank 1
Iron
Share this question
or