Skip to content

Commit b68432a

Browse files
committed
HyperlinkClicked event for RadPdfViewer
1 parent bbc2eec commit b68432a

File tree

1 file changed

+122
-76
lines changed

1 file changed

+122
-76
lines changed
Lines changed: 122 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
---
2-
title: Annotations
3-
page_title: Annotations - WinForms PdfViewer Control
4-
description: WinForms PdfViewer supports Link annotations, which means that if you open a PDF file that includes hyperlinks to absolute URIs, you can click them and have a window open, navigated to the respective address.
5-
slug: winforms/pdfviewer/annotations
6-
tags: annotations
7-
published: True
8-
position: 0
9-
---
10-
11-
# Annotations
12-
__RadPdfViewer__ supports link annotations, which means that if you open a PDF file that includes hyperlinks to absolute URIs, you can click them and have a window open, navigated to the respective address. In addition, if there are links pointing to bookmarks in the same document, the view port will be scrolled to the destination specified in the link.
13-
14-
The current API includes the following members, which allow customization of the default behavior or implementing custom logic:
15-
16-
* __AnnotationClicked__ event of __RadPdfViewer__: This event is fired when you click on an annotation such as a hyperlink. It comes handy when you want to detect or even cancel the opening of a web page. The __AnnotationEventArgs__ contain the Annotation as property and the Link itself has information of its Action, i.e. if it is a UriAction. Handling the event in the following manner will not only show the Uri of each clicked link as the text of a MessageBox, but will also cancel the default behavior.
17-
18-
#### AnnotationClicked Event Handler
19-
20-
{{source=..\SamplesCS\PdfViewer\PdfAnnotations.cs region=AnnotationClicked}}
21-
{{source=..\SamplesVB\PdfViewer\PdfAnnotations.vb region=AnnotationClicked}}
22-
1+
---
2+
title: Annotations
3+
page_title: Annotations - WinForms PdfViewer Control
4+
description: WinForms PdfViewer supports Link annotations, which means that if you open a PDF file that includes hyperlinks to absolute URIs, you can click them and have a window open, navigated to the respective address.
5+
slug: winforms/pdfviewer/annotations
6+
tags: annotations
7+
published: True
8+
position: 0
9+
---
10+
11+
# Annotations
12+
__RadPdfViewer__ supports link annotations, which means that if you open a PDF file that includes hyperlinks to absolute URIs, you can click them and have a window open, navigated to the respective address. In addition, if there are links pointing to bookmarks in the same document, the view port will be scrolled to the destination specified in the link.
13+
14+
The current API includes the following members, which allow customization of the default behavior or implementing custom logic:
15+
16+
* __AnnotationClicked__ event of __RadPdfViewer__: This event is fired when you click on an annotation such as a hyperlink. It comes handy when you want to detect or even cancel the opening of a web page. The __AnnotationEventArgs__ contain the Annotation as property and the Link itself has information of its Action, i.e. if it is a UriAction. Handling the event in the following manner will not only show the Uri of each clicked link as the text of a MessageBox, but will also cancel the default behavior.
17+
18+
#### AnnotationClicked Event Handler
19+
20+
{{source=..\SamplesCS\PdfViewer\PdfAnnotations.cs region=AnnotationClicked}}
21+
{{source=..\SamplesVB\PdfViewer\PdfAnnotations.vb region=AnnotationClicked}}
22+
2323
````C#
2424

2525
private void radPdfViewer1_AnnotationClicked(object sender, Telerik.Windows.Documents.Fixed.Model.Annotations.EventArgs.AnnotationEventArgs e)
@@ -37,8 +37,8 @@ private void radPdfViewer1_AnnotationClicked(object sender, Telerik.Windows.Docu
3737
MessageBox.Show(a.Uri.ToString());
3838
e.Handled = true;
3939
}
40-
41-
````
40+
41+
````
4242
````VB.NET
4343
Private Sub radPdfViewer1_AnnotationClicked(sender As Object, e As Telerik.Windows.Documents.Fixed.Model.Annotations.EventArgs.AnnotationEventArgs)
4444
Dim l As Telerik.Windows.Documents.Fixed.Model.Annotations.Link = TryCast(e.Annotation, Telerik.Windows.Documents.Fixed.Model.Annotations.Link)
@@ -52,18 +52,64 @@ Private Sub radPdfViewer1_AnnotationClicked(sender As Object, e As Telerik.Windo
5252
MessageBox.Show(a.Uri.ToString())
5353
e.Handled = True
5454
End Sub
55-
56-
````
57-
58-
{{endregion}}
59-
60-
* __Annotations__ property of __RadFixedDocument__ – A collection which returns all annotations in the document. For example you can retrieve all links using the following code:
61-
62-
#### Get Annotation Links
63-
64-
{{source=..\SamplesCS\PdfViewer\PdfAnnotations.cs region=GetAllLinks}}
65-
{{source=..\SamplesVB\PdfViewer\PdfAnnotations.vb region=GetAllLinks}}
66-
55+
56+
````
57+
58+
{{endregion}}
59+
60+
* __HyperlinkClicked__ event of RadPdfViewer: This event is similar to AnnotationClicked, but it is raised only when you click on the hyperlink type annotations. It allows you to cancel the navigation to the associated URI or to modify the click action. The HyperlinkClickedEventArgs gives access to the URL, which can be manually checked if it is trusted. The navigation can be canceled by either setting the __Handled__ property of the event args to _true_ or the __IsTrustedUrl__ property to _false_. Below is an example of using this event to prompt that the clicked hyperlink might be unsafe and provide the opportunity to cancel the navigation process upon receiving the end user confirmation:
61+
62+
#### HyperlinkClicked Event Handler
63+
64+
{{source=..\SamplesCS\PdfViewer\PdfAnnotations.cs region=HyperlinkClicked}}
65+
{{source=..\SamplesVB\PdfViewer\PdfAnnotations.vb region=HyperlinkClicked}}
66+
67+
````C#
68+
private void RadPdfViewer1_HyperlinkClicked(object sender, Telerik.WinControls.Hyperlinks.HyperlinkClickedEventArgs e)
69+
{
70+
var link = e.URL;
71+
if (link.EndsWith("exe"))
72+
{
73+
e.Handled = true; MessageBoxResult Result = System.Windows.MessageBox.Show("You are about to open an executable file. Do you want to proceed", "Possible unsafe link", MessageBoxButton.YesNo, MessageBoxImage.Question);
74+
if (Result == MessageBoxResult.Yes)
75+
{
76+
Process.Start(new ProcessStartInfo()
77+
{
78+
FileName = link,
79+
UseShellExecute = true
80+
});
81+
}
82+
}
83+
}
84+
85+
````
86+
````VB.NET
87+
Private Sub RadPdfViewer1_HyperlinkClicked(sender As Object, e As HyperlinkClickedEventArgs)
88+
Dim link = e.URL
89+
If link.EndsWith("exe") Then
90+
e.Handled = True
91+
Dim Result As MessageBoxResult = System.Windows.MessageBox.Show("You are about to open an executable file. Do you want to proceed", "Possible unsafe link", MessageBoxButton.YesNo, MessageBoxImage.Question)
92+
If Result = MessageBoxResult.Yes Then
93+
Process.Start(New ProcessStartInfo() With {
94+
.FileName = link,
95+
.UseShellExecute = True
96+
})
97+
End If
98+
End If
99+
End Sub
100+
101+
````
102+
103+
{{endregion}}
104+
105+
106+
* __Annotations__ property of __RadFixedDocument__ – A collection which returns all annotations in the document. For example you can retrieve all links using the following code:
107+
108+
#### Get Annotation Links
109+
110+
{{source=..\SamplesCS\PdfViewer\PdfAnnotations.cs region=GetAllLinks}}
111+
{{source=..\SamplesVB\PdfViewer\PdfAnnotations.vb region=GetAllLinks}}
112+
67113
````C#
68114

69115
private IEnumerable<Telerik.Windows.Documents.Fixed.Model.Annotations.Link> GetAllLinks(Telerik.Windows.Documents.Fixed.Model.RadFixedDocument document)
@@ -77,8 +123,8 @@ private IEnumerable<Telerik.Windows.Documents.Fixed.Model.Annotations.Link> GetA
77123
}
78124
}
79125
}
80-
81-
````
126+
127+
````
82128
````VB.NET
83129
Private Iterator Function GetAllLinks(document As Telerik.Windows.Documents.Fixed.Model.RadFixedDocument) As IEnumerable(Of Telerik.Windows.Documents.Fixed.Model.Annotations.Link)
84130
For Each a As Telerik.Windows.Documents.Fixed.Model.Annotations.Annotation In document.Annotations
@@ -88,18 +134,18 @@ Private Iterator Function GetAllLinks(document As Telerik.Windows.Documents.Fixe
88134
End If
89135
Next
90136
End Function
91-
92-
````
93-
94-
{{endregion}}
95-
96-
The bookmarks in terms of “docx bookmarks” are not explicitly saved in PDF files. They are persisted only if there are Link annotations to them, so you can use the snippet below to retrieve all destinations that have links to them:
97-
98-
#### Get Annotation Bookmarks
99-
100-
{{source=..\SamplesCS\PdfViewer\PdfAnnotations.cs region=Bookmarks}}
101-
{{source=..\SamplesVB\PdfViewer\PdfAnnotations.vb region=Bookmarks}}
102-
137+
138+
````
139+
140+
{{endregion}}
141+
142+
The bookmarks in terms of “docx bookmarks” are not explicitly saved in PDF files. They are persisted only if there are Link annotations to them, so you can use the snippet below to retrieve all destinations that have links to them:
143+
144+
#### Get Annotation Bookmarks
145+
146+
{{source=..\SamplesCS\PdfViewer\PdfAnnotations.cs region=Bookmarks}}
147+
{{source=..\SamplesVB\PdfViewer\PdfAnnotations.vb region=Bookmarks}}
148+
103149
````C#
104150

105151
private IEnumerable<Telerik.Windows.Documents.Fixed.Model.Navigation.Destination> GetAllBookmarks(Telerik.Windows.Documents.Fixed.Model.RadFixedDocument document)
@@ -113,8 +159,8 @@ private IEnumerable<Telerik.Windows.Documents.Fixed.Model.Navigation.Destination
113159
}
114160
}
115161
}
116-
117-
````
162+
163+
````
118164
````VB.NET
119165
Private Iterator Function GetAllBookmarks(document As Telerik.Windows.Documents.Fixed.Model.RadFixedDocument) As IEnumerable(Of Telerik.Windows.Documents.Fixed.Model.Navigation.Destination)
120166
For Each a As Telerik.Windows.Documents.Fixed.Model.Annotations.Annotation In document.Annotations
@@ -124,38 +170,38 @@ Private Iterator Function GetAllBookmarks(document As Telerik.Windows.Documents.
124170
End If
125171
Next
126172
End Function
127-
128-
````
129-
130-
{{endregion}}
131-
132-
In this way it would be possible to create some UI that contains all bookmarks. Then, you could implement the same action as the one being executed when a hyperlink is clicked, i.e. scroll the document to the specific place in the document where the destination of the link is placed. The following code can be used for this purpose – navigating to a specific destination:
133-
134-
#### Navigate to Destination
135-
136-
{{source=..\SamplesCS\PdfViewer\PdfAnnotations.cs region=GoToDestination}}
137-
{{source=..\SamplesVB\PdfViewer\PdfAnnotations.vb region=GoToDestination}}
138-
173+
174+
````
175+
176+
{{endregion}}
177+
178+
In this way it would be possible to create some UI that contains all bookmarks. Then, you could implement the same action as the one being executed when a hyperlink is clicked, i.e. scroll the document to the specific place in the document where the destination of the link is placed. The following code can be used for this purpose – navigating to a specific destination:
179+
180+
#### Navigate to Destination
181+
182+
{{source=..\SamplesCS\PdfViewer\PdfAnnotations.cs region=GoToDestination}}
183+
{{source=..\SamplesVB\PdfViewer\PdfAnnotations.vb region=GoToDestination}}
184+
139185
````C#
140186

141187
private void GoToDestination(Telerik.Windows.Documents.Fixed.Model.Navigation.Destination destination)
142188
{
143189
this.radPdfViewer1.PdfViewerElement.GoToDestination(destination);
144190
}
145-
146-
````
191+
192+
````
147193
````VB.NET
148194
Private Sub GoToDestination(destination As Telerik.Windows.Documents.Fixed.Model.Navigation.Destination)
149195
Me.RadPdfViewer1.PdfViewerElement.GoToDestination(destination)
150196
End Sub
151-
152-
````
153-
154-
{{endregion}}
155-
156-
# See Also
157-
158-
* [Getting Started]({%slug winforms/pdfviewer/getting-started%})
159-
* [Logical Structure]({%slug winforms/pdfviewer/structure/logical-structure%})
160-
* [Visual Structure]({%slug winforms/pdfviewer/structure/visual-structure%})
197+
198+
````
199+
200+
{{endregion}}
201+
202+
# See Also
203+
204+
* [Getting Started]({%slug winforms/pdfviewer/getting-started%})
205+
* [Logical Structure]({%slug winforms/pdfviewer/structure/logical-structure%})
206+
* [Visual Structure]({%slug winforms/pdfviewer/structure/visual-structure%})
161207
* [Properties, Methods and Events]({%slug winforms/pdfviewer/properties-methods-and-events%})

0 commit comments

Comments
 (0)