2

I'm currently trying to take an existing PDF, find all existing check boxes and complete them based on some criteria using C#.

After reviewing other related questions: https://stackoverflow.com/a/4827996/6328714

The main issue I am having is finding all of the Checkbox objects within the PDF - which I believe I need to be able to reference the correct checkbox within my code.

As for tools to view the PDF's internal structure I am using PDFXplorer, but I'm not having much luck finding the actual check boxes within the tree-structure.

So:

  • Do I ~need~ the object to be able to check the box?
  • Is checking the boxes as simple as the posted code below? (It seems fairly straight forward if so)

Below example is taken from the linked question:

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();
Community
  • 1
  • 1
confusedandamused
  • 746
  • 2
  • 8
  • 28

1 Answers1

3

The first thing you want to do, is to find the fields that were defined for the form, and to discover which of those fields are check boxes.

Read this question to find out how this is done: iText doesn't set checkbox field

public class MainClass {
    public static void main(String[] args) {
        try {
            PdfReader reader = new PdfReader("pdf/fw9_template.pdf");
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("test.pdf"));
            AcroFields form = stamper.getAcroFields();

            String states[] = form.getAppearanceStates("topmostSubform[0].Page1[0].FederalClassification[0].c1_1");
            System.out.println(states);


            for (Iterator i = form.getFields().keySet().iterator(); i.hasNext(); ) {
                String key = (String) i.next();
                System.out.print(key + " : ");
                switch(form.getFieldType(key)) {
                    case AcroFields.FIELD_TYPE_CHECKBOX:
                        System.out.println("Checkbox");
                        break;
                    case AcroFields.FIELD_TYPE_COMBO:
                        System.out.println("Combobox");
                        break;
                    case AcroFields.FIELD_TYPE_LIST:
                        System.out.println("List");
                        break;
                    case AcroFields.FIELD_TYPE_NONE:
                        System.out.println("None");
                        break;
                    case AcroFields.FIELD_TYPE_PUSHBUTTON:
                        System.out.println("Pushbutton");
                        break;
                    case AcroFields.FIELD_TYPE_RADIOBUTTON:
                        System.out.println("Radiobutton");
                        break;
                    case AcroFields.FIELD_TYPE_SIGNATURE:
                        System.out.println("Signature");
                        break;
                    case AcroFields.FIELD_TYPE_TEXT:
                        System.out.println("Text");
                        break;
                    default:
                        System.out.println("?");
                }
            }
            form.setField("topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]", "true");

            stamper.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Then you want to check the check box, as is asked in the question Get field value for the check box

testForm.SetField("AmountCollect", "Off");
testForm.SetField("AmountCollect", "Yes");
testForm.SetField("AmountCollect", "0");

Different check boxes can have different values ("On", "Yes", "true", "1",...) for the on state (read How to determine “Checked” value for Checkboxes (from GetAppearanceStates)), so you have to use the getAppearances() method to know which values can be used.

All of this is, of course, explained in great detail on the official iText web site, which is the first place you should look when you are looking for an answer:

Browsing the official FAQ, you'll discover that iText also has a tool to inspect the objects inside a PDF. That tool is called RUPS.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165