Changes

Jump to navigation Jump to search
425 bytes added ,  10:53, 20 February 2022
Restructuring, updates
You can find more about interactive form elements in [http://www.pragma-ade.com/general/manuals/mwidget-s.pdf Widgets uncovered]. It’s written for MkII, but still mostly valid.
Most of the following examples are from <tt>mwidget</tt> the manual. We will cook up our own later.
=JavaScript= If you need to check or otherwise process the input of your forms, you need JavaScript to handle interaction. For simple forms without input validation, you can skip this section. <texcode>\startJSpreamble {name}MyCounter = 0 ;\stopJSpreamble \startJScode {increment}MyCounter = MyCounter + 1 ; // or: ++MyCounter ;\stopJScode \goto {advance by one} [JS(increment)]</texcode> In some versions of MkIV there was a bug that the JS code was only copied to the PDF if there was a \goto referencing one of the defined functions. Should be gone. You can pass values to a JS function: <texcode>\startJScode {increment}MyCounter = MyCounter + JS_V_1 ;\stopJScode \goto {advance by five} [JS(increment{V{5}})]</texcode> * V{} is verbose, defaults to string* S{} = as string* R{} = as reference* JS_V_n, JS_S_n, JS_R_n are the names of the variables* JS_N keeps the number of arguments == Documentation == JavaScript in Acrobat is different than in a web context. Documentation is even more sparse than on ConTeXt ;)Debugging is only possible in Acrobat Pro, and also there very inconvenient.Additionally, Acrobat’s possibilities change with every version. * [http://www.adobe.com/devnet/acrobat/javascript.html JavaScript documentation at Adobe’s]* [http://help.adobe.com/livedocs/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/wwhelp/wwhimpl/js/html/wwhelp.htm?href=JS_Dev_Tools.72.1.html&accessible=true JavaScript API Reference for Acrobat 9]* [https://acrobatusers.com/tutorials/javascript_console Tutorial on JS in Acrobat 11] == Examples == === Setting a default value ==Fields=
Here we set Create a date field to the current date on opening the document.Additionally we have a button that can hide/show a form “class” with {{cmd|setupfield}}, then create single instances with {{cmd|definefield}}, finally typeset it with {{cmd|field}}.
<texcode>\starttext\setupinteraction [state=start]That might look complicated, but you can use the same field several times, and the contents will automatically repeat themselves if you need the same content at several places, even on different pages. (Think e.g. of name, location and date in a set of forms.)
\startJSpreamble {EXAMPLE} used now
var d = new Date();
var df = this.getField("CurDate");
df.value = util.printd("dd.mm.yyyy", d);
 
function toggleField(){
var f = this.getField("CurDate");
f.display = ! f.display;
}
\stopJSpreamble
 
\setupfield[shortString][reset,horizontal][height=5mm, width=50mm, frame=off, bottomframe=on]
\definefield[CurDate][line][shortString][][JavaScript should replace this text with the current date]
 
Current date: \field[CurDate]
 
\stoptext
</texcode>
 
===Setting the current date===
Similar, but more usable than the example above:
 
<texcode>
\starttext
 
\setupinteraction [state=start]
 
\startJSpreamble {EXAMPLE} used now
function Dummy(){
return 0;
}
 
function setCurrentDate(fieldname) {
var f = this.getField(fieldname);
f.value = util.printd("yyyy-mm-dd", new Date());
}
 
setCurrentDate("myDateField");
\stopJSpreamble
 
\setupfield[dateString][reset,horizontal][width=5em,option=printable]
\definefield[myDateField][line][dateString][][JavaScript should replace this text with the current date]
 
Current date: \field[myDateField]
\stoptext
</texcode>
 
 
=Fields=
 
{{Explanation}}
Relevant commands:
* {{cmd|setupfield}}, {{cmd|setupfields}}
* {{cmd|definefield|[name][type][setup name][content values][default content]}}
* {{cmd|field|[name]}}
* {{cmd|fillinline}}
* {{cmd|fillintext}}
* {{cmd|fillinrules}}
* {{cmd|fillinfield}} (defined in the {{src|m-fields.mkiv|fields}} module, broken)
Field types:
* radio: radiobutton (only one of a group can be active)
* check: checkbox
* signature: electronic signature (since ConTeXt beta of 2016-03-11)
Beware, for fillinfields in MkIV you need {{code|\usemodule[===Single fields]}}!They’re meant for clozes (texts with gaps, like in questionnaires).===
In MkIV (as of 2015-04-01) default values are always used verbatim, i.e. JS() doesn’t work. (Check: other bug is gone, maybe this also?) Other Since you often need fields you must first define and then use. That might look complicatedonly once, but you can use the same field several times, and the contents will automatically repeat themselves if you need the same content at several places, even on different pages. ==Text Entries==define your own shortcut macros:
<texcode>
\fillinfieldsetupfield[nameMyFieldSetup][reset,horizontal][... options ...]\define[1]\MyField{text that defines \definefield[#1][line][MyFieldSetup]\field length[#1]}
</texcode>
If you use this command in MkIV, avoid using default validation with <tt>\setupfieldcategory[fillinfield][validate=]</tt>. The default validation removes the contents from field.=Text Entries==
<context source=yes>
\field [Email] [your email]
</context>
 
==Checkboxes==
 
<texcode>
\setupfield[setup 3]
[width=2cm, height=2cm,
rulethickness=3pt, corner=round, framecolor=red]
 
\definesymbol [yes] [{\externalfigure[mpcont.502]}]
\definesymbol [no] []
\definefield [checkme][check] [setup 3] [yes,no] [no]
\field[checkme]
</texcode>
==Radiobuttons==
</texcode>
==CheckboxesFillin fields == These are meant for clozes (texts with gaps, like in questionnaires); they’re defined in the {{src|m-fields.mkiv|fields}} module (i.e. you need {{code|\usemodule|[fields]}}) and more or less broken (current state unknown). * {{cmd|fillinline}}* {{cmd|fillintext}}* {{cmd|fillinrules}}* {{cmd|fillinfield}} <texcode>\fillinfield[name]{text that defines field length}</texcode> Avoid using default validation with {{code|\setupfieldcategory|[fillinfield][validate=]}}, because it removes the contents from the field. =JavaScript= If you want to check or otherwise process the input of your forms, you need JavaScript to handle interaction. For simple forms without input validation, you don’t need this. '''Beware:''' JavaScript in PDFs works only in Adobe Acrobat Reader/Professional (Win/Mac only) and very few other PDF editors, e.g. [[https://www.qoppa.com|Qoppa]]’s PDF Studio (also on Linux) supports JS even in the free viewer, but used to crash on all forms created with TeX in the 2019 version, maybe this is fixed now. <texcode>\startJSpreamble {name}MyCounter = 0 ;\stopJSpreamble \startJScode {increment}MyCounter = MyCounter + 1 ; // or: ++MyCounter ;\stopJScode \goto {advance by one} [JS(increment)]</texcode> You can pass values to a JS function: <texcode>\startJScode {increment}MyCounter = MyCounter + JS_V_1 ;\stopJScode \goto {advance by five} [JS(increment{V{5}})]</texcode> * V{} is verbose, defaults to string* S{} = as string* R{} = as reference* JS_V_n, JS_S_n, JS_R_n are the names of the variables* JS_N keeps the number of arguments == Documentation == JavaScript in Acrobat is different than in a web context. Documentation is even more sparse than on ConTeXt ;)Debugging is only possible in Acrobat Pro, and also there very inconvenient.Additionally, Acrobat’s possibilities change with every version. * [http://www.adobe.com/devnet/acrobat/javascript.html JavaScript documentation at Adobe’s]* [http://help.adobe.com/livedocs/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/wwhelp/wwhimpl/js/html/wwhelp.htm?href=JS_Dev_Tools.72.1.html&accessible=true JavaScript API Reference for Acrobat 9]* [https://acrobatusers.com/tutorials/javascript_console Tutorial on JS in Acrobat 11] == Examples == === Setting a default value === Here we set a date field to the current date on opening the document.Additionally we have a button that can hide/show a form field. <texcode>\starttext\setupinteraction [state=start] \startJSpreamble {EXAMPLE} used nowvar d = new Date();var df = this.getField("CurDate");df.value = util.printd("dd.mm.yyyy", d); function toggleField(){ var f = this.getField("CurDate"); f.display = ! f.display;}\stopJSpreamble \setupfield[shortString][reset,horizontal][height=5mm, width=50mm, frame=off, bottomframe=on]\definefield[CurDate][line][shortString][][JavaScript should replace this text with the current date] Current date: \field[CurDate] \stoptext</texcode> ===Setting the current date===Similar, but more usable than the example above:
<texcode>
\setupfieldstarttext \setupinteraction [setup 3state=start] [width\startJSpreamble {EXAMPLE} used nowfunction Dummy(){ return 0;} function setCurrentDate(fieldname) { var f =2cm, heightthis.getField(fieldname); f.value =2cmutil.printd("yyyy-mm-dd",new Date());} rulethickness=3ptsetCurrentDate("myDateField");\stopJSpreamble \setupfield[dateString][reset, cornerhorizontal][width=round5em, framecoloroption=redprintable]\definefield[myDateField][line][dateString][][JavaScript should replace this text with the current date]
Current date: \definesymbol [yes] [{\externalfigure[mpcont.502]}]\definesymbol [no] field[myDateField]\definefield [checkme][check] [setup 3] [yes,no] [no]\field[checkme]stoptext
</texcode>
 
=Tricks and Traps=
==MkIV==
* JS code is was only copied to the PDF if there is was a {{cmd|goto}} referencing one of the defined functions! – This is actually a feature, you can get your JS without {{cmd|goto}}, using the magic incantation <code>used now</code>, as in the default value example.(This was fixed.)
* JS code for default values doesn’t work (reported 2015-04-01, still true 2015-10-07); default values are always used verbatim.(Unchecked if this is fixed.)
* There is no {{cmd|setupfields}} (plural)!
\tracefieldstrue
\showfields % typeset a table of field relations
\logfields % logs field descriptions to a file fields.log
</texcode>

Navigation menu