如何使用iText从pdf中提取TextField?

时间:2015-03-12 09:24:38

标签: java itext textfield

我需要使用iText从PDF中提取TextField。

PdfReader reader = new PdfReader(pdf.getAbsolutePath());

AcroFields form = reader.getAcroFields();
Map<String, Item> fields = form.getFields();

for(Entry<String, Item> entry : fields.entrySet()){
    String field_name = entry.getKey();

    int field_type = form.getFieldType(field_name);
    List<FieldPosition> fieldPosition = form.getFieldPositions(field_name);

    Rectangle position = fieldPosition.get(0).position;

    float left   = position.getLeft();
    float top    = position.getTop();
    float width  = position.getWidth();
    float height = position.getHeight();

    int load_page = fieldPosition.get(0).page;


    /**
    In this point I need to extract maxlength and options of filed.
    This information are stored in a TextField object and i can read with the methods TextField.getMaxCharacterLength() and TextField.getOptions().
    **/
}

reader.close();

Text Field对象包含我需要的信息,即maxlength和使用TextField.getMaxCharacterLength()和TextField.getOptions()方法的选项。

还有另一种提取这些信息的解决方案吗?

1 个答案:

答案 0 :(得分:0)

使用PdfDictionary解决

Item field = entry.getValue();

PdfDictionary dict = field.getMerged(0);

int maxLength = dict.getAsNumber(PdfName.MAXLEN).intValue();
int ff = dict.getAsNumber(PdfName.FF).intValue();

ff var包含一个表示te类型字段的数字

有代码检查字段是否梳理

if((ff & BaseField.COMB) > 0)