Add Field Script

Add JavaScript Actions to PDF Form Fields Using Python

This code snippet enables you to add JavaScript actions to an existing PDF form field using the Aspose.PDF for Python library. It opens a PDF document, assigns a JavaScript action to a form field, and adds a script that runs when the field is triggered. Finally, the updated PDF is saved as a new file. Using the FormEditor class from the aspose.pdf.facades module, you can programmatically attach JavaScript to existing form fields:

  1. Open an existing PDF form.
  2. Set a JavaScript action for a specific field.
  3. Append another JavaScript action to the same field.
  4. Save the modified PDF document.
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
import sys
from os import path

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

from config import set_license, initialize_data_dir


def add_field_script(input_file_name, output_file_name):
    # Create FormEditor object
    form_editor = pdf_facades.FormEditor()

    # Open input PDF file
    form_editor.bind_pdf(input_file_name)

    # Set JavaScript action for the field
    form_editor.set_field_script(
        "Script_Demo_Button", "app.alert('Script 1 has been executed');"
    )

    # Add JavaScript action to the field
    form_editor.add_field_script(
        "Script_Demo_Button", "app.alert('Script 2 has been executed');"
    )

    # Save output PDF file
    form_editor.save(output_file_name)