Add JavaScript Link
Contents
[
Hide
]
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.
- Create a PdfContentEditor instance.
- Bind the input PDF document.
- Define a rectangle for the clickable JavaScript link.
- Specifie the page number and link color.
- Assign JavaScript code to execute when clicked.
- 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)