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

Pasting MS Word Formatted Fragments

1 Answer 131 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
James Daresta
Top achievements
Rank 1
James Daresta asked on 31 May 2016, 03:29 PM
Trying to figure out the best way via code to paste from a word document (using interop) a fragment of formatted text to a RichTextEditor as Html that is filtered. I can easily just paste, but then it seems I get a whole bunch of Word specific format. I want to paste filtered text. How would I go about this?

1 Answer, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 03 Jun 2016, 07:33 AM
Hi James,

You could filter the content through the ClipboardStringFilter property of the clipboard handlers. For more details on thе available settings, please refer to the Clipboard Support help article.

In case you need to customize the default paste behavior, you could subscribe to the CommandExecuting event and change the PasteCommand. The ClipboardEx class allows you obtain the contents of the Clipboard. Once you get this content, you can modify it and insert it in the document according to your preferences. Here is a simple example:
private void RadRichTextBox_CommandExecuting(object sender, Telerik.Windows.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
{
    if (e.Command is PasteCommand)
    {
        // Stop the default paste command
        e.Cancel = true;
 
        // Get the contents of the clipboard
        DocumentFragment documentFragment = ClipboardEx.GetDocument();
        RadDocument document = documentFragment.ToDocument();
 
        // Apply the desired changes
        RadDocumentEditor editor = new RadDocumentEditor(document);
        editor.InsertInline(new Break(BreakType.LineBreak));
        document.Selection.SelectAll();
 
        // Insert in RadRichTextBox's document
        this.radRichTextBox.InsertFragment(new DocumentFragment(document.Selection));
    }
}

Hope this helps.

Regards,
Tanya
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
RichTextBox
Asked by
James Daresta
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Share this question
or