[NTG-context] Widgets: Any good examples for developing JS-enabled PDFs that calculate?

Hans Hagen j.hagen at xs4all.nl
Mon Nov 18 09:48:19 CET 2019


On 11/17/2019 9:13 PM, Paul Mazaitis wrote:
> 
> Hi everyone,
> 
> First: apologies for the long email; this got a little complicated.
> 
> Summary: there may be a bug in setting up calculate field for widgets?

it's more that javascript in pdf is kind of unreliable and can interfere 
in all kind of ways with settings in the viewer

so, what you need to do it

- play safe (only use actions that don't relate to something clever in 
the viewer, and calculat eis one of them)
- be redundant (hook into several actions)
- disable the spell checker
- enable javascript
- disable some security stuff
- use the debugger (which can be erratic but at least you can see 
something; in my older acrobat doesn't seem to work with field related 
scripts)
- put code in functions (easier to debug)

and

- cross your fingers

Here's something to play with ... note the Number conversion!

\usemodule[fields]

\starttext

\setupinteraction [state=start]

\startJSpreamble {whatever} used now
function recalculate() {
     var a_fld = this.getField("A");
     var b_fld = this.getField("B");
     var t_fld = this.getField("T");
     var a_val = Number(a_fld.value);
     var b_val = Number(b_fld.value);
     var t_val = a_val + b_val;
     t_fld.value = String(t_val);
     console.clear ;
     console.println("A: "+a_val) ;
     console.println("B: "+b_val) ;
     console.println("T: "+t_val) ;
     console.println("?: "+t_fld.value) ;
     this.dirty = true;
}
\stopJSpreamble

\setupfield
   [numField]
   [reset,horizontal]
   [height=2pc,
    rulethickness=1pt,
    afterkey=JS(recalculate{}),
    focusout=JS(recalculate{}),
    framecolor=lightgrey]

\setupfield
   [totalField]
   [reset,horizontal]
   [height=2pc,
    rulethickness=1pt,
    option=readonly,
    framecolor=lightgrey]

\definefield[A][line][numField]  [0]
\definefield[B][line][numField]  [0]
\definefield[T][line][totalField][0]

A: \field[A]\par
B: \field[B]\par
T: \field[T]\par

\stoptext



-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the ntg-context mailing list