jeudi 28 juin 2018

How to see if radio button or checkbox is checked using PDFBox in Java?

I am trying to create a program that reads a PDF Form (questions and answers) and, for now, simply outputs everything back to the screen. My problem is, when I use getValueAsString on radio buttons or check boxes, it always returns an empty string, no matter if it is checked or not. Is there another method I should be using? Here is my code:

public static void listFields(PDDocument doc) throws Exception
{
    PDDocumentCatalog catalog = doc.getDocumentCatalog();
    PDAcroForm form = catalog.getAcroForm();
    List<PDField> fields = form.getFields();

    for(PDField field: fields)
    {
        String name = field.getFullyQualifiedName();
        if (field instanceof PDTextField || field instanceof PDComboBox)
        {
             Object value = field.getValueAsString();
             System.out.print(name);
             System.out.print(" = ");
             System.out.print(value);
             System.out.println();
        }
        else if (field instanceof PDPushButton)
            ;
        else
        {
            if (field instanceof PDRadioButton)
            {
                List<String> exportValues = ((PDRadioButton) field).getSelectedExportValues();
                for (String string : exportValues)
                {
                    name = field.getFullyQualifiedName();
                    System.out.print(name);
                    System.out.print(" = ");
                    System.out.print(string);
                    System.out.println();
                }
            }
            else if (field instanceof PDCheckBox)
            {
                PDButton box = (PDButton)field;
                String value = box.getValue();
                System.out.print(name);
                System.out.print(" = ");
                System.out.print(value);
                System.out.println();
            }

        }
    }
}

public static void main(String[] args) throws Exception {
    File file = new File("C:\\Users\\bobdu\\Documents\\SHIP_CCF_LastName_FirstName_YYYYMMDD_v1_Sample.pdf");
    PDDocument doc = PDDocument.load(file);
    listFields(doc);
}

for(PDField field: fields)
    {
        String name = field.getFullyQualifiedName();
        if (field instanceof PDTextField || field instanceof PDComboBox)
        {
             Object value = field.getValueAsString();
             System.out.print(name);
             System.out.print(" = ");
             System.out.print(value);
             System.out.println();
        }
        else if (field instanceof PDPushButton)
            ;
        else
        {
            if (field instanceof PDRadioButton)
            {
                List<String> exportValues = ((PDRadioButton) field).getSelectedExportValues();
                for (String string : exportValues)
                {
                    name = field.getFullyQualifiedName();
                    System.out.print(name);
                    System.out.print(" = ");
                    System.out.print(string);
                    System.out.println();
                }
            }
            else if (field instanceof PDCheckBox)
            {
                PDButton box = (PDButton)field;
                String value = box.getValue();
                System.out.print(name);
                System.out.print(" = ");
                System.out.print(value);
                System.out.println();
            }

        }
    }
}

public static void main(String[] args) throws Exception {
    File file = new File("C:\\Users\\bobdu\\Documents\\SHIP_CCF_LastName_FirstName_YYYYMMDD_v1_Sample.pdf");
    PDDocument doc = PDDocument.load(file);
    listFields(doc);
}

Thank you in advance for helping me.




Aucun commentaire:

Enregistrer un commentaire