简体   繁体   中英

How to find the index position of a radio button selected in a PDF Acrofield using iText Java

PdfReader reader = new PdfReader(pdfbytes);
AcroFields acroFields = reader.getAcroFields();
Set <String> fields = acroFields.getFields().keySet();
for (String field:fields) {
    if (AcroFields.FIELD_TYPE_RADIOBUTTON == acroFields.getFieldType(field)) {
        String fieldId = acroFields.getField(field);
        List<AcroFields.FieldPosition> positions = acroFields.getFieldPostions(field);
        System.out.println("List size : " + positions.size());
        for (int i = 0; i < positions.size(); i++) {
            Rectangle rect = acroFields.getFieldPositions(field).get(i).position;
        }
    }
}

I am able to find and iterate the list of position under a radio group.

I would like to know the index of the radio button that is selected in order to find its absolute position.

The the following code should provide you with directions. Please note that it might be possible that no radiobutton is checked. Also you have to do the error handling etc...

for (int i = 0; i < positions.size(); i++) {

    Item fieldItem = acroFields.getFieldItem(field);
    PdfDictionary dict = entry.getValue().getWidget(i);
    PdfName as = dict.getAsName(PdfName.AS);
    if(as==null){
      //handle/throw error if AS key is not set and there are one or more  subdictionaries!
    }
    String asValue = PdfName.decodeName(as.toString());
    if("Off".equalsIgnoreCase(asValue)){
       System.out.println("not checked");
    } 
    else{
       System.out.println("Checked");
       //do what you need to do
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM