JFLib

pdf/src/edit/form.rs#

Source: lib/pdf/src/edit/form.rs

edit::form -- filling in the fields of an interactive form (ISO 32000-1, 12.7) and flattening
them into their pages.

set_field finds a terminal field by its full name (the partial names of the parents joined by
dots), checks the value against the field's kind and gives the field its /V and each widget
a new normal appearance (appear.rs) or, for buttons, its /AS:

  Text(s)          a text field (MaxLen counts characters), an editable combo box
  Choice(values)   a list box or combo box: export values or the texts shown (/Opt), several
                   only with MultiSelect; a radio button group or check box: the export
                   value or state name of the button to turn on (none: all off)
  Check(on)        a check box

A text field's /RV is dropped (it would show the old value), the AcroForm's /XFA too (Acrobat
would show the XFA data instead).  A form with NeedAppearances gets the appearances of all its
fields with the first value set, and the flag goes (as Acrobat saves): a viewer would else
make them again in the /DA font and lose what a fallback font shows.  Read-only fields are filled as any other (Field::read_only
says which they are); signature fields and push buttons take no value.  JavaScript (format,
calculate, /AA) is not run.

The text is set in the font of /DA from the form's /DR when it has every character (a simple
font: its encoding; an embedded subset only for the codes it has widths for).  Otherwise the
whole text takes fonts of the FontSource (set_fonts; SystemFonts by default): a sans-serif
substitute of the /DA font for Latin, Greek and Cyrillic, and one of the language for other
scripts (kana ja, hangul ko, Han after set_lang or the document's /Lang, else zh-Hans; Arabic,
Hebrew, Thai, Indic and others by their language, then the usual families of Windows, macOS
and Linux for the script), each the first that has the characters it is for; embedded as
subsets in the update, once for all its appearances.  /DA itself stays as it is.  Drawing
(widget_ap) makes appearances from the same parts, in the /DA font alone.

flatten draws each visible widget's appearance into its page (a form XObject after the page's
content, which is wrapped in q .. Q; under the widget's /OC as marked content) and takes the
widget off the page and the field out of the form, parents left without kids too; flattening
every field removes the AcroForm.  Signature fields stay.  Fields whose appearance is missing
or that NeedAppearances asks for get one first, from the value they have (/V inherited too,
/I choosing among options of one export value); their values do not change, and an
appearance that cannot be made again stays as it is.  A form with NeedAppearances keeps the
flag when one of its fields' appearances cannot be made.

set_field and flatten are transactions: when they fail, the update is as it was (the objects
they added gone, those they changed as before).

Fields are indirect objects (12.7.3.1); one that is not is changed inside the object that
holds it.
  • enum FieldValue<'a> a value for Update::set_field
    • Text(&'a str)
    • Check(bool)
    • Choice(&'a [&'a str])

impl Update<'_>

  • fn set_fonts(&mut self, fonts: Rc<dyn crate::read::FontSource>) where fallback fonts come from (SystemFonts by default)
  • fn set_lang(&mut self, lang: &str) the language of Han characters in values (zh-Hans, zh-Hant, ja, ko; else the document's /Lang)
  • fn set_field(&mut self, name: &str, value: FieldValue) -> Result<(), Error> fill in a field (see the head of edit/form.rs); when it fails, the update is as it was
  • fn flatten(&mut self, names: Option<&[&str]>) -> Result<(), Error> draw the fields (all, or those named and their kids) into their pages and take them out of the form; when it fails, the update is as it was

Source: docs/API.md in the JFLib repository.