Update Links in PDF

The following code snippet also work with Aspose.PDF.Drawing library.

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:

  1. Load a PDF file.
  2. Go to a specific page in the PDF file.
  3. Specify the link destination using the GoToAction object’s Destination property.
  4. The destination page is specified using the XYZExplicitDestination constructor.

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.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();
// Load the PDF file
Document doc = new Document(dataDir + "UpdateLinks.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.Annotations.XYZExplicitDestination(1, 1, 2, 2);
dataDir = dataDir + "PDFLINK_Modified_UpdateLinks_out.pdf";
// Save the document with updated link
doc.Save(dataDir);

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.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();
// Load the PDF file
Document doc = new Document(dataDir + "UpdateLinks.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");

dataDir = dataDir + "SetDestinationLink_out.pdf";
// Save the document with updated link
doc.Save(dataDir);

The following code snippet shows how to update a link in a PDF file and set its target to another PDF file.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();
// Load the PDF file
Document document = new Document(dataDir + "UpdateLinks.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(2, 0, 0, 1.5);
// Next line update file
goToR.File = new FileSpecification(dataDir +  "input.pdf");

dataDir = dataDir + "SetTargetLink_out.pdf";
// Save the document with updated link
document.Save(dataDir);

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.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();
// Load the PDF file
Document doc = new Document(dataDir + "UpdateLinks.pdf");
foreach (Annotation annotation in doc.Pages[1].Annotations)
{
    if (annotation is LinkAnnotation)
    {
        // Search the 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;
        }
    }

}
dataDir = dataDir + "UpdateLinkTextColor_out.pdf";
// Save the document with updated link
doc.Save(dataDir);