Example usage for com.lowagie.text.pdf AcroFields getAppearanceStates

List of usage examples for com.lowagie.text.pdf AcroFields getAppearanceStates

Introduction

In this page you can find the example usage for com.lowagie.text.pdf AcroFields getAppearanceStates.

Prototype

public String[] getAppearanceStates(String fieldName) 

Source Link

Document

Gets the list of appearance names.

Usage

From source file:oscar.form.pharmaForms.formBPMH.pdf.PDFController.java

License:Open Source License

/**
 * Read the smart tags off of a pdf document and use them to 
 * extract the property values from a POJO Object.
 * /*from  w w w .j av  a 2s.  c o  m*/
 * Assuming that the pdf input path has been preset.
 * 
 * @param data : data object that contains data.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void addDataToPDF() {

    AcroFields acroFields = getStamper().getAcroFields();
    Map acroFieldsMap = acroFields.getFields();
    Iterator<String> acroFieldsIt = acroFieldsMap.keySet().iterator();
    String replaceWith = "";
    String key = "";
    String cleanKey = "";
    int fieldType;
    String[] appStates;
    AcroFields.Item acroField;
    PdfDictionary annots;
    Iterator itannots;

    while (acroFieldsIt.hasNext()) {

        key = acroFieldsIt.next().toString();
        cleanKey = key.replaceAll(STRING_FILTER, "");
        fieldType = acroFields.getFieldType(key);
        appStates = acroFields.getAppearanceStates(key);
        acroField = (Item) acroFieldsMap.get(key);
        annots = acroField.getWidget(0);

        _Logger.debug("Field Type: " + cleanKey + " = " + fieldType);

        itannots = annots.getKeys().iterator();
        while (itannots.hasNext()) {
            PdfName annotKey = (PdfName) itannots.next();
            _Logger.debug("ANNOT KEY: " + annotKey);
            _Logger.debug("ANNOT VALUE: " + annots.get(annotKey));
        }

        if (appStates.length > 0) {
            for (int i = 0; appStates.length > i; i++) {
                _Logger.debug("APPEARANCE STATE: " + appStates[i]);
            }
        }

        replaceWith = invokeValue(cleanKey);

        //count the characters and compare to limit.
        //         if(fieldType == AcroFields.FIELD_TYPE_TEXT) {
        //            replaceWith = contentSplicer(replaceWith, 30);
        //         }

        try {

            _Logger.debug("Replacement Key and Value: Key = " + cleanKey + " Value = " + replaceWith);

            acroFields.setField(cleanKey, replaceWith);

        } catch (IOException e) {
            _Logger.log(Level.FATAL,
                    "Failed to set method " + cleanKey + " with value " + replaceWith + " into PDF document",
                    e);
        } catch (DocumentException e) {
            _Logger.log(Level.FATAL,
                    "Failed to set method " + cleanKey + " with value " + replaceWith + " into PDF document",
                    e);
        }

    }

}