Working with Hyperlinks in Windows Forms PDF Viewer (PdfViewerControl)

PDF Viewer provides support for URLs (hyperlinks) in the PDF document that enables the navigation to the destination by opening it in the default browser just by clicking on it. This also supports a few events that are listed in the below 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 PdfViewerControl by wiring the event HyperlinkClicked. The below code example illustrates the same.

//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...
            
}
' 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 clicked URL details from PDF Viewer?

We can acquire the details of the URL which is clicked in the PDF file using the the AnnotEventArgs, which is passed as a parameter of the HyperlinkClicked event. The below code example illustrates the same.

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

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

How to redirect to a different URL?

We can navigate to different URL irrespective of the one clicked. The below code example illustrates the same.

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