Search in sources :

Example 26 with COSString

use of com.tom_roush.pdfbox.cos.COSString in project PdfBox-Android by TomRoush.

the class PDButton method getExportValues.

/**
 * This will get the (optional) export values.
 *
 * <p>The export values are defined in the field dictionaries /Opt key.</p>
 *
 * <p>The option values are used to define the export values
 * for the field to
 * <ul>
 *  <li>hold values in non-Latin writing systems as name objects, which represent the field value, are limited
 *      to PDFDocEncoding
 *  </li>
 *  <li>allow radio buttons having the same export value to be handled independently
 *  </li>
 * </ul>
 *
 * @return List containing all possible export values. If there is no /Opt entry an empty list will be returned.
 *
 * @see #getOnValues()
 */
public List<String> getExportValues() {
    COSBase value = getInheritableAttribute(COSName.OPT);
    if (value instanceof COSString) {
        List<String> array = new ArrayList<String>();
        array.add(((COSString) value).getString());
        return array;
    } else if (value instanceof COSArray) {
        return COSArrayList.convertCOSStringCOSArrayToList((COSArray) value);
    }
    return Collections.emptyList();
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) ArrayList(java.util.ArrayList) COSArrayList(com.tom_roush.pdfbox.pdmodel.common.COSArrayList) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 27 with COSString

use of com.tom_roush.pdfbox.cos.COSString 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)

Example 28 with COSString

use of com.tom_roush.pdfbox.cos.COSString in project PdfBox-Android by TomRoush.

the class FieldUtils method getPairableItems.

/**
 * Return either one of a list which can have two-element arrays entries.
 * <p>
 * Some entries in a dictionary can either be an array of elements
 * or an array of two-element arrays. This method will either return
 * the elements in the array or in case of two-element arrays, the element
 * designated by the pair index
 * </p>
 * <p>
 * An {@link IllegalArgumentException} will be thrown if the items contain
 * two-element arrays and the index is not 0 or 1.
 * </p>
 *
 * @param items the array of elements or two-element arrays
 * @param pairIdx the index into the two-element array
 * @return a List of single elements
 */
static List<String> getPairableItems(COSBase items, int pairIdx) {
    if (pairIdx < 0 || pairIdx > 1) {
        throw new IllegalArgumentException("Only 0 and 1 are allowed as an index into two-element arrays");
    }
    if (items instanceof COSString) {
        List<String> array = new ArrayList<String>();
        array.add(((COSString) items).getString());
        return array;
    } else if (items instanceof COSArray) {
        List<String> entryList = new ArrayList<String>();
        for (COSBase entry : (COSArray) items) {
            if (entry instanceof COSString) {
                entryList.add(((COSString) entry).getString());
            } else if (entry instanceof COSArray) {
                COSArray cosArray = (COSArray) entry;
                if (cosArray.size() >= pairIdx + 1 && cosArray.get(pairIdx) instanceof COSString) {
                    entryList.add(((COSString) cosArray.get(pairIdx)).getString());
                }
            }
        }
        return entryList;
    }
    return Collections.emptyList();
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) ArrayList(java.util.ArrayList) COSBase(com.tom_roush.pdfbox.cos.COSBase) List(java.util.List) ArrayList(java.util.ArrayList) COSString(com.tom_roush.pdfbox.cos.COSString) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 29 with COSString

use of com.tom_roush.pdfbox.cos.COSString in project PdfBox-Android by TomRoush.

the class PDVariableText method getDefaultAppearanceString.

/**
 * Get the default appearance.
 *
 * This is an inheritable attribute.
 *
 * The default appearance contains a set of default graphics and text operators
 * to define the field’s text size and color.
 *
 * @return the DA element of the dictionary object
 */
PDDefaultAppearanceString getDefaultAppearanceString() throws IOException {
    COSString da = (COSString) getInheritableAttribute(COSName.DA);
    PDResources dr = getAcroForm().getDefaultResources();
    return new PDDefaultAppearanceString(da, dr);
}
Also used : PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 30 with COSString

use of com.tom_roush.pdfbox.cos.COSString in project PdfBox-Android by TomRoush.

the class PDField method importFDF.

/**
 * This will import a fdf field from a fdf document.
 *
 * @param fdfField The fdf field to import.
 * @throws IOException If there is an error importing the data for this field.
 */
void importFDF(FDFField fdfField) throws IOException {
    COSBase fieldValue = fdfField.getCOSValue();
    if (fieldValue != null && this instanceof PDTerminalField) {
        PDTerminalField currentField = (PDTerminalField) this;
        if (fieldValue instanceof COSName) {
            currentField.setValue(((COSName) fieldValue).getName());
        } else if (fieldValue instanceof COSString) {
            currentField.setValue(((COSString) fieldValue).getString());
        } else if (fieldValue instanceof COSStream) {
            currentField.setValue(((COSStream) fieldValue).toTextString());
        } else if (fieldValue instanceof COSArray && this instanceof PDChoice) {
            ((PDChoice) this).setValue(COSArrayList.convertCOSStringCOSArrayToList((COSArray) fieldValue));
        } else {
            throw new IOException("Error:Unknown type for field import" + fieldValue);
        }
    } else if (fieldValue != null) {
        dictionary.setItem(COSName.V, fieldValue);
    }
    Integer ff = fdfField.getFieldFlags();
    if (ff != null) {
        setFieldFlags(ff);
    } else {
        // these are suppose to be ignored if the Ff is set.
        Integer setFf = fdfField.getSetFieldFlags();
        int fieldFlags = getFieldFlags();
        if (setFf != null) {
            int setFfInt = setFf;
            fieldFlags = fieldFlags | setFfInt;
            setFieldFlags(fieldFlags);
        }
        Integer clrFf = fdfField.getClearFieldFlags();
        if (clrFf != null) {
            // we have to clear the bits of the document fields for every bit that is
            // set in this field.
            // 
            // Example:
            // docFf = 1011
            // clrFf = 1101
            // clrFfValue = 0010;
            // newValue = 1011 & 0010 which is 0010
            int clrFfValue = clrFf;
            clrFfValue ^= 0xFFFFFFFF;
            fieldFlags = fieldFlags & clrFfValue;
            setFieldFlags(fieldFlags);
        }
    }
}
Also used : COSStream(com.tom_roush.pdfbox.cos.COSStream) COSName(com.tom_roush.pdfbox.cos.COSName) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException) COSString(com.tom_roush.pdfbox.cos.COSString)

Aggregations

COSString (com.tom_roush.pdfbox.cos.COSString)65 COSArray (com.tom_roush.pdfbox.cos.COSArray)33 COSBase (com.tom_roush.pdfbox.cos.COSBase)24 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)5 COSName (com.tom_roush.pdfbox.cos.COSName)5 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)4 Map (java.util.Map)4 COSBoolean (com.tom_roush.pdfbox.cos.COSBoolean)2 COSFloat (com.tom_roush.pdfbox.cos.COSFloat)2 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)2 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)2 MessageDigest (java.security.MessageDigest)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)1