Forum How do I...?

Is there any way to specify Javascript for form field events?

David J Prokopetz
I can see that Prince has a mechanism for passing arbitrary Javascript to document-level events, such as opening, saving, or printing a document (i.e., via the -prince-pdf-script and -prince-pdf-event-scripts CSS properties), but unless I'm overlooking something, the documentation doesn't seem to cover any way to attach arbitrary Javascript to a generated form field's events.

I have a document I'm generating using Prince which contains nearly fifty fields that need to have custom Javascript attached to their MouseUp events, and while it's not terribly challenging to load the resulting PDF up in Adobe Acrobat and manually paste in the required code for each field, it's quite time-consuming to do it every single time the document is revised; it'd be really handy to be able to pass the scripts in at the time that the PDF is generated, so consider this a feature request if it's not currently possible.
wangp
There isn't a mechanism for this yet.

However, it might work for you to set the actions programmatically when the PDF is opened, by passing --pdf-script with a file containing something such as:
var f = this.getField("Text1");
f.setAction("MouseUp", "app.alert('Thanks for that');");
David J Prokopetz
Thanks – that worked!

(It also allowed me to dodge another issue I just discovered: namely, that there doesn't appear to be a mechanism for setting a generated PDF form button's icon positioning behaviour via CSS. I'm not terribly surprised at that, as most of those properties have no obvious CSS analogues; it'd be handy to be able to set their initial values when the document is generated, but setting them programmatically when the document is opened is an acceptable workaround.)
wangp
You can use object-fit and object-position properties on image buttons, e.g.
<style>
input[type=image i] {
  background-color: cyan;
  width: 40px;
  height: 30px;
  object-fit: none;
  object-position: top right;
}
</style>

<input type=image src="icon.gif">

It will not always map exactly, as we do not have that degree of control over the icon position in PDF.

(Also, Prince can't yet create buttons that have both an icon and label.)
David J Prokopetz
If I understand correctly, what this means is that, while the value of the control's buttonPosition attribute can't be set explicitly via CSS, one can do so implicitly via the source HTML's type attribute, with a type attribute of "button" corresponding to "position.textOnly", and a type attribute of "image" corresponding to "position.iconOnly" – is that right?

Edited by David J Prokopetz

wangp
I wasn't sure what you were referring to, but I found it:
https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#buttonposition

Yes, that's one way to look at it. More directly, a HTML text button creates a text-only button field in the PDF, and a HTML image button creates an icon-only button field in the PDF.

What I was saying before is that the object-fit and object-position CSS properties allow some control over the scale and alignment of the icon on the button face.
David J Prokopetz
Thanks. One more question: is there any way to specify the name of the document Javascript string produced by the --pdf-script, or – ideally – to split it into multiple, separately named document Javascript strings? It appears that everything passed in using that flag is getting stuffed into a single document Javascript string named "0", which isn't ideal for debugging purposes.
wangp
The latest build (20231005) allows the --pdf-script option to be specified multiple times and the pdf-script JSON job description field to take an array of multiple values.