Search in sources :

Example 1 with KeyValue

use of com.tom_roush.pdfbox.pdmodel.interactive.form.FieldUtils.KeyValue in project PdfBox-Android by TomRoush.

the class PDChoice method setOptions.

/**
 * This will set the display and export values - the 'Opt' key.
 *
 * <p>
 * This will set both, the export value and the display value
 * of the choice field. If either one of the parameters is null or an
 * empty list is supplied the options will
 * be removed.
 * </p>
 * <p>
 * An {@link IllegalArgumentException} will be thrown if the
 * number of items in the list differ.
 * </p>
 *
 * @see #setOptions(List)
 * @param exportValues List containing all possible export values.
 * @param displayValues List containing all possible display values.
 */
public void setOptions(List<String> exportValues, List<String> displayValues) {
    if (exportValues != null && displayValues != null && !exportValues.isEmpty() && !displayValues.isEmpty()) {
        if (exportValues.size() != displayValues.size()) {
            throw new IllegalArgumentException("The number of entries for exportValue and displayValue shall be the same.");
        } else {
            List<KeyValue> keyValuePairs = FieldUtils.toKeyValueList(exportValues, displayValues);
            if (isSort()) {
                FieldUtils.sortByValue(keyValuePairs);
            }
            COSArray options = new COSArray();
            for (int i = 0; i < exportValues.size(); i++) {
                COSArray entry = new COSArray();
                entry.add(new COSString(keyValuePairs.get(i).getKey()));
                entry.add(new COSString(keyValuePairs.get(i).getValue()));
                options.add(entry);
            }
            getCOSObject().setItem(COSName.OPT, options);
        }
    } else {
        getCOSObject().removeItem(COSName.OPT);
    }
}
Also used : KeyValue(com.tom_roush.pdfbox.pdmodel.interactive.form.FieldUtils.KeyValue) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSString(com.tom_roush.pdfbox.cos.COSString)

Aggregations

COSArray (com.tom_roush.pdfbox.cos.COSArray)1 COSString (com.tom_roush.pdfbox.cos.COSString)1 KeyValue (com.tom_roush.pdfbox.pdmodel.interactive.form.FieldUtils.KeyValue)1