Fill form - Checkbox
Hi,
First, fantastic work and effort is put in here - the docs, the code, the explanations, the comments! Very concise and user friendly. Thank you for that.
Environment is typescript in node, and pdf-lib v1.16.0.
If in my PDF I have checkboxes like this (this was forwarded by 3rd party person, not generated by me)

and with logs like this
const form = pdfDoc.getForm();
form.getFields().forEach((field) => {
console.log('FIELD ---- ', `name: ${field.getName()}; type: ${field.constructor.name}`);
});
i have output:
FIELD ---- name: Status; type: PDFCheckBox
With command const status = form.getCheckBox('Status'); and status.check(); the only first checkbox gets checked.
The problem for me here is that I don't know how to manipulate with particular checkboxes ('Single or Head of household', 'Married', 'Married, but withhold at higher single rate').
I was wondering where the problem lies:
- in the PDF implementation?
- or am I missing something in my code?
Giving any kind of directions or hints would be very much appreciated. If I can provide more details from logs, please let me know. Cheers!
Can you provide the PDF?
Sure, I modified it and I hope it will work for you as use case. Please contact me if something wrong is with PDF or if anyhow else I can assist.
Looks like all checkboxes have the same name - those should probably be a radio group, since they are mutually exclusive.

If you want to access the individual widgets, try:
const statusField = form.getCheckBox('Status');
console.log(statusField.acroField.getWidgets());
OK, but what can I do with this output?
[
PDFWidgetAnnotation {
dict: PDFDict { dict: [Map], context: [PDFContext] }
},
PDFWidgetAnnotation {
dict: PDFDict { dict: [Map], context: [PDFContext] }
},
PDFWidgetAnnotation {
dict: PDFDict { dict: [Map], context: [PDFContext] }
}
]
It depends on your goal but at this point you're looking at internal apis to figure out how to get done what you want to accomplish.
Ok, with this
const widgets = status.acroField.getWidgets();
console.log('WIDGETS', widgets[0].getOnValue(), widgets[1].getOnValue(), widgets[2].getOnValue());
i get this
WIDGETS PDFName { encodedName: '/Single#2FHOH' } PDFName { encodedName: '/Married' } PDFName { encodedName: '/Higher#20Rate' }
The main question from me is: can I manage to get desired behavior without changing the PDF (because I am not the owner)?
OK, we managed to do it after diving into PDFCheckBox.ts file & setValue method and PDFAcroCheckBox.ts file &setValue method.
PDFCheckBox.ts file:
check() {
const onValue = this.acroField.getOnValue() ?? PDFName.of('Yes');
this.markAsDirty();
this.acroField.setValue(onValue); <<<---- this line
}
PDFAcroCheckBox.ts file:
setValue(value: PDFName) {
const onValue = this.getOnValue() ?? PDFName.of('Yes');
if (value !== onValue && value !== PDFName.of('Off')) {
throw new InvalidAcroFieldValueError();
}
this.dict.set(PDFName.of('V'), value); <<<---- this line
const widgets = this.getWidgets();
for (let idx = 0, len = widgets.length; idx < len; idx++) {
const widget = widgets[idx];
const state = widget.getOnValue() === value ? value : PDFName.of('Off');
widget.setAppearanceState(state); <<<---- and this line
}
}
Our code for demonstration:
const status = form.getCheckBox('Status');
let widgets = status.acroField.getWidgets();
status.acroField.dict.set(PDFName.of('V'), widgets[1].getOnValue());
widgets[1].setAppearanceState(widgets[1].getOnValue());
status.acroField.dict.set(PDFName.of('V'), widgets[0].getOnValue());
widgets[0].setAppearanceState(widgets[0].getOnValue());
status.acroField.dict.set(PDFName.of('V'), widgets[2].getOnValue());
widgets[2].setAppearanceState(widgets[2].getOnValue());
Now we can check all checkboxes if we want while with status.check() we can check only first one.

I have also tried w/ & w/o .markAsDirty() method but it made no visual difference. Any suggestions or corrections are most welcomed!
EDIT: another way of managing something is to edit PDF online with https://www.pdfescape.com/, delete existing checkboxes and insert your own checkbox form fields. In that way you should have desired behavior with .check().
I took a similar approach when field.check() check didn't work for me.
Same scenario where multiple checkboxes for a given field and I want to be able to set one of them
const acroFieldWidgets = field.acroField.getWidgets()
if (!acroFieldWidgets) {
return
}
const acroFieldFormValue = acroFieldWidgets.find(x => x.getOnValue() === PDFName.of(formFieldValue))
if (!acroFieldFormValue) {
return
}
const acroFieldOnValue = acroFieldFormValue.getOnValue()
if (!acroFieldOnValue) {
return
}
acroFieldFormValue.dict.set(acroFieldOnValue, PDFHexString.fromText(formFieldValue))
acroFieldFormValue.setAppearanceState(acroFieldOnValue)
It provided the ability to set an individual check box for a field however I am still facing issues with this, as once I call field.enableReadOnly() It changes the appearance of the checkbox from a cross to a tick.