locked
RichEditBox Hyperlink issue when loading rtf data RRS feed

  • Question

  • App Overview

    I have created a simple application with a RichEditBox as an editor and viewer.

    I have added a load and save button to save the contents of the RichEditBox as an RTF file – The load and save code was taken from msdn - http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.richeditbox.aspx

    I have also added a simple hyperlink button to the app.  This takes the selected/highlighted text from the RichEditBox and changes it to a rtf hyperlink.

    Procedure

    1  In the App type a url (example here is http://www.bing.com).  Highlight the text and click the Hyperlink button.
    2  The link can then be clicked (ctrl + click)
    3  Save the rtf file to desktop
    4  Open the rtf file that was just saved – the hyperlink is still present
    5  Add a second hyperlink to the RichEditBox (http://www.microsoft.com).  Both Bing and Microsoft websites will load.
    6  Save the RTF file to desktop
    7  Load the 2<sup>nd</sup> rtf file saved into the RichEditBox -  Now the Bing hyperlink does not show.  The Microsoft hyperlink does show and function.

    Issue

    It appears that the RichEditBox is populated through the Document.LoadFromStream and then saved with Document.SaveFromStream "it does not remember" hyperlinks that were loaded.

    Only hyperlinks that are added in the RichEditBox after the load are saved and work correctly.

    If you open the saved rtf's from the app in Word at each stage the results match the app and the hyperlinks do not work.  If you open the exact files in WordPad then the hyperlinks are shown at each stage and work correctly.

    What is also interesting is that if you change the first step in the procedure and create the original document in Word and the hyperlink text is not the same as the url (Example Text = "Link Here", URL = http://bing.com/news).  Then this hyperlink remains correct through all other steps - as shown below

    Code Below -  I am happy to provide the example visual studio project if required

    xaml

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <StackPanel>
                <Button x:Name="Load" Click="Load_Clicked" Content="Load" Margin="10,10,0,20"/>
                
                <RichEditBox x:Name="REB1" Height="500" Width="800" HorizontalAlignment="Left"/>
               
                <Button x:Name="Hyperlink" Click="Link_Clicked" Content="Hyperlink" Margin="10,20,0,0"/> 
                
                <Button x:Name="Save" Click="Save_Clicked" Content="Save" Margin="10,20,0,0"/>
                
            </StackPanel>
            
        </Grid>


    c#

            private async void Save_Clicked(object sender, RoutedEventArgs e)
            {
                FileSavePicker save = new FileSavePicker();
                save.FileTypeChoices.Add("Rich Text", new List<string>(){".rtf"});
                save.SuggestedFileName = "Saved RTF";
    
                StorageFile file = await save.PickSaveFileAsync();
    
                if( file != null)
                {
                    IRandomAccessStream ranAccStream = await file.OpenAsync(FileAccessMode.ReadWrite);
    
                    REB1.Document.SaveToStream(Windows.UI.Text.TextGetOptions.FormatRtf, ranAccStream);
    
    
    
                }
    
                Windows.UI.Popups.MessageDialog saveBox = new Windows.UI.Popups.MessageDialog("File Saved");
                await saveBox.ShowAsync();
            }
    
            private async void Load_Clicked(object sender, RoutedEventArgs e)
            {
                FileOpenPicker open = new FileOpenPicker();
                open.FileTypeFilter.Add(".rtf");
                
                StorageFile file = await open.PickSingleFileAsync();
    
                if (file != null)
                {
                    IRandomAccessStream randAccStream = await file.OpenAsync(FileAccessMode.Read);
    
    
                    REB1.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, randAccStream);
                }
            }
    
            private void Link_Clicked(object sender, RoutedEventArgs e)
            {
                //We take the selected Text
                ITextSelection selection = REB1.Document.Selection;
                //We extract the Text/String value to determine if any text is selected.  If so we only Bold this.
                string selectedText = selection.Text;
    
                //We set the formatting now
                ITextCharacterFormat charFormatting = selection.CharacterFormat;
    
                REB1.Document.GetRange(selection.StartPosition, selection.EndPosition).Link = "\"" + selectedText + "\"";
                
            }

    Is this a known issue?
    Are you able to suggest any other methods for loading/saving rtf's in a RichEditBox so that the hyperlinks show consistently?


    Wednesday, January 15, 2014 11:00 PM

Answers

  • Daniel - I'm going to ping you offline.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Thursday, January 16, 2014 9:10 PM
    Moderator