Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

like image 804
Rakesh Prajapati Avatar asked Oct 16 '25 16:10

Rakesh Prajapati


1 Answers

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.

like image 156
Pablo Roldan Avatar answered Oct 18 '25 06:10

Pablo Roldan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!