PdfFormState

class PdfFormState(document: PdfDocument)(source)

The live values of a document's form fields, while the reader has it open.

A file's own values are what PdfFormField.value reports, and they never change. This holds what the form looks like now: what the reader typed, what a script wrote, which box is ticked. A viewer draws from it, a script reads and writes it, and io.github.yuroyami.kitepdf.writer.PdfEditor saves it.

Nothing here touches the file. Saving is a separate step, so a reader can fill a form, change its mind, and close without writing anything.

val state = PdfFormState(doc)
state.setValue("total", "42")
state.value("total") // "42"
doc.formField("total")?.value // still what the file says

A viewer keeps one for as long as the document is open, and reads revision to know when to redraw. Reads and writes are safe from two threads, because a document's scripts run on a thread of their own while the viewer draws on its own: the engine that runs the scripts is confined to one thread, but the values it writes are read by another.

Constructors

Link copied to clipboard
constructor(document: PdfDocument)

Types

Link copied to clipboard
class Change

What changed, for a listener that redraws only the widgets that moved.

Properties

Link copied to clipboard

Every field this state has a value for, in the order they were first set.

Link copied to clipboard

How many changes this state has seen. A viewer that keeps a drawn page can compare it with the number it drew at to know whether anything moved. It is read under the same lock that guards the writes, so a thread that did not write it still sees the latest number (#364).

Functions

Link copied to clipboard
fun cancelChoiceTransaction(fieldName: String, expectedRevision: Int): Boolean

Cancels a pending choice edit only while its expectedRevision is still current. This advances the field revision without changing its value or requesting a redraw, so an Escape during validation can prevent the eventual store (ISO 32000-1, 12.6.3 and 12.7.4.4). If the edit already committed or another change intervened, returns false and leaves that newer state intact.

Link copied to clipboard

The complete live choice value, falling back to the unchanged source file.

Link copied to clipboard
fun fieldRevision(fieldName: String): Int

Changes and reset requests for this field alone; useful for discarding a stale edit.

Link copied to clipboard
fun isChanged(fieldName: String): Boolean

True when this state has its own value for the field, so the file's is out of date.

Link copied to clipboard
fun isHidden(fieldName: String): Boolean

Whether the field is drawn: what this state says, or the Hidden flag of the field's own widget (ISO 32000-1 §12.5.3, Table 165, bit 2). A script changes it through field.display or field.hidden.

Link copied to clipboard
fun isReadOnly(fieldName: String): Boolean

Whether the reader may change the field: the file's own flag, unless this state says otherwise.

Link copied to clipboard
fun onChange(listener: (PdfFormState.Change) -> Unit): () -> Unit

Calls listener after every change, with the field that moved. Returns a function that stops the listening.

Link copied to clipboard
fun reset(fieldName: String)

Drops this state's value for fieldName, so the file's own value shows again.

Link copied to clipboard
fun resetAll()

Drops every value, visibility and read-only change this state holds.

Link copied to clipboard

Performs a reset-form action: the fields it names, by name or by reference, or every field but those when its Include/Exclude flag is set, or every field when it names none. See the other resetForm.

fun resetForm(fields: Collection<String>? = null, exclude: Boolean = false)

Gives each selected field its default value (/DV) back, or no value when it has none, as a reset-form action and a script's resetForm do (ISO 32000-1, 12.7.5.3). A name selects that field and every field below it, so address selects address.street. With exclude, every field that the names do not select is reset instead. With no names at all, every field is reset. A push button and a signature keep theirs, and visibility and read-only changes stay (#439).

Link copied to clipboard

Stores one validated selection and publishes one revision. Returns false for a missing field or an invalid value. Programmatic writes, like setValue, may update read-only fields. A user transaction should use the overload with an expected field revision.

fun setChoiceSelection(fieldName: String, selection: PdfChoiceSelection, expectedRevision: Int): Boolean

Commits only if the field has not changed since expectedRevision and remains visible and writable. The revision check and update share one lock, so a late script cannot replace a newer choice or one whose flags changed while the script ran. This overload can also restore the exact parsed source or default selection, including unresolved source values, when an editor cancels an edit. Arbitrary unresolved values remain invalid; user commits should first call PdfFormField.validateChoiceSelection.

Link copied to clipboard
fun setHidden(fieldName: String, value: Boolean)

Hides or shows the field, whatever the file's own flag says.

Link copied to clipboard
fun setReadOnly(fieldName: String, value: Boolean)

Makes the field read-only, or gives it back to the reader.

Link copied to clipboard
fun setValue(fieldName: String, value: String)

Sets the value shown for fieldName. Does nothing when the document has no such field, so a script that names a missing field cannot grow the state.

Link copied to clipboard

Captures the accepted values, choice selections and field flags at one instant. A renderer can keep this copy through a suspended or repeated draw, so a script cannot change the form halfway through constructing its appearances (ISO 32000-1, 12.7.3.3). All state and revision counters are copied together under the same lock.

Link copied to clipboard
fun value(fieldName: String): String?

The live scalar value, or the file's own value. For choices this is a lossy view: the first selected export in option order, never a joined list. Use choiceSelection for multiple selections and to distinguish an empty selection from an empty export.