JFLib

pdf/src/form.rs#

Source: lib/pdf/src/form.rs

form -- the fields of an interactive form the writer makes (ISO 32000-1, 12.7): text fields,
check boxes, radio buttons, combo and list boxes, push buttons and empty signature fields,
each a widget on its page with its appearance streams (appear.rs), in the document's
AcroForm.

  c.text_field(x, y, w, h, "person.name", &TextField { value: "Jakob", ..TextField::default() });
  c.check_box(x, y, w, h, "agree", true, &FieldStyle::default());
  c.radio(x, y, w, h, "size", "M", true, &style);      // one name: one group of buttons
  c.combo_box(x, y, w, h, "land", &["DE", "AT"], "DE", false, &style);
  c.list_box(x, y, w, h, "colours", &["red", "blue"], &["blue"], false, &style);
  c.push_button(x, y, w, h, "ok", "OK", Some("https://example.org/"), &style);
  c.signature_field(x, y, w, h, "signature");

Names with dots make parent fields ("person.name" is the field "name" under "person").  A
name given to fields of two kinds, or both to a field and as a parent, a font that is not the
document's, a combo box's value that is none of its options (unless it is editable) make
write() fail (Error::Invalid).  A radio button's on state is its place in its group ("0",
"1", ...) and its export value that place of the group's /Opt, so any text is an export
value; a check box's on state is /Yes.  A widget keeps its border's width in /BS, so an
appearance made again has it; a comb is one line (with multiline or password, no comb).

Fonts: FieldStyle::font is Helvetica (one of the 14 standard fonts, WinAnsiEncoding, not
embedded; a character outside WinAnsi makes write() fail) or a font of the document, which is
then embedded in full (no subset tag, every glyph, the widths of every glyph) so a viewer can
set any text the font has.  Size 0 is automatic.  Check boxes and radio buttons draw
ZapfDingbats' check mark and dot.  The form's /DA is "/Helv 0 Tf 0 g"; NeedAppearances is not
set, every widget has its appearance.
  • enum Align how a text field's lines sit (/Q)

    • Left
    • Center
    • Right
  • enum FieldFont the font of a field's text

    • Helvetica -- the standard font Helvetica (WinAnsiEncoding, not embedded)
    • Embedded(FontId) -- a font of the document, embedded in full
  • struct FieldStyle what a field looks like

    • border: Option<Rgb> -- the border's colour (None: no border) and width
    • border_width: f64
    • background: Option<Rgb>
    • text: Rgb
    • font: FieldFont
    • size: f64 -- points; 0 is automatic
  • struct TextField<'a> a text field

    • value: &'a str
    • align: Align
    • multiline: bool
    • password: bool
    • max_len: Option<u32>
    • comb: bool -- max_len cells of one character each
    • read_only: bool
    • required: bool
    • style: FieldStyle

impl Canvas<'_>

  • fn text_field(&mut self, x: f64, y: f64, w: f64, h: f64, name: &str, f: &TextField) a text field on the rectangle x, y, w, h
  • fn check_box(&mut self, x: f64, y: f64, w: f64, h: f64, name: &str, checked: bool, style: &FieldStyle) a check box, checked or not
  • fn radio(&mut self, x: f64, y: f64, w: f64, h: f64, name: &str, value: &str, selected: bool, style: &FieldStyle) a radio button of the group name with the export value value; the last one selected of a group is on. Its on state is its place in the group ("0", "1", ...), its export value that place of the group's /Opt (12.7.4.2.4), so any text is an export value
  • fn combo_box(&mut self, x: f64, y: f64, w: f64, h: f64, name: &str, options: &[&str], value: &str, editable: bool, style: &FieldStyle) a combo box: one of the options shown, typed text too when editable
  • fn list_box(&mut self, x: f64, y: f64, w: f64, h: f64, name: &str, options: &[&str], selected: &[&str], multi: bool, style: &FieldStyle) a list box with the options selected chosen (several only with multi)
  • fn push_button(&mut self, x: f64, y: f64, w: f64, h: f64, name: &str, caption: &str, uri: Option<&str>, style: &FieldStyle) a push button with a caption, opening uri when pressed
  • fn signature_field(&mut self, x: f64, y: f64, w: f64, h: f64, name: &str) an empty signature field, to be signed later

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