[英]Java: How to extract text by a selected area from a PDF file using iText?
[英]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;
}
}
}
我能夠在無線電組下找到並迭代 position 的列表。
我想知道為了找到其絕對值 position 而選擇的單選按鈕的索引。
以下代碼應為您提供指導。 請注意,可能沒有選中單選按鈕。 您還必須進行錯誤處理等...
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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.