Update PDF Links in Python

Update LinkAnnotation Text Color

This example shows how to detect all link annotations on the first page of a PDF using Aspose.PDF for Python, then highlight the text near each link by changing its font color to red. It uses TextFragmentAbsorber with a slightly expanded area around each link rectangle to find and modify nearby text.

This can be used for:

  • Visually marking links in a document
  • Enhancing accessibility by emphasizing linked content
  • Preprocessing PDF files before review or export

For each of these link annotations, the script retrieves its rectangular boundary and slightly expands that region to include nearby text, allowing for broader visual identification. It then applies a TextFragmentAbsorber over this expanded area to extract any text fragments located within it. These captured fragments are modified by changing their font color to red, effectively marking the surrounding text for emphasis and review. After applying all these updates, the modified document is saved to the output path, preserving the highlighted annotations and their associated text.

import aspose.pdf as ap
import sys
from os import path
from aspose.pycore import cast, is_assignable

def link_annotation_update_text_color(infile, outfile):
    document = ap.Document(infile)
    link_annotations = [
        a
        for a in document.pages[1].annotations
        if (a.annotation_type == ap.annotations.AnnotationType.LINK)
    ]

    for la in link_annotations:
        ta = ap.text.TextFragmentAbsorber()
        rect = la.rect
        rect.llx -= 2
        rect.lly -= 2
        rect.urx += 2
        rect.ury += 2
        ta.text_search_options = ap.text.TextSearchOptions(rect)
        ta.visit(document.pages[1])
        for textFragment in ta.text_fragments:
            textFragment.text_state.foreground_color = ap.Color.red

    document.save(outfile)

Update Border

The script focuses on the first page of the document and filters out all annotations, selecting only those of the LINK type—these typically represent interactive elements, such as hyperlinks or navigation triggers. For each of these link annotations, the code casts them to the LinkAnnotation type and updates their color property to red, visually highlighting them to stand out from other content. After all link annotations have been modified, the updated document is saved to the defined output location, preserving the new styling.

import aspose.pdf as ap
import sys
from os import path
from aspose.pycore import cast, is_assignable

def link_annotation_update_border(infile, outfile):
    document = ap.Document(infile)
    link_annotations = [
        a
        for a in document.pages[1].annotations
        if (a.annotation_type == ap.annotations.AnnotationType.LINK)
    ]

    for la in link_annotations:
        link_annotation = cast(ap.annotations.LinkAnnotation, la)
        link_annotation.color = ap.Color.red

    document.save(outfile)

Update Web Destination

Once the paths are in place, the original document is loaded using the Aspose.PDF library, making its content accessible for modification. The script then focuses on the first page of the document, filtering out all annotations and selecting only those that are link-type, typically representing clickable areas or hyperlinks. To avoid type errors and ensure safe handling, each annotation is checked with is_assignable and then cast to the appropriate LinkAnnotation type. If the link is associated with a GoToURIAction, meaning it points to a web address, the script updates that URI to redirect users to “https://www.aspose.com”. Finally, after all desired changes have been applied, the modified document is saved to the specified output path.

import aspose.pdf as ap
import sys
from os import path
from aspose.pycore import cast, is_assignable

def link_annotation_update_web_destination(infile, outfile):
    document = ap.Document(infile)
    link_annotations = [
        a
        for a in document.pages[1].annotations
        if (a.annotation_type == ap.annotations.AnnotationType.LINK)
    ]

    for la in link_annotations:
        if is_assignable(la, ap.annotations.LinkAnnotation):
            annotation = cast(ap.annotations.LinkAnnotation, la)
            if is_assignable(annotation.action, ap.annotations.GoToURIAction):
                action = cast(ap.annotations.GoToURIAction, annotation.action)
                action.uri = "https://www.aspose.com"
    document.save(outfile)