none
Hyperlinks inside a FlowDocument

    Question

  • Hi!!

    In a FlowDocument, is it possible - programmaticaly - to create a hyperlink to a paragraph/section/table within the same document? 

    Many thanks!!
    --
    Pedro
    Friday, October 24, 2008 5:04 PM

Answers

  • Hi Pedro,

    In order to enable hyperlinks, you have to host your page in some sort of navigationwindow or frame.
    the following works for me.  i made the frame really small so i can test that clicking on the hyperlink gets you to the desired text fragment.  that's the only thing i tested though but you can go from here.

    Window1.xaml

    <Window x:Class="FDRDemo.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window1" Height="600" Width="600">

        <Grid>

            <Frame x:Name="TheFrame" Width="300" Height="100" Source="FirstPage.xaml"/>

            

        </Grid>

    </Window>



    FirstPage.xaml

    <Page x:Class="FDRDemo.FirstPage"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="FirstPage">

        <FlowDocumentReader xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>

            <FlowDocument>

                <Paragraph>This is some link

                    <Hyperlink NavigateUri="#here">Link</Hyperlink>

                </Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph Name="here">here you go.</Paragraph>

            </FlowDocument>

        </FlowDocumentReader>

    </Page>



    Evan Chua-Yap, dotnetfreelanzer at yahoo dot com
    • Proposed as answer by Evan Chua-Yap Tuesday, October 28, 2008 1:52 PM
    • Marked as answer by pedrompereira Tuesday, October 28, 2008 4:04 PM
    Tuesday, October 28, 2008 1:52 PM

All replies

  •  

     

    Hi,

     

    It is possible.

    Here is a small example of creating hyperlink to paragraph/section/table.

     

    In order to navigate to website, we can create a Frame  Control for navigation. The hierarchical relationship of  elements in this example is like this :

     

    Frame-->FlowDocument-->Table-->Section-->Paragraph-->Hyperlink

     

    In the code behind:

    public Window1()

            {

                InitializeComponent();

     

                // add a Frame for navigation

                Frame frame = new Frame();

                this.Content = frame;

                //add FlowDocument

                FlowDocument doc = new FlowDocument();

                frame.Navigate(doc);

     

                //add Table

                Table table = new Table();

                doc.Blocks.Add(table );

                TableRowGroup group = new TableRowGroup();

                table.RowGroups.Add(group );

     

                TableColumn col1 = new TableColumn();

                TableColumn col2 = new TableColumn();

                table.Columns.Add(col1 );

                table.Columns.Add(col2);

     

                TableRow row1 = new TableRow();

                TableCell cel1 = new TableCell();

                row1.Cells.Add(cel1);

     

                group.Rows.Add(row1);

     

                //add Section

                Section mySection = new Section();

                //add section to the Table cell.

                cel1.Blocks.Add(mySection);

     

                Paragraph paraValue = new Paragraph();

                Hyperlink hl = new Hyperlink(new Run("Click Here to Google"));

                hl.Foreground = Brushes.Red;

                paraValue.Inlines.Add(hl);

     

                hl.FontSize = 11;

                hl .NavigateUri =new Uri ("Http://www.google.cn");

     

                mySection.Blocks.Add(paraValue);

            }

    If you have any additional question about this,please feel free to ask.

    Thanks.

     

     


    Jim Zhou -MSFT
    • Edited by Jim Zhou - MSFT Monday, October 27, 2008 9:08 AM modify text font
    • Marked as answer by pedrompereira Monday, October 27, 2008 10:27 AM
    • Unmarked as answer by pedrompereira Monday, October 27, 2008 10:31 AM
    • Marked as answer by Jim Zhou - MSFT Tuesday, October 28, 2008 2:00 AM
    • Unmarked as answer by pedrompereira Tuesday, October 28, 2008 9:06 AM
    Monday, October 27, 2008 9:07 AM
  • Hi Jim!

    Thanks for your answer, but (I think I did not express myself the right way) I was looking for a way to insert a link on a document which points to another "zone" inside the same document (e.g. a link to a target 6 paragraphs bellow). 

    Much like the following example in html:

    <a href="#mylink">Inner link</a> 
    foo 
    foo 
    foo 
    <a name="mylink">target</a> 

    Best regards
    Pedro
    Monday, October 27, 2008 10:27 AM
  •  <Paragraph>This is some link <Hyperlink NavigateUri="#here">Link</Hyperlink> </Paragraph>

      <Paragraph Name="here">here you go.</Paragraph>
    Monday, October 27, 2008 3:02 PM
  • have u looked at the fragment navigation sample in

    http://msdn.microsoft.com/en-us/library/ms771338.aspx  ?
    Monday, October 27, 2008 3:13 PM
  • LesterLobo, looks like it doesn't work in a FlowDocument (using FlowDocumentReader).


    Monday, October 27, 2008 3:55 PM
  • Evan, the "Fragment Navigation Sample" you pointed to, does more or less the trick, but it uses a StackPanel to build the text instead of a FlowDocument. 
    Thanks anyway

    Monday, October 27, 2008 4:01 PM
  • why doesnt it work? i am using the following sample

    <
    FlowDocumentReader xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'

    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Height="120">

    <FlowDocument>

    <Paragraph>This is some link <Hyperlink NavigateUri="#here">Link</Hyperlink> </Paragraph>

    <Paragraph>some text.</Paragraph>

    <Paragraph>some text.</Paragraph>

    <Paragraph>some text.</Paragraph>

    <Paragraph>some text.</Paragraph>

    <Paragraph>some text.</Paragraph>

    <Paragraph>some text.</Paragraph>

    <Paragraph>some text.</Paragraph>

    <Paragraph>some text.</Paragraph>

    <Paragraph Name="here">here you go.</Paragraph>

    </FlowDocument>

    </FlowDocumentReader>

    • Marked as answer by Jim Zhou - MSFT Tuesday, October 28, 2008 2:00 AM
    • Unmarked as answer by pedrompereira Tuesday, October 28, 2008 9:21 AM
    Monday, October 27, 2008 7:55 PM

  • Hi Lester, 
     
    I don't know why is this not working, but here goes the entire snipet of my window: 
     
    <Window x:Class="WpfApplication2.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Window1" Height="300" Width="300"
            <FlowDocumentReader xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
                <FlowDocument> 
                    <Paragraph>This is some link <Hyperlink NavigateUri="#here">Link</Hyperlink></Paragraph
                    <Paragraph>some text.</Paragraph> 
                    <Paragraph>some text.</Paragraph> 
                    <Paragraph>some text.</Paragraph> 
                    <Paragraph>some text.</Paragraph> 
                    <Paragraph>some text.</Paragraph> 
                    <Paragraph>some text.</Paragraph> 
                    <Paragraph>some text.</Paragraph> 
                    <Paragraph>some text.</Paragraph> 
                    <Paragraph Name="here">here you go.</Paragraph> 
                </FlowDocument> 
            </FlowDocumentReader> 
    </Window> 
     
    When I click on the first paragraph's link, the FlowDocumentReader does not scroll down to the last paragraph - actualy it does nothing... 
     
    Am I doing something wrong? 

    Tuesday, October 28, 2008 9:21 AM
  • Hi Pedro,

    In order to enable hyperlinks, you have to host your page in some sort of navigationwindow or frame.
    the following works for me.  i made the frame really small so i can test that clicking on the hyperlink gets you to the desired text fragment.  that's the only thing i tested though but you can go from here.

    Window1.xaml

    <Window x:Class="FDRDemo.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window1" Height="600" Width="600">

        <Grid>

            <Frame x:Name="TheFrame" Width="300" Height="100" Source="FirstPage.xaml"/>

            

        </Grid>

    </Window>



    FirstPage.xaml

    <Page x:Class="FDRDemo.FirstPage"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="FirstPage">

        <FlowDocumentReader xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>

            <FlowDocument>

                <Paragraph>This is some link

                    <Hyperlink NavigateUri="#here">Link</Hyperlink>

                </Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph>some text.</Paragraph>

                <Paragraph Name="here">here you go.</Paragraph>

            </FlowDocument>

        </FlowDocumentReader>

    </Page>



    Evan Chua-Yap, dotnetfreelanzer at yahoo dot com
    • Proposed as answer by Evan Chua-Yap Tuesday, October 28, 2008 1:52 PM
    • Marked as answer by pedrompereira Tuesday, October 28, 2008 4:04 PM
    Tuesday, October 28, 2008 1:52 PM
  • Hi Evan, thanks a lot for your help. 

    This way (using the Frame container), it works for me too...

    Tuesday, October 28, 2008 4:03 PM