Working with Hyperlinks in Windows Forms PDF Viewer (PdfViewerControl)
15 Nov 20225 minutes to read
The WinForms PDF Viewer supports URLs (hyperlinks) in the PDF document, which allows you to navigate to the destination just by clicking on it and by opening it in the default browser. This also supports a few events that are listed in the following table.
Events Table
Event | Description | Arguments |
---|---|---|
This event is triggered when the mouse pointer is placed over the hyperlink. | HyperlinkMouseOverEventArgs | |
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 have renamed theHyperLinkClicked
andHyperLinkMouseOver
events to HyperlinkClicked and HyperlinkMouseOver respectively. Also, We have introduced HyperlinkMouseOverEventArgs to view the details of hyperlink when mouse is over.
How to disable hyperlink navigation in PDF Viewer
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.
//Initialize PDF Viewer.
PdfViewerControl pdfViewerControl = new PdfViewerControl();
// Wires the event handler for `HyperlinkClicked` event.
pdfViewerControl.HyperlinkClicked += PdfViewerControl_HyperlinkClicked;
private void PdfViewerControl_HyperlinkClicked(object sender, Syncfusion.Windows.Forms.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 pdfViewerControl.HyperlinkClicked, AddressOf PdfViewerControl_HyperlinkClicked
Private Sub PdfViewerControl_HyperlinkClicked(obj As Object, args As Syncfusion.Windows.PdfViewer.AnnotEventArgs)
'Your code here...
End Sub
How to retrieve the hyperlink details from PDF Viewer
You can retrieve the hyperlink details from PDF Viewer by using HyperlinkClickedEventArgs when the hyperlink is clicked and by using HyperlinkMouseOverEventArgs when the mouse is over the hyperlink.
HyperlinkClicked Event
You can acquire the details of the hyperlink which is clicked in the PDF file by 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 PdfViewerControl_HyperlinkClicked(object sender, Syncfusion.Windows.Forms.PdfViewer.HyperlinkClickedEventArgs args)
{
//Returns the URI clicked in the PDF viewer control.
string URI = args.Uri;
}
' 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.AnnotEventArgs)
' Returns the URI clicked in the PDF viewer control.
Dim uri As String = args.URI
End Sub
HyperlinkMouseOver Event
You can acquire the details of the hyperlink which is hovered in the PDF file by using the HyperlinkMouseOverEventArgs in the HyperlinkMouseOver event.
Please refer to the below example for more details.
// Hooks the event handler for `HyperlinkMouseOver` event.
pdfViewerControl.HyperlinkMouseOver += PdfViewerControl_HyperlinkMouseOver;
private void PdfViewerControl_HyperlinkMouseOver(object sender, Syncfusion.Windows.Forms.PdfViewer.HyperlinkMouseOverEventArgs e)
{
//Returns the URI hovered in the PDF viewer control.
string URI = e.Uri;
}
Redirecting to a different Hyperlink
You can navigate to different hyperlink irrespective of the hyperlink clicked. To redirect a hyperlink, you need to pass 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 PdfViewerControl_HyperlinkClicked(object sender, Syncfusion.Windows.Forms.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 pdfViewerControl.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