question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fill form - Checkbox

See original GitHub issue

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) image

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!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
StefanZivkoviccommented, Feb 5, 2021

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. image

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().

1reaction
quickskapecommented, Feb 3, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fill check box form - Microsoft Community
Hello, I want fill checked box form , but when i double-click in button ... It would appear that you have inserted a...
Read more >
Fill checkbox on selection with CSS - Stack Overflow
Here's a nifty CSS-only solution for custom checkboxes: .hideme { display: none; } .woo { cursor: pointer; } .hideme + .woo:before { width: ......
Read more >
How to Insert a Checkbox in Word | Make a Checklist in Word
Word allows you to add checkboxes so that users can fill in the survey digitally. First, let's move the cursor and click on...
Read more >
How to Create Checkbox for Fillable Forms in Word 2016
This video shows how to insert a checkbox in Word 2016 document. A Checkbox provides an easy way for a user to select...
Read more >
How to Make a Checkbox in HTML [+Examples] - HubSpot Blog
You want to make your forms as easy to fill out as possible. ... In HTML, a checkbox is an <input element> with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found