mardi 2 juin 2020

In iText 7 why checkbox is being checked if wrong value is set and CheckType changes from CHECK to CROSS

I am using iText 7 java library. Here are my gradle dependencies.

compile group: 'com.itextpdf', name: 'kernel', version: '7.1.9'
compile group: 'com.itextpdf', name: 'layout', version: '7.1.9'
compile group: 'com.itextpdf', name: 'forms', version: '7.1.9'

I need to fill Acro form in an existing pdf template. PDF form has a checkbox defined with following settings -

Name = COMM_PREF_EMAIL

Check Box Style = Check

Export Value = Yes

enter image description here

If I open the pdf in acrobat reader and click the checbox, it shows checked with a check mark as expected.

enter image description here

With iText library, my expectation is that if I set checkbox value to Yes (matching to export value of the checkbox) then only checkbox should be checked. If I set any other value, checkbox should remain unchecked.

Here is my java code using iText 7 library.

PdfAcroForm pdfAcroForm = PdfAcroForm.getAcroForm(pdfDocument, true);
PdfFormField pdfFormField = pdfAcroForm.getField("COMM_PREF_EMAIL");
pdfFormField.setValue("B");

I am setting checkbox value as "B" which does not match export value "Yes" but still checkbox is being checked in the PDF and worst part is Check Box Style also changed to Cross. Below is how the checkbox looks after running this code.

enter image description here

Note that Check Box Style always changes to Cross irrespective of whatever value I set.

I did some debugging and noticed that when PdfFormField is retrieved using getField, it has chekType=0, which is not correct. Its not what is defined in the pdf. It should be 1 which is Check. Now when I call pdfFormField.setValue(...), it eventually calls regenerateField() which I guess taking default value of chekType=3 (the cross) because it has invalid value.

enter image description here

Can someone help with following 2 issues -

  1. Why checkbox is being checked if value being set does not match to export value?
  2. Why setValue changing Check Box Style to Cross?

Any suggestion or advice is much appreciated.




Aucun commentaire:

Enregistrer un commentaire