Changes

Jump to navigation Jump to search
5,913 bytes added ,  18:14, 8 June 2020
m
no edit summary
< [[VisualsPresentations]] | [[Basics|Text formatting]] | [[Interaction]] >
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.
At the moment all Most of the following examples are from this <tt>mwidget</tt> manual. We will cook up our own later.
For PDF is only an interface, you need =JavaScript to handle interaction (field input etc.).=
=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>
\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:
* S{} = as string
* R{} = as reference
* JS_V_n, JS_S_nm 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 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:
* line: one line of text
* text: more lines of text
* 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[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 fields you must first define and then use. 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.
==Text Entries==
 
* <cmd>fillinfield</cmd>
* <cmd>fillinlines</cmd>
* <cmd>fillintext</cmd>
* <cmd>fillinrules</cmd>
<texcode>
\fillinfield[name]{text that defines field length}
% or</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. <context source=yes>\setupfield[ShortLine][horizontal][width=2cm,height=2em]
\definefield [Email] [line] [ShortLine] [] [sample@contextgarden.net]
\field [Email] [your email]
</texcodecontext>
==Radiobuttons==
Sample Example from the manual:
<texcode>
height=4cm,
frame=off,
background=screencolor, backgroundcolor=lightgray]
\definefield[Logos] [radio] [LogoSetup][ConTeXt,PPCHTEX,TeXUtil] [PPCHTEX]
\definesubfield [ConTeXt] [] [ConTeXtLogo]
\hbox to \hsize{\hss\field[ConTeXt]\hss\field[PPCHTEX]\hss\field[TeXUtil]\hss}
</texcode>
 
As usual, first you need to define a class of fields ({{cmd|setupfield}}). Then you define the (invisible) group of radio buttons ({{cmd|definefield}} with "radio"). At last you define the single radio buttons with {{cmd|definesubfield}}.
 
Arguments of {{cmd|definefield}}:
# field name
# field type "radio"
# setup class, as defined by {{cmd|setupfield}}
# list of field names that should be part of the group
# name of default (activated) button
 
Arguments of {{cmd|definesubfield}}:
# field name
# setup class (default is inherited, but you can use a different one)
# content symbol, defined by {{cmd|definesymbol}}
 
===Setup for questionnaire===
 
If you need a lot of similar radiobuttons, like in a questionnaire where you answer every question with a range choice, a meta definition makes sense. Fortunately it’s quite easy:
 
<texcode>
\setupfield [ChoiceSetup][width=1em,height=1em,corner=00]
 
\definesymbol[X][X] % replace with dingbat symbol
 
\def\Choice#1{\definefield[#1:main][radio][ChoiceSetup][#1:1,#1:2,#1:3,#1:4,#1:5,#1:0][#1:0]%
\definesubfield [#1:1][][X]%
\definesubfield [#1:2][][X]%
\definesubfield [#1:3][][X]%
\definesubfield [#1:4][][X]%
\definesubfield [#1:5][][X]%
\definesubfield [#1:0][][X]%
\field[#1:1]\,\field[#1:2]\,\field[#1:3]\,\field[#1:4]\,\field[#1:5]\hskip1em\field[#1:0]}
 
\def\ChoiceTitle{\hfill$-$\hskip4em$+$\hskip1.25em?\ \strut\par}
 
\def\Question{\dosingleempty\doQuestion}
% We need the "optional" parameter as reference
\def\doQuestion[#1]#2{%
\iffirstargument
#2\dotfill\Choice{#1}\par
\else
#2\par
\fi
}
 
\starttext
 
\ChoiceTitle
\Question[q:ctx]{How much do you love \CONTEXT?}
\Question[q:lua]{How are your Lua skills?}
\Question[q:xml]{How often do you dream in XML?}
 
\stoptext
</texcode>
</texcode>
=Tricksand Traps= ==MkIV== * JS code is only copied to the PDF if there is 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. * JS code for default values doesn’t work (reported 2015-04-01, still true 2015-10-07). * There is no {{cmd|setupfields}} (plural)! ==MkII==
This helps debugging:
\logfields % logs field descriptions to a file fields.log
</texcode>
 
=Samples=
 
* [[Midgard PC sheet]] (RPG character sheet with lots of text fields in tables)
 
[[Category:Interaction]]
[[Category:PDF]]

Navigation menu