【问题标题】:How to find the index position of a radio button selected in a PDF Acrofield using iText Java如何使用 iText Java 查找在 PDF Acrofield 中选择的单选按钮的索引位置
【发布时间】:2020-10-12 09:50:58
【问题描述】:
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;
        }
    }
}

我能够找到并迭代无线电组下的位置列表。

我想知道被选中的单选按钮的索引,以便找到它的绝对位置。

【问题讨论】:

  • “我想知道选中的单选按钮的索引” - 你指的是哪个索引? “选定”是什么意思。

标签: java pdf radio-button itext acrofields


【解决方案1】:

以下代码应为您提供指导。请注意,可能没有选中单选按钮。您还必须进行错误处理等...

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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多