DynaPDF.AddActionToObj
----------------------

Adds an action to a PDF object.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [DynaPDF](component_DynaPDF.md) | [6.4](newinversion64.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "DynaPDF.AddActionToObj"; PDF; ObjectType; ObjectEvent; Action Handle; Object Handle ) 

**MBS**( **"DynaPDF.AddActionToObj";** /\* Adds an action to a PDF object. \*/  
**$PDF**; /\* The PDF reference returned from [DynaPDF.New](DynaPDFNew.md). \*/   
**$ObjectType**; /\* The type of object that should get the action.  
Can be Action, Annotation, Bookmark, Catalog, Field, Page or PageLink.e.g. "Field" \*/   
**$ObjectEvent**; /\* Which event should trigger the action.  
Can be OnOpen, OnClose, OnMouseUp, OnMouseEnter, OnMouseExit, OnMouseDown, OnFocus, OnBlur, OnKeyStroke, OnFormat, OnCalc, OnValidate, OnPageVisible, OnPageInVisible, OnPageOpen, OnPageClose, OnBeforeClosign, OnBeforeSaving, OnAfterSaving, OnBeforePrinting or OnAfterPrinting.e.g. "OnCalc" \*/   
**$Action Handle**; /\* The handle for the action.  
e.g. returned by [DynaPDF.CreateJSAction](DynaPDFCreateJSAction.md).e.g. $action \*/   
**$Object Handle**) /\* The object handle, e.g. a field number for a ObjectType = Field.e.g. $o \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| PDF | The PDF reference returned from [DynaPDF.New](DynaPDFNew.md). | $pdf |
| ObjectType | The type of object that should get the action.   Can be Action, Annotation, Bookmark, Catalog, Field, Page or PageLink. | "Field" |
| ObjectEvent | Which event should trigger the action.   Can be OnOpen, OnClose, OnMouseUp, OnMouseEnter, OnMouseExit, OnMouseDown, OnFocus, OnBlur, OnKeyStroke, OnFormat, OnCalc, OnValidate, OnPageVisible, OnPageInVisible, OnPageOpen, OnPageClose, OnBeforeClosign, OnBeforeSaving, OnAfterSaving, OnBeforePrinting or OnAfterPrinting. | "OnCalc" |
| Action Handle | The handle for the action.   e.g. returned by [DynaPDF.CreateJSAction](DynaPDFCreateJSAction.md). | $action |
| Object Handle | The object handle, e.g. a field number for a ObjectType = Field. | $o |

### Result

Returns OK or error.

### Description

Adds an action to a PDF object.   
The parameter ActHandle requires a handle that was returned by a function that creates an action object such as [DynaPDF.CreateGoToAction](DynaPDFCreateGoToAction.md), etc. The object handle is also a return value of its creation function. If the object type is a page, then use the page number as handle.   
It is possible to add multiple actions to one object, but note that DynaPDF does not check whether all actions are valid. For example, it is possible to add more than one action to a text field to the OnFormat event, but Acrobat executes only the first one. Always test your actions with different Acrobat versions because each version handles actions in a different manner.   
The OnMouseUp event is the standard event supported by all objects. This event type supports also all action types. The other events are handled differently depending on the Acrobat version. Acrobat 4 supports JavaScript actions only in non-OnMouseUp events. Newer versions support also predefined action types. However, test your forms with the Acrobat versions that should be supported - this is the only way to find incompatibilities.   
See also [AddActionToObj](https://www.monkeybreadsoftware.com/DynaPDF-Manual/AddActionToObj.shtml) function in DynaPDF manual.

### Examples

Create sum field:

 Set Variable \[$y ; Value:50\]  
Set Variable \[$script ; Value:"var v1 = this.getField(\\"Val1\\");" &amp; ¶ &amp; "var v2 = this.getField(\\"Val2\\");" &amp; ¶ &amp; "var v3 = this.getField(\\"Val3\\");" &amp; ¶ &amp; "event.value = v1.value + v2.value + v3.value;"\]  
Set Variable \[$script ; Value:MBS ( "[Text.ReplaceNewLine](TextReplaceNewLine.md)"; $script ; 2)\]  
Set Variable \[$a ; Value:MBS ( "[DynaPDF.CreateJSAction](DynaPDFCreateJSAction.md)"; $pdf ; $script )\]  
\#Create 1st field   
Set Variable \[$f ; Value:MBS ( "[DynaPDF.CreateTextField](DynaPDFCreateTextField.md)"; $pdf ; "Val1"; -1; 0; 0; 50; $y ; 200; 20)\]  
Set Variable \[$r ; Value:MBS ( "[DynaPDF.SetTextFieldValue](DynaPDFSetTextFieldValue.md)"; $pdf ; $f ; "50.00 "; "50.00 "; "Right")\]  
Set Variable \[$r ; Value:MBS ( "[DynaPDF.SetNumberFormat](DynaPDFSetNumberFormat.md)"; $pdf ; $f ; "NoneDot"; 2; "MinusBlack")\]  
\#Create 2nd field   
Set Variable \[$y ; Value:$y + 30\]  
Set Variable \[$f ; Value:MBS ( "[DynaPDF.CreateTextField](DynaPDFCreateTextField.md)"; $pdf ; "Val2"; -1; 0; 0; 50; $y ; 200; 20)\]  
Set Variable \[$r ; Value:MBS ( "[DynaPDF.SetTextFieldValue](DynaPDFSetTextFieldValue.md)"; $pdf ; $f ; "100.00 "; "100.00 "; "Right")\]  
Set Variable \[$r ; Value:MBS ( "[DynaPDF.SetNumberFormat](DynaPDFSetNumberFormat.md)"; $pdf ; $f ; "NoneDot"; 2; "MinusBlack")\]  
\#Create 3rd field   
Set Variable \[$y ; Value:$y + 30\]  
Set Variable \[$f ; Value:MBS ( "[DynaPDF.CreateTextField](DynaPDFCreateTextField.md)"; $pdf ; "Val3"; -1; 0; 0; 50; $y ; 200; 20)\]  
Set Variable \[$r ; Value:MBS ( "[DynaPDF.SetTextFieldValue](DynaPDFSetTextFieldValue.md)"; $pdf ; $f ; "200.00 "; "200.00 "; "Right")\]  
Set Variable \[$r ; Value:MBS ( "[DynaPDF.SetNumberFormat](DynaPDFSetNumberFormat.md)"; $pdf ; $f ; "NoneDot"; 2; "MinusBlack")\]  
\#Create sum field   
\#This last field calculates sum of other fields   
\#Works only in PDF Viewers supporting JavaScript!   
Set Variable \[$y ; Value:$y + 30\]  
Set Variable \[$f ; Value:MBS ( "[DynaPDF.CreateTextField](DynaPDFCreateTextField.md)"; $pdf ; "Sum"; -1; 0; 10; 50; $y ; 200; 20)\]  
Set Variable \[$r ; Value:MBS ( "[DynaPDF.SetFieldBorderWidth](DynaPDFSetFieldBorderWidth.md)"; $pdf ; $f ; 0)\]  
Set Variable \[$r ; Value:MBS ( "[DynaPDF.SetTextFieldValue](DynaPDFSetTextFieldValue.md)"; $pdf ; $f ; "350.00 €"; "350.00 €"; "Right")\]  
Set Variable \[$r ; Value:MBS ( "[DynaPDF.SetFieldFlags](DynaPDFSetFieldFlags.md)"; $pdf ; $f ; "ReadOnly"; 0)\]  
Set Variable \[$r ; Value:MBS ( "DynaPDF.AddActionToObj"; $pdf ; "Field"; "OnCalc"; $a ; $f )\]  
Set Variable \[$r ; Value:MBS ( "[DynaPDF.SetNumberFormat](DynaPDFSetNumberFormat.md)"; $pdf ; $f ; "CommaDot"; 2; "MinusBlack"; " €"; 0)\]  
Create checkbox to confirm page and jump to next one:

 Set Variable \[ $f ; Value: MBS ( "[DynaPDF.CreateCheckBox](DynaPDFCreateCheckBox.md)"; $pdf ; "OK"; "OK";0; -1; $PageWidth -150; $pageHeight -20; 15; 15 ) \]   
\# This is javascript from Adobe to go to next page You could substitute many of their examples   
Set Variable \[ $script ; Value: "this.pageNum++" \]   
Set Variable \[ $script ; Value: MBS ( "[Text.ReplaceNewLine](TextReplaceNewLine.md)"; $script ; 2 ) \]   
Set Variable \[ $a ; Value: MBS ( "[DynaPDF.CreateJSAction](DynaPDFCreateJSAction.md)"; $pdf ; $script ) \]   
Set Variable \[ $r ; Value: MBS ( "DynaPDF.AddActionToObj"; $pdf ; "Field"; "OnMouseUp"; $a ; $f ) \]  
Add JavaScript action for a link:

 Set Variable \[ $r ; Value: MBS ( "[DynaPDF.SetFont](DynaPDFSetFont.md)"; $pdf ; "Helvetica"; "none"; 12) \]   
Set Variable \[ $r ; Value: MBS ( "[DynaPDF.SetTextRect](DynaPDFSetTextRect.md)"; $pdf ; 310; 30; 200; 20) \]   
Set Variable \[ $r ; Value: MBS ( "[DynaPDF.SetStrokeColor](DynaPDFSetStrokeColor.md)"; $pdf ; 0; 0; 0) \]   
Set Variable \[ $r ; Value: MBS ( "[DynaPDF.WriteFText](DynaPDFWriteFText.md)"; $pdf ; "center"; "Open MBS Website in new Tab") \]   
\# the page link points to the page to go for if no JavaScript is enabled   
// Set Variable \[ $LinkRef ; Value: MBS ( "[DynaPDF.PageLink](DynaPDFPageLink.md)"; $pdf ; 310; 10; 200; 20; 2) \]   
\# the web link points to the URL to use when no JavaScript is enabled   
Set Variable \[ $LinkRef ; Value: MBS ( "[DynaPDF.WebLink](DynaPDFWebLink.md)"; $pdf ; 310; 10; 200; 20; "https://www.monkeybreadsoftware.com/filemaker/") \]   
\# we add a JavaScript action to open URL   
Set Variable \[ $JSAction ; Value: MBS ( "[DynaPDF.CreateJSAction](DynaPDFCreateJSAction.md)"; $PDF ; "app.launchURL(\\"https://www.monkeybreadsoftware.com/filemaker/\\", true);" ) \]   
Set Variable \[ $r ; Value: MBS ( "DynaPDF.AddActionToObj"; $pdf ; "PageLink"; "OnMouseUp"; $JSAction ; $LinkRef ; ) \]   
Set Variable \[ $r ; Value: MBS ( "[DynaPDF.EndPage](DynaPDFEndPage.md)"; $pdf ) \]  
### See also

- [DynaPDF.EndPage](DynaPDFEndPage.md)
- [DynaPDF.InsertBookmarkEx](DynaPDFInsertBookmarkEx.md)
- [DynaPDF.New](DynaPDFNew.md)
- [DynaPDF.PageLink](DynaPDFPageLink.md)
- [DynaPDF.SetFieldBorderWidth](DynaPDFSetFieldBorderWidth.md)
- [DynaPDF.SetNumberFormat](DynaPDFSetNumberFormat.md)
- [DynaPDF.SetTextFieldValue](DynaPDFSetTextFieldValue.md)
- [DynaPDF.SetTextRect](DynaPDFSetTextRect.md)
- [DynaPDF.WebLink](DynaPDFWebLink.md)
- [DynaPDF.WriteFText](DynaPDFWriteFText.md)

### Example Databases

- [DynaPDF/Add Weblinks](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/DynaPDF/Add%20Weblinks.shtml#3ScriptAnchor_)
- [DynaPDF/Create PDF with sum form field](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/DynaPDF/Create%20PDF%20with%20sum%20form%20field.shtml#2ScriptAnchor_)
- [DynaPDF/Create Text as PDFA](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/DynaPDF/Create%20Text%20as%20PDFA.shtml#3ScriptAnchor_)
- [DynaPDF/Form/Form](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/DynaPDF/Form/Form.shtml#6ScriptAnchor_)
- [DynaPDF/PDF Attachments](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/DynaPDF/PDF%20Attachments.shtml#6ScriptAnchor_)

### Blog Entries

- [PDF Attachments in FileMaker with DynaPDF](https://www.mbsplugins.de/archive/2024-01-29/PDF_Attachments_in_FileMaker_w/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 6.4pr5](https://www.mbsplugins.de/archive/2016-08-29/MBS_FileMaker_Plugin_version_6/monkeybreadsoftware_blog_filemaker)
- [Tip of the day: DynaPDF form creation with calculation with FileMaker](https://www.mbsplugins.de/archive/2016-08-24/Tip_of_the_day_DynaPDF_form_cr/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 24th August 2016 , last changed 21st April 2023

  
[DynaPDF.ActivateAltFontList](DynaPDFActivateAltFontList.md) - [DynaPDF.AddAnnotToPage](DynaPDFAddAnnotToPage.md)

[HTML Version](DynaPDFAddActionToObj.shtml)