Create PDF Links in Python

According to the PDF 1.7 specification (ISO 32000-1:2008), a Link annotation can trigger several types of actions that define what happens when the annotation is activated. Here are the primary actions supported:

  1. GoTo – Navigates to a destination within the same document.
  2. GoToR – Jumps to a destination in another PDF file (remote go-to).
  3. URI – Opens a uniform resource identifier, typically a web link.
  4. Launch – Launches an application or opens a file (platform-dependent and often restricted for security).
  5. Named – Executes a predefined action, such as going to the next page or printing the document.
  6. JavaScript – Executes embedded JavaScript code (used with caution due to security concerns).
  7. SubmitForm – Submits form data to a specified URL.
  8. ResetForm – Resets form fields to their default values.
  9. ImportData – Imports data into the document from an external file.

By adding a link to an application into a document, it is possible to link to applications from a document. This is useful when you want readers to take a certain action at a specific point in a tutorial, for example, or to create a feature-rich document.

To create an application link with ‘LaunchAction’:

import aspose.pdf as ap
from os import path
import sys

def create_link_annotation_launch_action(infile, outfile):
    document = ap.Document(infile)
    page = document.pages[1]

    link = ap.annotations.LinkAnnotation(page, ap.Rectangle(10, 580, 120, 600, True))
    border = ap.annotations.Border(link)
    border.width = 5
    border.dash = ap.annotations.Dash(1, 1)
    link.color = ap.Color.green
    link.action = ap.annotations.LaunchAction(document, "sample.pdf")
    page.annotations.append(link)
    document.save(outfile)

Using GoToRemoteAction

This code snippet demonstrates how to add a link annotation to a specific page of a PDF document using a Python PDF library.

  1. Open the PDF document
  2. Select the second page of the document (index 1)
  3. Create a link annotation:
  4. Positioned at coordinates (10, 580, 120, 600)
  5. Colored green
  6. Links to ‘sample.pdf’ on its first page
  7. Add the link annotation to the page
  8. Save the modified document to the output file path

To create a PDF document link using ‘GoToRemoteAction’:

import aspose.pdf as ap
from os import path
import sys

def create_link_annotation_go_to_remote_action(infile, outfile):
    document = ap.Document(infile)
    page = document.pages[1]

    link = ap.annotations.LinkAnnotation(page, ap.Rectangle(10, 580, 120, 600, True))
    link.color = ap.Color.green
    link.action = ap.annotations.GoToRemoteAction("sample.pdf", 1)
    page.annotations.append(link)
    document.save(outfile)

Using GoToAction

This code demonstrates how to add a link annotation to a specific page of a PDF document using Aspose.PDF for Python. The link appears as a green, dashed-bordered rectangle and allows the user to navigate to another page within the same PDF. To create a PDF document link using ‘GoToAction’:

import aspose.pdf as ap
from os import path
import sys

def create_link_annotation_go_to_action(infile, outfile):
    document = ap.Document(infile)
    page = document.pages[1]

    link = ap.annotations.LinkAnnotation(page, ap.Rectangle(10, 580, 120, 600, True))
    border = ap.annotations.Border(link)
    border.width = 5
    border.dash = ap.annotations.Dash(1, 1)
    link.color = ap.Color.green
    if document.pages.length >= 4:
        link.action = ap.annotations.GoToAction(document.pages[4])
    else:
        link.action = ap.annotations.GoToAction(document.pages[document.pages.length])
    page.annotations.append(link)
    document.save(outfile)

Appling GoToURIAction

Next example demonstrates how to add a link annotation to a PDF document using Aspose.PDF for Python. The link appears as a green clickable area on the first page, and when clicked, it opens the Aspose.PDF for Python documentation in a web browser via a GoToURIAction.

This functionality is useful for embedding helpful external references, documentation, or support links directly within your PDFs.

  1. Load the PDF Document. Open the existing PDF file using ap.Document.
  2. Access the First Page. Use document.pages[1] to access the first page (Aspose uses 1-based indexing).
  3. Create a Link Annotation. Create a LinkAnnotation object and specify the clickable rectangular area using ap.Rectangle.
  4. Set Annotation Appearance. Set the annotation’s color to green using link.color = ap.Color.green.
  5. Assign a URI Action. Use GoToURIAction to link the annotation to an external URL.
  6. Add the Annotation to the Page. Append the configured link annotation to the first page’s annotations collection.
  7. Save the Modified Document. Save the updated PDF document to the specified output path.
import aspose.pdf as ap
from os import path
import sys
    
def create_link_annotation_go_to_URI_action(infile, outfile):
    document = ap.Document(infile)
    page = document.pages[1]

    link = ap.annotations.LinkAnnotation(page, ap.Rectangle(10, 580, 120, 600, True))
    link.color = ap.Color.green
    link.action = ap.annotations.GoToURIAction("https://docs.aspose.com/pdf/python")
    page.annotations.append(link)
    document.save(outfile)