Working with Hyperlinks in WPF Pdf Viewer

Essential PDF Viewer provides support for 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 previous table.

Events Table

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

We can disable the hyperlink navigation in PDF viewer control by wiring the event HyperlinkClicked which is available in PdfViewerControl and PdfDocumentView like below,

//Initialize PDF Viewer.

PdfViewerControl pdfViewerControl = new PdfViewerControl();



//Load the PDF.

pdfViewerControl.Load("Sample.pdf");

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

public void PdfViewerControl_HyperlinkClicked(object sender, AnnotEventArgs e)
{

//Your code here...
            
}

// Unhooks the event handler for `HyperlinkClicked` event.
pdfViewerControl.HyperlinkClicked -= PdfViewerControl_HyperlinkClicked;
' 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?

We can get the URI which is clicked in the PDF viewer control from the AnnotEventArgs which is passed as a parameter of the HyperlinkClicked event. Please refer the below example for more details.

We can navigate to different URL irrespective of the URI clicked in the PDF viewer. Please refer the below example for more details.

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

public void PdfViewerControl_HyperlinkClicked(object sender, AnnotEventArgs 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

How to redirect to a different URL?

We can navigate to different URL irrespective of the URI clicked in the PDF viewer. Please refer the below example for more details.

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

public void PdfViewerControl_HyperlinkClicked(object sender, AnnotEventArgs e)
{
 
 // 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 will be raised when we place the mouse pointer over the URI in the PDF viewer control.

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

public void PdfViewerControl_HyperlinkMouseOver(object sender, AnnotEventArgs e)
{

//Your code here
            
}

// Unhooks 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