Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can hyperlinks work in XPS files displayed in WPF controls?

Tags:

hyperlink

wpf

xps

I'm trying to create a help system for a software application. The interface is written in WPF. I have an XPS file (produced from a Word doc) that I want to access from the application. The XPS file contains hyperlinks that redirect within the XPS file. I can display the file using the DocumentViewer control, but the hyperlinks don't work. (When I view the same XPS file in the XPS Viewer, the hyperlinks work.) I'm new to WPF, so I may be overlooking something, but I've been trying to make this work for a week now and though I'm learning along the way I'm not getting anywhere with the task at hand. I'd greatly appreciate any help. -Dave

like image 624
Dave Avatar asked Oct 25 '22 23:10

Dave


1 Answers

Add the following code in your code behind to handle the hyperlinks manually:

public MainWindow() {
    xpsViewer.AddHandler(Hyperlink.RequestNavigateEvent, new RequestNavigateEventHandler(OnRequestNavigate));
}

private void OnRequestNavigate(object sender, RequestNavigateEventArgs e) {
    // URI contains the page number (e.Uri = "...#PG_7_LNK_2")
    int pageNumber;
    if (int.TryParse(Regex.Match(e.Uri.ToString(), @"(?<=PG_)[0-9]+").Value, out pageNumber)) {
        xpsViewer.GoToPage(pageNumber);
    }
}
like image 123
Michael Guntli Avatar answered Nov 13 '22 00:11

Michael Guntli