Pasting MS Word Formatted Fragments

2 posts, 0 answers
  1. James Daresta
    James Daresta avatar
    62 posts
    Member since:
    Sep 2009

    Posted 31 May 2016 Link to this post

    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?
  2. Tanya
    Tanya avatar
    583 posts

    Posted 02 Jun 2016 Link to this post

    Hi James,

    You could filter the pasted 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));
            // Insert in RadRichTextBox's document
            this.radRichTextBox.InsertFragment(new DocumentFragment(document.Selection));

    Hope this helps.

    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top