Update Links in PDF
The following code snippet also work with Aspose.PDF.Drawing library.
Update Links in PDF File
As discussed in Add Hyperlink in a PDF File, the LinkAnnotation class makes it possible to add links in a PDF file. There’s also a similar class used to get existing links from inside PDF files. Use this if you need to update an existing link. To update an existing link:
Load a PDF file.
Go to a specific page in the PDF file.
Specify the link destination using the GoToAction object’s Destination property.
The destination page is specified using the XYZExplicitDestination constructor.
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.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void UpdateLinkAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Load the PDF file
using ( var document = new Aspose . Pdf . Document ( dataDir + "UpdateLinks.pdf" ))
{
// Get the first link annotation from first page of document
var linkAnnot = ( Aspose . Pdf . Annotations . LinkAnnotation ) document . Pages [ 1 ]. Annotations [ 1 ];
// Modification link: change link destination
var goToAction = ( Aspose . Pdf . Annotations . 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 . Annotations . XYZExplicitDestination ( 1 , 1 , 2 , 2 );
// Save the document with updated link
document . Save ( dataDir + "PDFLINK_Modified_UpdateLinks_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void UpdateLinkAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Load the PDF file
using var document = new Aspose . Pdf . Document ( dataDir + "UpdateLinks.pdf" );
// Get the first link annotation from first page of document
var linkAnnot = ( Aspose . Pdf . Annotations . LinkAnnotation ) document . Pages [ 1 ]. Annotations [ 1 ];
// Modification link: change link destination
var goToAction = ( Aspose . Pdf . Annotations . 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 . Annotations . XYZExplicitDestination ( 1 , 1 , 2 , 2 );
// Save the document with updated link
document . Save ( dataDir + "PDFLINK_Modified_UpdateLinks_out.pdf" );
}
Set Link Destination to a Web Address
To update the hyperlink so that it points to a 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.
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.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void UpdateLinkAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Load the PDF file
using ( var document = new Aspose . Pdf . Document ( dataDir + "UpdateLinks.pdf" ))
{
var linkAnnot = ( Aspose . Pdf . Annotations . LinkAnnotation ) document . Pages [ 1 ]. Annotations [ 1 ];
var goToR = ( Aspose . Pdf . Annotations . GoToRemoteAction ) linkAnnot . Action ;
// Next line update destination, do not update file
goToR . Destination = new Aspose . Pdf . Annotations . XYZExplicitDestination ( 2 , 0 , 0 , 1.5 );
// Next line update file
goToR . File = new Aspose . Pdf . FileSpecification ( dataDir + "input.pdf" );
// Save the document with updated link
document . Save ( dataDir + "SetTargetLink_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void UpdateLinkAnnotation ()
{
// The path to the documents directory
string dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Load the PDF file
using var document = new Aspose . Pdf . Document ( dataDir + "UpdateLinks.pdf" );
var linkAnnot = ( Aspose . Pdf . Annotations . LinkAnnotation ) document . Pages [ 1 ]. Annotations [ 1 ];
var goToR = ( Aspose . Pdf . Annotations . GoToRemoteAction ) linkAnnot . Action ;
// Next line update destination, do not update file
goToR . Destination = new Aspose . Pdf . Annotations . XYZExplicitDestination ( 2 , 0 , 0 , 1.5 );
// Next line update file
goToR . File = new Aspose . Pdf . FileSpecification ( dataDir + "input.pdf" );
// Save the document with updated link
document . Save ( dataDir + "SetTargetLink_out.pdf" );
}
Update LinkAnnotation Text Color
The link annotation does not contain text. Instead, the text is placed in the contents of the page under the annotation. Therefore, to change the color of the text, replace the 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 a PDF file.