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

how to insert hyperlinked image at current caret position

1 Answer 95 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Byron
Top achievements
Rank 1
Byron asked on 22 Mar 2014, 02:47 PM
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;
06. 
07. ImageInline image = new ImageInline();
08. image.UriSource = new Uri(linkFormatter.FormatAllImageHyperlink(link));
09. image.Size = new Size(16, 16);
10.  
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));
17. 
18. //activeParagraph.Inlines.Add(hyperlinkStart);
19. //activeParagraph.Inlines.Add(image);
20. //activeParagraph.Inlines.Add(hyperlinkEnd);
21. 
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.

1 Answer, 1 is accepted

Sort by
0
Missing User
answered on 26 Mar 2014, 05:28 PM
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);
    this.editor.InsertInline(image);
 
    this.editor.Document.CaretPosition.MoveToPreviousInline();
    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);
 
        start.MoveToStartOfDocumentElement(imageInDoc);
        end.MoveToEndOfDocumentElement(imageInDoc);
        end.MoveToNext();
 
        this.editor.Document.Selection.AddSelectionStart(start);
        this.editor.Document.Selection.AddSelectionEnd(end);
 
        HyperlinkInfo hyperlinkInfo = new HyperlinkInfo() { NavigateUri = "http://www.telerik.com", Target = HyperlinkTargets.Blank };
        this.editor.InsertHyperlink(hyperlinkInfo);
    }
}

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.
 
Regards,
Yancho
Telerik
 

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.

 
Tags
RichTextBox
Asked by
Byron
Top achievements
Rank 1
Answers by
Missing User
Share this question
or