I am trying to use a WPF application in C# to display a RTF document which contains some relative links. To process the links in the document I found the following code online. I have verified that a link to the URL www.google.com works. Now the problem is the RTF contains a few links to other pages, such as a link to the bookmark "Pag2". How do i get those to work?
//Code to process the links:
using System.Windows.Documents;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using System.Diagnostics;
#region Activate Hyperlinks in the Rich Text box
static class Hyperlinks
{
//http://stackoverflow.com/questions/5465667/handle-all-hyperlinks-mouseenter-event-in-a-loaded-loose-flowdocument
public static void SubscribeToAllHyperlinks(FlowDocument flowDocument)
{
var hyperlinks = GetVisuals(flowDocument).OfType<Hyperlink>();
foreach (var link in hyperlinks)
link.RequestNavigate += new System.Windows.Navigation.RequestNavigateEventHandler(link_RequestNavigate);
}
public static IEnumerable<DependencyObject> GetVisuals(DependencyObject root)
{
foreach (var child in LogicalTreeHelper.GetChildren(root).OfType<DependencyObject>())
{
yield return child;
foreach (var descendants in GetVisuals(child))
yield return descendants;
}
}
public static void link_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
//http://stackoverflow.com/questions/2288999/how-can-i-get-a-flowdocument-hyperlink-to-launch-browser-and-go-to-url-in-a-wpf
if (e.Uri.IsAbsoluteUri)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
}
else
{
// code to navigate to relative uri such as #Page2??
}
e.Handled = true;
}
}
#endregion Activate Hyperlinks in the Rich Text box
//Calling code:
class FlowDocTest_VM
{
public FlowDocTest_VM()
{
string filename = "D:\\Manual.rtf";
fdContent = new FlowDocument();
FileStream fsHelpfile = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
TextRange trContent = new TextRange(fdContent.ContentStart, fdContent.ContentEnd);
trContent.Load(fsHelpfile, DataFormats.Rtf);
Hyperlinks.SubscribeToAllHyperlinks(fdContent);
}
public FlowDocument Document
{
get
{
return fdContent;
}
set
{
fdContent = value;
}
}
private FlowDocument fdContent;
}