Add JavaScript Link

Contents
[ ]

JavaScript links in PDFs allow interactive functionality such as displaying alerts, performing calculations, or dynamically modifying document content. Using PdfContentEditor, you can define a clickable rectangle on a page and associate it with custom JavaScript code.

  1. Create a PdfContentEditor instance.
  2. Bind the input PDF document.
  3. Define a rectangle for the clickable JavaScript link.
  4. Specifie the page number and link color.
  5. Assign JavaScript code to execute when clicked.
  6. Save the updated PDF document.
import aspose.pdf.facades as pdf_facades
from aspose.pycore import cast, is_assignable
import aspose.pydrawing as apd
import aspose.pdf as ap

import sys
from os import path

sys.path.append(path.join(path.dirname(__file__), ".."))

from config import set_license, initialize_data_dir


def add_javascript_link(infile, outfile):
    # Create PdfContentEditor object
    content_editor = pdf_facades.PdfContentEditor()
    # Bind document to PdfContentEditor
    content_editor.bind_pdf(infile)
    # Add JavaScript link action
    content_editor.create_java_script_link(
        "app.alert('PdfContentEditor JavaScript link');",
        apd.Rectangle(160, 560, 260, 20),
        1,
        apd.Color.orange,
    )
    # Save updated document
    content_editor.save(outfile)