InsertInline, Hyperlinks, and a custom format provider

3 posts, 1 answers
  1. Jonathan
    Jonathan avatar
    2 posts
    Member since:
    Oct 2010

    Posted 28 Dec 2010 Link to this post

    Hi,

    In my custom format provider I'm running into a bit of difficulty during the export process in identifying which spans of the RadDocument are Hyperlinks from a document created by the RadRichTextBox. To insert a Hyperlink into the content of the text box I am using this code:

    string url = "http://www.example.ext";

    DocModel.Hyperlink link =
    new DocModel.Hyperlink(rtbText.Document.Selection.GetSelectedText(), url, DocModel.HyperlinkTargets.Blank);
     
    rtbText.InsertInline(link);

    The Hyperlink appears in the text box and is usable. The Telerik Xaml and Html format providers export  the link properly.

    In the export method of my own implementation of IDocumentFormatProvider, I am trying to identify the RadDocument's Hyperlinks in this way:

    foreach (Section section in document.Sections)
    {
        foreach (Paragraph paragraph in section.Paragraphs)
        {
            foreach (Span span in paragraph.Children)
            {
                if (span is Hyperlink)
                {
                    // process hyperlink here
                }
            }
        }
    }

    When I'm stepping through this code in the debugger, I can see that the Span I expect to be a Hyperlink does have some properties set (e.g. URL) that I would see in a Hyperlink, however the span is actually a Span object and the properties I'm seeing are internal so I can't access them.

    What is the preferred way for identifying hyperlinks while exporting a RadDocument in a custom format provider? If I construct a RadDocument which includes a Hyperlink by using the import method of my format provider (by adding a Hyperlink object to the Children collection of a Paragraph) and then run this document through the export method of my format provider, I'm seeing the Hyperlink objects as expected. When I insert new Hyperlinks with the InsertInline method on the rich text box I am not able to identify the Hyperlink objects during document export.

    I'm using version 2010.2.812.1040.

    Thanks,
    Jonathan
  2. Answer
    Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 29 Dec 2010 Link to this post

    Hi Jonathan,

     I have tried running your code with Q2 SP2 and it seems to work fine. All you have to do in order to access the URL property of the Hyperlink is to cast  span to Hyperlink after you have verified that it is actually a Span:

    RadDocument document = this.radRichTextBox1.Document;
    foreach (Section section in document.Sections)
    {
        foreach (Paragraph paragraph in section.Paragraphs)
        {
            foreach (Span span in paragraph.Children)
            {
                if (span is Hyperlink)
                {
                    Hyperlink link = (Hyperlink) span;
                    MessageBox.Show(link.URL);
                }
            }
        }
    }
    As for your other question, the preferred way to manipulate the document structure is rather through the Inlines property of Paragraph, the Insert and InsertInline methods of RadRichTextBox, etc., than adding elements to the Children properties of the elements.

    Kind regards,
    Iva
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  3. Jonathan
    Jonathan avatar
    2 posts
    Member since:
    Oct 2010

    Posted 29 Dec 2010 Link to this post

    Hi Iva:

    Upgrading to Q2 SP2 solved the problem.

    Thanks,
    Jonathan
Back to Top