# JavaScript Actions

| Minimum Version | Q4 2024 |
| --------------- | ------- |

RadPdfProcessing supports:

* **JavaScript actions** associated with documents, pages, form fields, and other elements.
* **Event-triggered actions** that execute after a certain event in the respective viewer (for example, RadPdfViewer, Adobe Acrobat, or a web browser) is triggered.

The `JavaScriptAction` class represents JavaScript actions and stores the JS content as plain text in its public `Script` property.

![PdfProcessing JS Actions Overview diagram](https://www.telerik.com/document-processing-libraries/documentation/assets/ad57fbe926d72959ef1d0410bb52e790/js-action-overview.png)

You can add JS actions by using the public `Actions` property of the following classes:

| Class              | Collection Type                                                                                                                                                                                   |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Link`\*           | [ActionCollection](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/actions/js-actions/action-collections#actioncollection)                   |
| `BookmarkItem`\*   | [ActionCollection](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/actions/js-actions/action-collections#actioncollection)                   |
| `Widget`           | [WidgetActionCollection](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/actions/js-actions/action-collections#widgetactioncollection)       |
| `FormField`        | [FormFieldActionCollection](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/actions/js-actions/action-collections#formfieldactioncollection) |
| `RadFixedDocument` | [DocumentActionCollection](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/actions/js-actions/action-collections#documentactioncollection)   |
| `RadFixedPage`     | [PageActionCollection](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/actions/js-actions/action-collections#pageactioncollection)           |

\* The existing `Action` property is obsolete.

## Adding a JavaScript Action to a TextBoxField

The following example demonstrates how to create a PDF document with three `TextBoxField` instances where the third field calculates the sum of the values entered in the first two widgets:

![JS Action Sum FormField result showing automatic calculation](https://www.telerik.com/document-processing-libraries/documentation/assets/d4c74c2df0672561cc11a38bc923d41d/js-action-sum-form-field.gif)

```C#
RadFixedDocument document = new RadFixedDocument();
document.Pages.AddPage();

TextBoxField field1 = new TextBoxField("Field1");
VariableContentWidget widget1 = field1.Widgets.AddWidget();
widget1.Rect = new Rect(0, 0, 150, 30);

TextBoxField field2 = new TextBoxField("Field2");
VariableContentWidget widget2 = field2.Widgets.AddWidget();
widget2.Rect = new Rect(0, 50, 150, 30);

TextBoxField totalField = new TextBoxField("Total");
totalField.IsReadOnly = true;
totalField.Actions.Calculate = new Telerik.Documents.Fixed.Model.Actions.JavaScriptAction
    ("AFSimple_Calculate(\"SUM\", new Array (\"Field1\", \"Field2\"));");
VariableContentWidget totalWidget = totalField.Widgets.AddWidget();
totalWidget.Rect = new Rect(0, 100, 150, 30);

document.AcroForm.FormFields.Add(field1);
document.AcroForm.FormFields.Add(field2);
document.AcroForm.FormFields.Add(totalField);
document.Pages[0].Annotations.Add(widget1);
document.Pages[0].Annotations.Add(widget2);
document.Pages[0].Annotations.Add(totalWidget);
```

## Using the MergedJavaScriptNameResolving Event

The `MergedJavaScriptNameResolving` event fires when resolving conflicts between JavaScript names while merging `RadFixedDocument` instances.

```C#
document.MergedJavaScriptNameResolving += (sender, e) =>
{
    if (e.UsedNames.Contains(e.Name))
    {
        e.NewName = e.Name + "1";
    }
};
```

## See Also

* [FormField](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/form-fields/formfields)
* [FormFieldCollection](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/formfieldcollection)
* [Widgets](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/annotations/widgets)
* [Multiplying TextBoxField Values with JavaScript Actions and RadPdfProcessing](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/multiply-form-field-with-javascript-action-radpdfprocessing)
