get "Export Value" out of checkbox in pdf form using 'pdf-lib'

56 views Asked by At

I need to get the properties value of "Export Value" out of checkbox fields in PDF file, I using 'pdf-lib', and according to the code in the repo I should get it by using:

const pdfDoc = await PDFDocument.load(pdfBytes);
const form = pdfDoc.getForm();
const fields = form.getFields();
const field = field[0];
const exportValue = field.acroField.getExportValues()

But it always return undefined...

1

There are 1 answers

0
K J On

It is possible to get the value of a checkBox but pdf-lib simply treats them as true of false (checked or unchecked). To go deeper into the underlying values will need a much more complex method of PDF query.

"the export value is the value of the checkbox when it is checked. This is the same value returned by this.getField("checkbox").value"

Where "checkbox" is this. doc known field /Tagged "name" and /V is its binary value (true/false, on/off, yes/no or ja/nein)

So here we can use one of the examples to test and even alter another field based on the checkbox status. https://jsfiddle.net/f8L4u95e/2/

     // Query the form's fields
      
const checkit3 = form.getCheckBox('Check Box3');
if (checkit3.isChecked()) form.getTextField('Text1').setText('check box 3 is selected');

const checkit4 = form.getCheckBox('Check Box4');
if (checkit4.isChecked()) form.getTextField('Text1').setText('check box 4 is selected');

With

      form.getCheckBox('Check Box3').uncheck();
      form.getCheckBox('Check Box4').check();

enter image description here

similar with those reversed

      form.getCheckBox('Check Box3').check();
      form.getCheckBox('Check Box4').uncheck();

for wider discussions see Do checkbox inputs only post data if they're checked?

enter image description here