Add Document Action

Contents
[ ]

Document-level actions allow you to define behaviors that execute automatically when certain events occur, such as opening a PDF. With PdfContentEditor, you can attach JavaScript code to these events. This can be used for notifications, validation logic, or interactive workflows.

  1. Create the PdfContentEditor object.
  2. Bind the input PDF.
  3. Add Document-Level action.
  4. Save the updated Document.
import aspose.pdf.facades as pdf_facades
import aspose.pydrawing as apd
import sys
from os import path

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

from config import set_license, initialize_data_dir


def add_document_action(infile, outfile):
    # Create PdfContentEditor object
    content_editor = pdf_facades.PdfContentEditor()
    # Bind document to PdfContentEditor
    content_editor.bind_pdf(infile)
    # Add JavaScript action for document open event
    content_editor.add_document_additional_action(
        content_editor.DOCUMENT_OPEN,
        "app.alert('Document opened with PdfContentEditor action');",
    )
    # Save updated document
    content_editor.save(outfile)