* Set the checkbox value. * * This method is async because it regenerates the field's appearance * stream after setting the value. * * @param value "Off" or one of the on-values * @throws {Error} if field is read-only or value is invalid
(value: string)
| 97 | * @throws {Error} if field is read-only or value is invalid |
| 98 | */ |
| 99 | setValue(value: string): void { |
| 100 | this.assertWritable(); |
| 101 | |
| 102 | // Validate value |
| 103 | if (value !== "Off" && !this.getOnValues().includes(value)) { |
| 104 | throw new Error(`Invalid value "${value}" for checkbox "${this.name}"`); |
| 105 | } |
| 106 | |
| 107 | // Set /V on field |
| 108 | this.dict.set("V", PdfName.of(value)); |
| 109 | |
| 110 | // Update /AS on each widget |
| 111 | for (const widget of this.getWidgets()) { |
| 112 | const widgetOnValue = widget.getOnValue(); |
| 113 | const state = widgetOnValue === value ? value : "Off"; |
| 114 | |
| 115 | widget.setAppearanceState(state); |
| 116 | } |
| 117 | |
| 118 | this.needsAppearanceUpdate = true; |
| 119 | |
| 120 | // Regenerate appearance immediately |
| 121 | this.applyChange(); |
| 122 | } |
| 123 | } |
no test coverage detected