4

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.

Update on 6/10/2020
Link of pdf file to reproduce the issue https://github.com/rakeshprajapati1982/itext-7-issue/blob/master/Checklist.pdf

Rakesh Prajapati
  • 1,078
  • 8
  • 17
  • Hi, `7.1.11` is already out, please try with the latest version and if it does not work please provide your PDF so that the problem can be reproduced – Alexey Subach Jun 10 '20 at 22:14
  • I tested with iText `7.1.11` version as well and I see the same issue. I updated question with link of pdf in the end. You can use this pdf reproduce the issue. Checkex are towards end of the page in the pdf with label "Comm Preference" Also note that original pdf size is 193KB but the filled pdf generated by iText is 152KB which is less than the original. I have seen more significant size drop in some other PDF files. Do you know reason of the size drop? – Rakesh Prajapati Jun 11 '20 at 03:24

1 Answers1

1

As itextSharp with the method SetField() seems automatically gets the aparence of the form

PdfStamper pdfStamper = new PdfStamper(pdfReader_, fileStream);
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField(FIELD_NAME, "123",true);

but now in itext7 in my research seems like it's not implicit the widget shape "dove, cross, start, etc...", just it gets the values lets call'em states ("Yes, Off"), so just set the widget to type with SetCheckType(NUMBER) as mention in Why does iText enter a cross symbol when CheckType

so in your code it'll be like

            var field = form1.GetField(FIELD_NAME);
            if (field.GetType() == typeof(PdfButtonFormField)) {
                field.SetCheckType(PdfFormField.TYPE_CHECK);//PdfFormField.TYPE_CIRCLE,PdfFormField.TYPE_CROSS,PdfFormField.TYPE_DIAMOND,PdfFormField.TYPE_SQUARE,PdfFormField.TYPE_STAR,etc
                field.SetValue("Yes", true);
            }

By the way if I'm aswering in a bad way this question please be free to edit it don't delete it.