Set Field Script
Interactive PDF forms often rely on JavaScript to perform tasks such as displaying alerts, validating input, or triggering dynamic form behavior. With Aspose.PDF for Python, developers can programmatically manage these scripts.
The example first adds a JavaScript action to the field and then replaces it with another script using the ‘set_field_script’ method. This approach allows developers to control or update the interactive behavior of PDF form elements such as buttons or input fields.
The form field used in this example is named ‘Script_Demo_Button’, which typically represents a button that executes the assigned script when triggered.
Using the FormEditor class from the aspose.pdf.facades module, developers can programmatically manage JavaScript actions associated with form fields:
- Open an existing PDF form document.
- Add a JavaScript action to a form field.
- Set (replace) the JavaScript action with a new script.
- 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 set_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)
# Add JavaScript action to the field
form_editor.add_field_script(
"Script_Demo_Button", "app.alert('Script 1 has been executed');"
)
# Set JavaScript action for the field
form_editor.set_field_script(
"Script_Demo_Button", "app.alert('Script 2 has been executed');"
)
# Save output PDF file
form_editor.save(output_file_name)