Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
7.04k stars 677 forks source link

PDFAcroRadioButton is not found using doc.findPageForAnnotationRef(acroField.ref) API #1375

Open mkhatib opened 1 year ago

mkhatib commented 1 year ago

What were you trying to do?

I am trying to find the page on which a PDF form radio button is on. The doc.findPageForAnnotationRef(acroField.ref) works for these types (PDFAcroText, PDFAcroCheckBox, PDFAcroComboBox) but not for PDFAcroRadioButton - returns undefined - I tried on multiple PDF forms.

Is this by design or a bug? How do I find what page the radio button is on?

How did you attempt to do it?

  const allFields = doc.getForm().acroForm.getAllFields();
  const fieldsForPage = allFields
    .forEach((field) => {
      const [acroField] = field;
      const fieldPage = doc.findPageForAnnotationRef(acroField.ref);
      console.log(acroField, fieldPage); // For the radio radio button acro field it prints undefined for fieldPage
    });

What actually happened?

The API returned undefined

What did you expect to happen?

The API would return an instance of PDFPage that the Radio button field is on like all other field types.

How can we reproduce the issue?

Here's a codesandbox (Relevant code is in CombinePDF.tsx file): https://codesandbox.io/p/github/human-tools/toolbox-web/draft/elegant-maxwell?create=true&file=%2Fsrc%2FCombinePDF%2FCombinePDF.tsx&selection=%5B%7B%22endColumn%22%3A49%2C%22endLineNumber%22%3A7%2C%22startColumn%22%3A49%2C%22startLineNumber%22%3A7%7D%5D

In the open CombinePDF.tsx you'll see a upload field and once you upload a PDF with a form and radio buttons, you'll see in the console a print of all fields and the page found through the doc.findPageForAnnotationRef API. Notice that both the radio and pushbutton fields have undefined for their pages while the rest get the correct page.

Attached PDF samples with a form and radio buttons. form_sample_pdf.pdf Purchase_Order_Form.pdf lobbyguardform_Sample.pdf

Version

^1.17.1

What environment are you running pdf-lib in?

Browser

Checklist

Additional Notes

No response

jean343 commented 3 months ago

I have the same problem, in my case, I could find the page with pages.find((x) => x.ref === widget.P()). I ended up doing

const page = doc.findPageForAnnotationRef(field.ref) ?? pages.find((x) => x.ref === widget.P());