Hyperlink Navigation in WPF Pdf Viewer
9 Jul 20267 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. | HyperlinkMouseOverEventArgs |
| HyperlinkClicked | This event is triggered when the hyperlink in the PDF document is clicked. | HyperlinkClickedEventArgs |
NOTE
From version 19.3, the
HyperlinkClickedEventArgsis renamed to HyperlinkClickedEventArgs. Also, we recommend you to use HyperlinkClicked and HyperlinkMouseOver events to notify when the hyperlink is clicked and when the mouse is over the hyperlink respectively as shown in the above table.
How to disable hyperlink navigation in PDF viewer control
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 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
trueto disable the navigation.
using Syncfusion.Windows.PdfViewer;
// Initialize PDF Viewer.
PdfViewerControl pdfViewerControl = new PdfViewerControl();
// Load the PDF.
pdfViewerControl.Load("Sample.pdf");
// Wires the event handler for `HyperlinkClicked` event.
pdfViewerControl.HyperlinkClicked += PdfViewerControl_HyperlinkClicked;
private void PdfViewerControl_HyperlinkClicked(object sender, Syncfusion.Windows.PdfViewer.HyperlinkClickedEventArgs args)
{
// Gets or sets the value to handle the navigation of hyperlink.
args.Handled = true;
}'Initialize PDF Viewer.
Private pdfViewerControl As New PdfViewerControl()
'Load the PDF.
pdfViewerControl.Load("Sample.pdf")
' Hooks the event handler for `HyperlinkClicked` event.
AddHandler pdfViewerControl.HyperlinkClicked, AddressOf PdfViewerControl_HyperlinkClicked
Private Sub PdfViewerControl_HyperlinkClicked(obj As Object, args As Syncfusion.Windows.PdfViewer.HyperlinkClickedEventArgs)
' Gets or sets the value to handle the navigation of hyperlink.
args.Handled = True
End Sub
' Unhooks the event handler for `HyperlinkClicked` event.
RemoveHandler pdfViewerControl.HyperlinkClicked, AddressOf PdfViewerControl_HyperlinkClickedHow 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.
using Syncfusion.Windows.PdfViewer;
// Initialize PDF Viewer.
PdfViewerControl pdfViewerControl = new PdfViewerControl();
// Load the PDF.
pdfViewerControl.Load("Sample.pdf");
// Wires the event handler for `HyperlinkClicked` event.
pdfViewerControl.HyperlinkClicked += PdfViewerControl_HyperlinkClicked;
private void PdfViewerControl_HyperlinkClicked(object sender, Syncfusion.Windows.PdfViewer.HyperlinkClickedEventArgs args)
{
// Returns the URI clicked in the PDF viewer control.
string uri = args.Uri;
// Use the URI as needed; for example:
// System.Diagnostics.Debug.WriteLine(uri);
}'Initialize PDF Viewer.
Private pdfViewerControl As New PdfViewerControl()
'Load the PDF.
pdfViewerControl.Load("Sample.pdf")
' Hooks the event handler for `HyperlinkClicked` event.
AddHandler pdfViewerControl.HyperlinkClicked, AddressOf PdfViewerControl_HyperlinkClicked
Private Sub PdfViewerControl_HyperlinkClicked(obj As Object, args As Syncfusion.Windows.PdfViewer.HyperlinkClickedEventArgs)
' Returns the URI clicked in the PDF viewer control.
Dim uri As String = args.URI
' Use the URI as needed; for example:
' Console.WriteLine(uri)
End SubRedirecting to a different Hyperlink
You can navigate to a 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.
using Syncfusion.Windows.PdfViewer;
// Initialize PDF Viewer.
PdfViewerControl pdfViewerControl = new PdfViewerControl();
// Load the PDF.
pdfViewerControl.Load("Sample.pdf");
// Wires the event handler for `HyperlinkClicked` event.
pdfViewerControl.HyperlinkClicked += PdfViewerControl_HyperlinkClicked;
private void PdfViewerControl_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");
}'Initialize PDF Viewer.
Private pdfViewerControl As New PdfViewerControl()
'Load the PDF.
pdfViewerControl.Load("Sample.pdf")
' Hooks the event handler for `HyperlinkClicked` event.
AddHandler pdfViewerControl.HyperlinkClicked, AddressOf PdfViewerControl_HyperlinkClicked
Private Sub PdfViewerControl_HyperlinkClicked(obj As Object, args As Syncfusion.Windows.PdfViewer.HyperlinkClickedEventArgs)
' 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")
End SubHow to get notified when a mouse pointer moves over a hyperlink
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.
using Syncfusion.Windows.PdfViewer;
// Initialize PDF Viewer.
PdfViewerControl pdfViewerControl = new PdfViewerControl();
// Load the PDF.
pdfViewerControl.Load("Sample.pdf");
// Wires the event handler for `HyperlinkMouseOver` event.
pdfViewerControl.HyperlinkMouseOver += PdfViewerControl_HyperlinkMouseOver;
private void PdfViewerControl_HyperlinkMouseOver(object sender, Syncfusion.Windows.PdfViewer.HyperlinkMouseOverEventArgs args)
{
//Your code here
}
// Unwires the event handler for `HyperlinkMouseOver` event.
pdfViewerControl.HyperlinkMouseOver -= PdfViewerControl_HyperlinkMouseOver;'Initialize PDF Viewer.
Private pdfViewerControl As New PdfViewerControl()
'Load the PDF.
pdfViewerControl.Load("Sample.pdf")
' Hooks the event handler for `HyperlinkMouseOver` event.
AddHandler pdfViewerControl.HyperlinkMouseOver, AddressOf PdfViewerControl_HyperlinkMouseOver
Private Sub PdfViewerControl_HyperlinkMouseOver(obj As Object, args As Syncfusion.Windows.PdfViewer.HyperlinkMouseOverEventArgs)
'Your code here
End Sub
' Unhooks the event handler for `HyperlinkMouseOver` event.
RemoveHandler pdfViewerControl.HyperlinkMouseOver, AddressOf PdfViewerControl_HyperlinkMouseOverNOTE
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.