how to insert hyperlinked image at current caret position

2 posts, 0 answers
  1. Byron
    Byron avatar
    3 posts
    Member since:
    Feb 2014

    Posted 22 Mar 2014 Link to this post

    01.HyperlinkRangeStart hyperlinkStart = new HyperlinkRangeStart();
    02. HyperlinkRangeEnd hyperlinkEnd = new HyperlinkRangeEnd();
    03. hyperlinkEnd.PairWithStart(hyperlinkStart);
    04. HyperlinkInfo hyperlinkInfo = new HyperlinkInfo() { NavigateUri = hrefURL, Target = HyperlinkTargets.Blank };
    05. hyperlinkStart.HyperlinkInfo = hyperlinkInfo;
    07. ImageInline image = new ImageInline();
    08. image.UriSource = new Uri(linkFormatter.FormatAllImageHyperlink(link));
    09. image.Size = new Size(16, 16);
    11. Telerik.Windows.Documents.Model.Paragraph activeParagraph = Document.CaretPosition.GetCurrentParagraphBox().AssociatedParagraph;
    12. DocumentElementCollection docElemColl = new DocumentElementCollection(activeParagraph);
    13. docElemColl.Add(hyperlinkStart);
    14. docElemColl.Add(image);
    15. docElemColl.Add(hyperlinkEnd);
    16. radRichTextBox.InsertHyperlink(hyperlinkStart.HyperlinkInfo, new Telerik.Windows.Documents.Model.InlineCollection(docElemColl));
    18. //activeParagraph.Inlines.Add(hyperlinkStart);
    19. //activeParagraph.Inlines.Add(image);
    20. //activeParagraph.Inlines.Add(hyperlinkEnd);
    22. //Telerik.Windows.Documents.Model.Inline inline = Document.CaretPosition.GetCurrentInline();
    23. //inline.Children.Add(hyperlinkStart);
    24. //inline.Children.Add(image);
    25. //inline.Children.Add(hyperlinkEnd);
    Everything that I've tried results in a crash, with the exception being: Unable to cast object of type 'Telerik.Windows.Documents.Model.HyperlinkRangeEnd' to type 'Telerik.Windows.Documents.Model.Span'.  The two commented out sections were previous attempts at adding the hyperlinked image to the document at the current caret position.

  2. Yancho
    Yancho avatar
    72 posts

    Posted 26 Mar 2014 Link to this post

    Hi Byron,

    Thank you for your interest in Telerik RadRichTextBox!

    If you are going to insert a hyperlinked image at the current caret position, please take a look at the following code-snippet which illustrates how this could be achieved on button click event handler:
    private void InsertHyperlinkedImage()
        ImageInline image = new ImageInline(new Uri("/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/32/Picture.png", UriKind.Relative));
        image.Size = new Size(32, 32);
        var imageInDoc = this.editor.Document.CaretPosition.GetCurrentInline() as ImageInline;
        if (imageInDoc != null)
            DocumentPosition start = new DocumentPosition(this.editor.Document);
            DocumentPosition end = new DocumentPosition(this.editor.Document);
            HyperlinkInfo hyperlinkInfo = new HyperlinkInfo() { NavigateUri = "", Target = HyperlinkTargets.Blank };

    Basically, my recommendation is to insert the image first then select it and insert the hyperlink around it. Please note that changing the document collections of a measured (shown in RadRichTextBox) document is not recommended and you should use RadRichTextBox's methods instead.

    I hope this helps! Let me know how it goes.

    Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

  3. DevCraft banner
Back to Top