import type { FormFieldRef, FormWidget } from '../identity/FormFieldRef'; import type { IsoDateTime } from '../dto/IsoDateTime'; import type { PdfFieldActions } from '../dto/PdfAction'; import type { Coordinates, PageCoordinates } from '../pageSpace/coordinates'; import type { FormValueEntry } from './value-entry'; /** * The field kind — the discriminant of {@link FormField}. Narrowing on * it reveals exactly the members that are meaningful for that kind, the * same way annotation DTOs narrow on `subtype`. */ export type FormFieldKind = | 'text' | 'checkbox' | 'radio' | 'combobox' | 'listbox' | 'pushbutton' | 'signature' | 'unknown'; /** * Where the field was found. * * - `acroform` — reachable from the /AcroForm /Fields tree, as the spec * requires. * - `recovered` — only reachable through a page's /Annots array (a common * producer bug). The engine reconciles these on every read so they work * like any other field, but other PDF processors will not see them until * {@link DocumentFormsService.repair} makes the fix durable. */ export type FormFieldOrigin = 'acroform' | 'recovered'; /** * A widget of a field: its address and its page. Its place, look and state * are its row in the form's `widgets`, joined by `ref`. */ export type FormFieldWidget = FormWidget; /** * A widget of a toggle (checkbox/radio) field. Toggle widgets always carry * their appearance-state machinery — no nullable fields to probe. */ export interface ToggleFieldWidget extends FormFieldWidget { /** * The widget's appearance state name in the file (the non-"Off" key of * its /AP /N dictionary), usually its export value. Writes never need it. */ onState: string; /** * The widget's export value (/Opt entry when present, else the * on-state). The identity FDF/XFDF carry. */ exportValue: string; /** Whether this widget is currently checked. */ checked: boolean; } /** One option of a choice (combo/list box) field. */ export interface FormFieldOption { /** Display label shown to the user. */ label: string; /** Export value used by /V, interchange, and `FormFieldValue.choice`. */ value: string; /** Whether the option is currently selected. */ selected: boolean; } /** * The trunk every field kind shares: identity, provenance, universal * flags, and widget placement. A logical field is the document-scoped * record that holds the value; its widgets are page-scoped views — join * them to the annotation subsystem via each widget's `ref`. */ export interface FormFieldBase { /** * Durable ref: the field dictionary's object number, or its full name when * the dictionary is a direct object (spec-violating) and has no number. */ ref: FormFieldRef; /** Fully qualified name, e.g. `"billing.name"`. */ name: string; kind: FormFieldKind; origin: FormFieldOrigin; /** * The user must not change the value. The engine's write transactions * still accept programmatic writes to read-only fields (calculated * fields are read-only yet script-written); enforcing fill policy is * the application's job. */ readOnly: boolean; /** PDF apps ask for a value before the form is submitted. */ required: boolean; /** Left out when the form is submitted or exported. */ noExport: boolean; /** /TU — the accessible tooltip / alternate name. */ alternateName: string | null; /** /TM — the export mapping name. */ mappingName: string | null; /** Exact effective `/V` object shape; kind conveniences below are derived. */ valueEntry: FormValueEntry; /** Exact effective `/DV` object shape, including absent and malformed values. */ defaultValueEntry: FormValueEntry; /** Effective inherited field `/AA` actions. */ actions?: PdfFieldActions; /** * The group the field belongs to: who fills it in (the buyer's fields). * `fields:` scopes grant filling in and signing by group. `null` for a * field in no group. */ groupId: string | null; /** * The user who created the field, as the creating session's identity * named them; `null` for a field another tool made, or an anonymous * session. */ createdBy: string | null; /** When the field was created; `null` when nobody recorded it. */ createdAt: IsoDateTime | null; /** * The user whose write last changed the value: a fill, or the script that * fill ran (a calculated total counts as filled by whoever's fill * calculated it). `null` after a reset, or when an anonymous session * filled it last. */ filledBy: string | null; /** That user's display name, as their session gave it. */ filledByName: string | null; /** When the value last changed by a fill; `null` with `filledBy`. */ filledAt: IsoDateTime | null; /** The session that restored this field's attribution in an import. */ importedBy: string | null; /** The field's widget annotations, in control order. May be empty ("unplaced"). */ widgets: FormFieldWidget[]; } /** A text field. Write with `{ value }`. */ export interface TextField extends FormFieldBase { kind: 'text'; value: string; /** /DV — restored by `reset()`. */ defaultValue: string; /** /MaxLen; `null` when unlimited. Longer writes are truncated to this length. */ maxLength: number | null; multiline: boolean; password: boolean; /** Fixed character cells; meaningful with `maxLength`. */ comb: boolean; } /** A checkbox. Write with `{ checked }`, or `{ value }` naming a widget's export value. */ export interface CheckboxField extends FormFieldBase { kind: 'checkbox'; checked: boolean; /** The export value reported while checked ("Off" is never exported). */ exportValue: string; widgets: ToggleFieldWidget[]; } /** A radio group: One field, N widgets. Write with `{ value }`, a button's export value. */ export interface RadioField extends FormFieldBase { kind: 'radio'; /** The checked widget's export value, or `"Off"` when the group is clear. */ value: string; /** Widgets sharing an export value check together. */ radiosInUnison: boolean; /** The group cannot be cleared once a choice is made. */ noToggleToOff: boolean; widgets: ToggleFieldWidget[]; } /** A combo box (dropdown). Write with `{ value }`. */ export interface ComboBoxField extends FormFieldBase { kind: 'combobox'; /** An option export value — or free text when `edit` is set. */ value: string; /** /DV — restored by `reset()`. */ defaultValue: string; /** Free text is allowed in addition to the options. */ edit: boolean; options: FormFieldOption[]; } /** A list box. Write with `{ selectedValues }`. */ export interface ListBoxField extends FormFieldBase { kind: 'listbox'; /** Selected option export values, in option order. */ selectedValues: string[]; /** /DV — the option values `reset()` selects. */ defaultValue: string[]; /** Whether several options may be selected at once. */ multiSelect: boolean; options: FormFieldOption[]; /** The option shown in the box's top row (`/TI`): its position in `options`. */ topIndex: number; } /** A push button: pure trigger, holds no value. Never written or exported. */ export interface PushButtonField extends FormFieldBase { kind: 'pushbutton'; } /** * A signature field: identity and placement only. Form writes never set a * signature value; signing goes through the signatures API. */ export interface SignatureField extends FormFieldBase { kind: 'signature'; } /** * Forward-compat placeholder for field types the engine does not model, * mirroring the `unsupported` annotation kind. Round-trips safely. */ export interface UnknownField extends FormFieldBase { kind: 'unknown'; /** The raw /V value as text, for diagnostics. */ rawValue: string; } /** * A logical form field. Discriminated on `kind`: * * ```ts * if (field.kind === 'listbox') { * field.selectedValues; // string[] * } * ``` */ export type FormField = | TextField | CheckboxField | RadioField | ComboBoxField | ListBoxField | PushButtonField | SignatureField | UnknownField;