9

I am filling the data for a fillable pdf using iTextsharp. There are n number of checkboxes in the pdf form. I have set the value for the check boxes using "Yes" or "No". This works fine. But some of the check boxes does not work in this way; instead i need to use 1 or 0 to make it work. So can anyone help me what is the common way to check/uncheck the checkboxes in pdf using iTextSharp?

Thanks in Advance,

Snowwhite

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
snowwhite
  • 91
  • 1
  • 1
  • 2

7 Answers7

18

Open chosen PDF and convert it.

PdfReader reader = new PdfReader(fileNameIn);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(fileNameOut));
AcroFields form = stamper.getAcroFields();

Inspect the form objects Fields > Keys > Results view to find the string value of a Check Box, in my case is was "Check Box1"

String[] checkboxstates = form.GetAppearanceStates("Check Box1");

Inspect the checkboxstates variable. [0] = value of non checked, [1] = value of checked. Then to make it checked

fields.SetField("Check Box1", checkboxstates[1])
jball
  • 24,791
  • 9
  • 70
  • 92
Meir Snyder
  • 789
  • 1
  • 6
  • 16
  • I had to change `stamper.getAcroFields();` to `stamper.AcroFields();` but this works great. – rg89 Sep 28 '17 at 14:32
  • 1
    This should be the accepted answer since the fields can have multiple states and anything greater than the default state (index 0) will be checked. – clockwiseq Oct 10 '17 at 15:04
  • 1
    Hmm, on my PDF there was only one checkbox state, setting the field value to an empty string cleared it. The one state that `GetAppearenceStates` returned was what I needed, though. – John Feb 26 '20 at 21:59
  • This was helpful, but I have one small quibble: The last line of the answer should start with "form.SetField" instead of "fields.SetField" based on the name of the variable created earlier in the answer. – John Gilmer Apr 16 '20 at 09:36
  • This answer is not accurate, because the order in the AppearanceStates maybe inverted, one should check for the PdfName.OFF value. – luiso1979 Oct 18 '22 at 07:52
7

you can find in this way:

PdfReader reader = new PdfReader(fileNameIn);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(fileNameOut));
AcroFields form = stamper.getAcroFields();

form.setField("Name","Test Name");
form.setField("odot","123456");
form.setField("Consortium","A Testing Co");
form.setField("PName","My Name");
form.setField("date","10/14/03");
form.setField("Box1","true"); //This is the checkbox control
stamper.close();

hope this help

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
Suresh Chaudhary
  • 1,609
  • 5
  • 25
  • 40
3

pdfFormFields.SetField("formfieldchkbox", "Yes"); pdfFormFields.SetField("formfieldchkbox", "No");

That should do the job.

S Lal
  • 71
  • 3
3

There is no "common way". You need to know the check/uncheck values in order to change them.

There's a similar question I answered where I showed how to find out those values... Ah!

Get the export value of a checkbox using iTextSharp

Community
  • 1
  • 1
Mark Storer
  • 15,672
  • 3
  • 42
  • 80
1

I have done like this

stamp.AcroFields.SetField("chk1", "Yes");
stamp.AcroFields.SetField("chk2", "No");
Ashi
  • 409
  • 3
  • 12
  • This only works under special circumstances. In general one needs to determine the state names like in [Meir Snyder's answer](https://stackoverflow.com/a/38107530/1729265). – mkl Oct 13 '18 at 17:23
0

I find that I can set export value of check box control in PDF file using Adobe Acrobat, When the export value is set as "anytext" I can check the checkbox using following code:

form.setField("Box1","anytext")

Property window of Check box

0

In this export value is very important.

String pathin = @"D:\Research And Development\ITextSharpPdf\Data\\ACR_Form_Ver_11.pdf";
String pathout = @"D:\Research And Development\ITextSharpPdf\Data\ACR_Form_Ver_11_out.pdf";

        PdfReader reader = new PdfReader(pathin);// formFile);
        using (PdfStamper stamper = new PdfStamper(reader, new FileStream(pathout, FileMode.Create)))
        {

            var formFields = stamper.AcroFields;
            //formFields.SetField(<fieldname>, <exportvalue>,<true/false>);
            formFields.SetField("pdfQ1No", "No",true);
            formFields.SetField("pdfAppNo", "234234");
            stamper.FormFlattening = true;
            stamper.Close();
        }

pdfescapteeditimage

outputimage

Swapnil Malap
  • 610
  • 1
  • 7
  • 14