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.
//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 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 action and set target as web address linkAnnot.Action = New Aspose.Pdf.InteractiveFeatures.GoToURIAction("www.aspose.com") ' Save the document with updated link doc.Save("PDFLINK_Modified_output.pdf") //Set Link Target to Another PDF File //The following code snippet shows how to update a link in a PDF file and set its target to another PDF file. //C# Code Sample Document document = new Document("c:/pdftest/This is a test Zoom Fit Page.pdf"); LinkAnnotation linkAnnot = (LinkAnnotation)document.Pages[1].Annotations[1]; GoToRemoteAction goToR = (GoToRemoteAction)linkAnnot.Action; // Next line update destination, do not update file goToR.Destination = new XYZExplicitDestination(document, 2, 0, 0, 1.5); // Next line update file goToR.File = new FileSpecification("c:/pdftest/PDFInput (1).pdf"); document.Save("c:/pdftest/ResultantFile.pdf"); //VB.NET Code Sample Dim document As Document = New Document("c:/pdftest/This is a test Zoom Fit Page.pdf") Dim linkAnnot As LinkAnnotation = TryCast(document.Pages(1).Annotations(1), LinkAnnotation) Dim goToR As InteractiveFeatures.GoToRemoteAction = TryCast(linkAnnot.Action, InteractiveFeatures.GoToRemoteAction) ' Next line update destination, do not update file goToR.Destination = New InteractiveFeatures.XYZExplicitDestination(document, 2, 0, 0, 1.5) ' Next line update file goToR.File = New FileSpecification("c:/pdftest/PDFInput (1).pdf") document.Save("c:/pdftest/ResultantFile.pdf") //Update LinkAnnotation text color //The Link annotation does not contain text. Text is placed in the contents of the page under the annotation. Therefore in order to change color of the text, we need to replace color of the page text instead of trying change color of the annotation. The following code snippet shows how to update the color of link annotation in PDF file. //C# Code Sample Document doc = new Document("AsposeCaseExample.pdf"); foreach (Annotation annotation in doc.Pages[1].Annotations) { if (annotation is LinkAnnotation) { //search text under the annotation TextFragmentAbsorber ta = new TextFragmentAbsorber(); Rectangle rect = annotation.Rect; rect.LLX -= 10; rect.LLY -= 10; rect.URX += 10; rect.URY += 10; ta.TextSearchOptions = new TextSearchOptions(rect); ta.Visit(doc.Pages[1]); //change color of the text. foreach (TextFragment tf in ta.TextFragments) { tf.TextState.ForegroundColor = Color.Red; } } } doc.Save("output.pdf"); //VB.NET Code Sample Dim doc As Document = New Document("AsposeCaseExample.pdf") For Each annotation As Aspose.Pdf.InteractiveFeatures.Annotations.Annotation In doc.Pages(1).Annotations If TypeOf annotation Is InteractiveFeatures.Annotations.LinkAnnotation Then 'search text under the annotation Dim ta As Aspose.Pdf.Text.TextFragmentAbsorber = New Aspose.Pdf.Text.TextFragmentAbsorber() Dim rect As Aspose.Pdf.Rectangle = annotation.Rect rect.LLX -= 10 rect.LLY -= 10 rect.URX += 10 rect.URY += 10 ta.TextSearchOptions = New TextSearchOptions(rect) ta.Visit(doc.Pages(1)) 'change color of the text. For Each tf As Aspose.Pdf.Text.TextFragment In ta.TextFragments tf.TextState.ForegroundColor = Color.Red Next End If Next doc.Save("output.pdf")

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