Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filling form, radio options with same name #811

Closed
rienheuver opened this issue Feb 25, 2021 · 5 comments
Closed

Filling form, radio options with same name #811

rienheuver opened this issue Feb 25, 2021 · 5 comments

Comments

@rienheuver
Copy link

I'm trying to fill out a form from a third-party and they screwed up their form pretty big. When I get the radio group and log the opttons like this:
console.log(form.getRadioGroup('Client 10').getOptions());
I get the following output:
[ 'Keuze', 'Keuze' ]
I.e. they are exactly the same. When I then do
form.getRadioGroup('Client 10').select('Keuze');
It will always select the second option of the radio group. Obviously this form is pretty bad, but it would help a lot if I could select an option based on the index in the options instead of the string.

@mtzmlz
Copy link

mtzmlz commented Apr 26, 2021

Did you find a work around for this? im having the same issue.

@rienheuver
Copy link
Author

No, I haven't found a method yet. Currently my project is still in proof-of-concept phase, but this will get annoying eventually.

@mtzmlz
Copy link

mtzmlz commented Apr 27, 2021

Ok Thanks!

@Hopding
Copy link
Owner

Hopding commented Sep 23, 2021

This can be done if you use the lower level PDFAcroRadioButton API. See also PDFRadioGroup.

I would like to provide better support for this though. I've added it to the roadmap for tracking: #998.

@shadaxv
Copy link

shadaxv commented Jan 11, 2022

Unfortunately, I don't have time to create a proper PR at this point, so I'm adding this as a comment to this thread for now.

PDFRadioGroup can be modified with the following method:

selectByIndex(index: number) {
  assertIs(index, 'index', ['number']);

  this.markAsDirty();

  const onValues = this.acroField.getOnValues();

  if (onValues[index]) {
    this.acroField.setValue(onValues[index]);
  }
}

I also have a simple solution that does not require changes to the library, the value can be selected as follows:

const pdfDoc = await PDFDocument.load(...);
const form = pdfDoc.getForm();
const field = form.getRadioGroup(fieldName);
const onValues = field.acroField.getOnValues();

if (onValues[index]) {
  field.acroField.setValue(onValues[index]);
}

This solution worked for me @rienheuver @mtzmlz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants