How to remove Hyperlink Info from RadDocument on Paste?

1 Answer 17 Views
RichTextBox
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
Bob asked on 21 Mar 2024, 07:18 PM

I have to scrub pasted fragments from web sources to remove any hyperlinks in those fragments but keep the text associated with the link if any.  I have tried the following code which gets the proper hyperlink elements, but this is not removing the hyperlink info.   Please let me know the proper method of finding all hyperlinks in the pasted document content and removing those links.  Thanks.


            var richTextBox = new RadRichTextBox();
            richTextBox.InsertFragment(document.Selection.CopySelectedDocumentElements());
            richTextBox.Document.Selection.SelectAll();

            var boxes = richTextBox.Document.Selection.GetSelectedBoxes().Where(b => b.AssociatedInline.FieldStart != null && b.AssociatedInline.FieldStart.GetType() == typeof(HyperlinkRangeStart));
            foreach (var box in boxes)
            {
                richTextBox.Document.CaretPosition.MoveToInline(box.AssociatedInline);
                richTextBox.RemoveHyperlink();
            }

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 22 Mar 2024, 08:15 AM

Hi Bob,

I will recommend using the following appraoch: 

private void RadRichTextBoxCommandExecuting(object sender, CommandExecutingEventArgs e)
{
    if (e.Command is PasteCommand)
    { 
        e.Cancel = true;
        PasteNewText();
    } 
}
 
public void PasteNewText()
{
    DocumentFragment clipboardDocument = null;
    string clipboardText = null;
    bool clipboardContainsData = false;

    if (ClipboardEx.ContainsDocument(null))
    {
        clipboardDocument = ClipboardEx.GetDocument();
        clipboardContainsData = true;
    }
    else if (ClipboardEx.ContainsText(null))
    {
        clipboardText = ClipboardEx.GetText(null);
        clipboardContainsData = true;
    }

    if (!clipboardContainsData)
    {
        return;
    } 

    if (clipboardDocument != null)
    {
        RadDocument doc = new RadDocument();
        RadDocumentEditor editor = new RadDocumentEditor(doc);
        editor.InsertFragment(clipboardDocument);

        var links = doc.GetAnnotationMarkersOfType<HyperlinkRangeStart>();
        foreach (var item in links.ToList())
        {
            editor.DeleteAnnotationRange(item);
        }

        this.radRichTextBox.ActiveDocumentEditor.InsertFragment(new DocumentFragment(doc));
    }
    else if (!string.IsNullOrEmpty(clipboardText))
    {
        this.radRichTextBox.ActiveDocumentEditor.Insert(clipboardText);
    }
} 

Let me know what you think.

Regards,
Dimitar
Progress Telerik

A brand new ThemeBuilder course was just added to the Virtual Classroom. The training course was designed to help you get started with ThemeBuilder for styling Telerik and Kendo UI components for your applications. You can check it out at https://learn.telerik.com
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
commented on 23 Mar 2024, 07:11 PM

Thank you, this worked perfectly!
Tags
RichTextBox
Asked by
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
Answers by
Dimitar
Telerik team
Share this question
or