Update PDF Links: Set Link to a Page, Web Address or PDF File using .NET
This technical tip shows how .NET developers can update the existing link to go to a page in the loaded PDF file using Aspose.Pdf for .NET. The LinkAnnotation class makes it possible to add links inside a PDF file. There's also a similar class used to get existing links from inside PDF files. In case you need to update the existing link to go to a page in the loaded PDF file, specify the link destination the GoToAction object's Destination property. The destination page is specified using the XYZExplicitDestination constructor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//your code here...Set Link Target to a Page in the Same Document
//The following code snippet shows you how to update a link in a PDF file and set its target to the second page of the document.
//C# Code Sample
// Load the PDF file
Document doc = new Document("Source.pdf");
// Get the first link annotation from first page of document
LinkAnnotation linkAnnot = (LinkAnnotation)doc.Pages[1].Annotations[1];
// Modification link: change link destination
GoToAction goToAction = (GoToAction)linkAnnot.Action;
// Specify the destination for link object
// The first parameter is document object, second is destination page number.
// The 5ht argument is zoom factor when displaying the respective page. When using 2, the page will be displayed in 200% zoom
goToAction.Destination = new Aspose.Pdf.InteractiveFeatures.XYZExplicitDestination(doc,1, 1, 2, 2);
// Save the document with updated link
doc.Save("PDFLINK_Modified_output.pdf");
//VB.NET Code Sample
' Load the PDF file
Dim doc As Document = New Document("source.pdf")
' Get the first link annotation from first page of document
Dim linkAnnot As LinkAnnotation = CType(doc.Pages(1).Annotations(1), LinkAnnotation)
' Modification link: change link destination
Dim goToAction As Aspose.Pdf.InteractiveFeatures.GoToAction = CType(linkAnnot.Action, Aspose.Pdf.InteractiveFeatures.GoToAction)
' Specify the destination for link object
' The first parameter is document object, second is destination page number.
' The 5ht argument is zoom factor when displaying the respective page. When using 2, the page will be displayed in 200% zoom
goToAction.Destination = New Aspose.Pdf.InteractiveFeatures.XYZExplicitDestination(doc, 1, 1, 2, 2)
' Save the document with updated link
doc.Save("PDFLINK_Modified_output.pdf")
//Set Link Destination to a Web Address
//To update the hyperlink so that it points the web address, instantiate the GoToURIAction object and pass it to the LinkAnnotation's Action property
. The following code snippet shows how to update a link in a PDF file and set its target to a web address.
//C# Code Sample
// Load the PDF file
Document doc = new Document("Source.pdf");
// Get the first link annotation from first page of document
LinkAnnotation linkAnnot = (LinkAnnotation)doc.Pages[1].Annotations[1];
// Modification link: change link action and set target as web address
linkAnnot.Action = new GoToURIAction("www.aspose.com");
// Save the document with updated link
X
Url: http://www.aspose.com/.net/pdf-component.aspx
Language: C# | User: Sheraz Khan | Created: Nov 7, 2014 | Tags: Update link in PDF file Set Link Target to a Page Destination Web Address Another File LinkAnnotation text color update existing go page add links inside