PdfFormState

class PdfFormState(document: PdfDocument)

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.

Functions

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
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
fun value(fieldName: String): String?

The value the reader sees: what this state holds, or the file's own value.