Hyperlink Navigation in WPF Pdf Viewer

7 Oct 20215 minutes to read

The WPF PDF Viewer supports URI annotations (hyperlinks) in the PDF document that enables the URI available in the PDF document to be opened in the default browser just by clicking on it. This also supports a few events that are listed in the following table.

Events Table

Event Description Arguments
HyperlinkMouseOver This event is triggered when the mouse pointer is placed over the hyperlink. N/A
HyperlinkClicked This event is triggered when the hyperlink in the PDF document is clicked. HyperlinkClickedEventArgs

NOTE

From the version 19.3, The HyperLinkClickedEventArgs is renamed to HyperlinkClickedEventArgs. Also, We recommend you to use HyperlinkClicked and HyperlinkMouseOver events to notify when the hyperlink is clicked and when mouse is over the hyperlink respectively as shown in the above table.

You can disable the hyperlink navigation in PDF viewer control by setting the value of Handled in the HyperlinkClickedEventArgs parameter as true in the HyperlinkClicked event which is available in the PdfViewerControl and PdfDocumentView class.
Please refer to the following example for more details.

NOTE

From the version 19.3, we have introduced Handled property in the event arguments to disable the hyperlink navigation based on the standards. You need to change its value to true to disable the navigation.

// Wires the event handler for `HyperlinkClicked` event.    
pdfViewerControl.HyperlinkClicked += PdfViewerControl_HyperlinkClicked;

private void Pdfviewer_HyperlinkClicked(object sender, Syncfusion.Windows.PdfViewer.HyperlinkClickedEventArgs args)
{

  // Gets or sets the value to handle the navigation of hyperlink.
  args.Handled = true;
            
}
' Hooks the event handler for `HyperlinkClicked` event

'Initialize PDF Viewer.

Private pdfViewerControl As New PdfViewerControl()



'Load the PDF.

pdfViewerControl.Load("Sample.pdf")

AddHandler pdfviewer.HyperlinkClicked, AddressOf PdfViewerControl_HyperlinkClicked

Private Sub PdfViewerControl_HyperlinkClicked(obj As Object, args As Syncfusion.Windows.PdfViewer.AnnotEventArgs)

'Your code here...

End Sub

 
' Unhooks the event handler for `HyperlinkClicked` event.
RemoveHandler pdfviewer.HyperlinkClicked, AddressOf PdfViewerControl_HyperlinkClicked

How to retrieve the clicked URI from PDF viewer

You can acquire the details of the hyperlink, which is clicked in the PDF file using the HyperlinkClickedEventArgs in the HyperlinkClicked event.
Please refer to the following example for more details.

// Wires the event handler for `HyperlinkClicked` event.    
pdfViewerControl.HyperlinkClicked += PdfViewerControl_HyperlinkClicked;

private void Pdfviewer_HyperlinkClicked(object sender, Syncfusion.Windows.PdfViewer.HyperlinkClickedEventArgs args)
{
  
 //Returns the URI clicked in the PDF viewer control.
 string URI = args.Uri; 
            
}
' Hooks the event handler for `HyperlinkClicked` event
AddHandler pdfviewer.HyperlinkClicked, AddressOf PdfViewerControl_HyperlinkClicked

Private Sub PdfViewerControl_HyperlinkClicked(obj As Object, args As Syncfusion.Windows.PdfViewer.AnnotEventArgs)

' Returns the URI clicked in the PDF viewer control.
Dim uri As String = args.URI

End Sub

You can navigate to different hyperlink irrespective of the hyperlink clicked. To redirect a hyperlink, you need to set the value of Handled parameter as true to stop the navigation of the hyperlink clicked. Then open the desired hyperlink that you want to navigate instead of the hyperlink clicked.
Please refer to the following example for more details.

// Wires the event handler for `HyperlinkClicked` event.    
pdfViewerControl.HyperlinkClicked += PdfViewerControl_HyperlinkClicked;

private void Pdfviewer_HyperlinkClicked(object sender, Syncfusion.Windows.PdfViewer.HyperlinkClickedEventArgs args)
{

 // Gets or sets the value to handle the navigation of hyperlink.
  args.Handled = true;

 // Opens the URI (www.google.com) in a default browser.
 System.Diagnostics.Process.Start("www.google.com");
             
}
' Hooks the event handler for `HyperlinkClicked` event.
AddHandler pdfviewer.HyperlinkClicked, AddressOf PdfViewerControl_HyperlinkClicked

Private Sub PdfViewerControl_HyperlinkClicked(obj As Object, args As Syncfusion.Windows.PdfViewer.AnnotEventArgs)

' Opens the URI (www.google.com) in a default browser.
System.Diagnostics.Process.Start("www.google.com")

End Sub

HyperlinkMouseOver event is triggered when you place the mouse pointer over the URI in the PDF viewer control.
Please refer to the following example for more details.

// Wire the event handler for `HyperlinkMouseOver` event.    
pdfViewerControl.HyperlinkMouseOver += PdfViewerControl_HyperlinkMouseOver;

private void Pdfviewer_HyperlinkMouseOver(object sender, EventArgs args)
{

//Your code here
            
}

// Unwire the event handler for `HyperlinkMouseOver` event.
pdfViewerControl.HyperlinkMouseOver -= PdfViewerControl_HyperlinkMouseOver;
' Hooks the event handler for `HyperlinkMouseOver` event
AddHandler pdfviewer.HyperlinkMouseOver, AddressOf PdfViewerControl_HyperlinkMouseOver

Private Sub PdfViewerControl_HyperlinkMouseOver(obj As Object, args As Syncfusion.Windows.PdfViewer.AnnotEventArgs)

End Sub

 
' Unhooks the event handler for `HyperlinkMouseOver` event.
RemoveHandler pdfviewer.HyperlinkMouseOver, AddressOf PdfViewerControl_HyperlinkMouseOver

NOTE

You can refer to our WPF PDF Viewer feature tour page for its groundbreaking feature representations. You can also explore our WPF PDF Viewer example to know how to render and configure the pdfviewer.