Search in sources :

Example 71 with COSBase

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

the class PDStandardAttributeObject method getNumberOrArrayOfNumber.

/**
 * Gets a number or an array of numbers.
 *
 * @param name the attribute name
 * @param defaultValue the default value
 * @return a Float or an array of floats
 */
protected Object getNumberOrArrayOfNumber(String name, float defaultValue) {
    COSBase v = this.getCOSObject().getDictionaryObject(name);
    if (v instanceof COSArray) {
        COSArray array = (COSArray) v;
        float[] values = new float[array.size()];
        for (int i = 0; i < array.size(); i++) {
            COSBase item = array.getObject(i);
            if (item instanceof COSNumber) {
                values[i] = ((COSNumber) item).floatValue();
            }
        }
        return values;
    }
    if (v instanceof COSNumber) {
        return ((COSNumber) v).floatValue();
    }
    if (defaultValue == UNSPECIFIED) {
        return null;
    }
    return defaultValue;
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 72 with COSBase

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

the class PDOptionalContentProperties method isGroupEnabled.

/**
 * Indicates whether an optional content group is enabled.
 * @param group the group object
 * @return true if the group is enabled
 */
public boolean isGroupEnabled(PDOptionalContentGroup group) {
    // TODO handle Optional Content Configuration Dictionaries,
    // i.e. OCProperties/Configs
    PDOptionalContentProperties.BaseState baseState = getBaseState();
    boolean enabled = !baseState.equals(BaseState.OFF);
    if (group == null) {
        return enabled;
    }
    COSDictionary d = getD();
    COSBase base = d.getDictionaryObject(COSName.ON);
    if (base instanceof COSArray) {
        for (COSBase o : (COSArray) base) {
            COSDictionary dictionary = toDictionary(o);
            if (dictionary == group.getCOSObject()) {
                return true;
            }
        }
    }
    base = d.getDictionaryObject(COSName.OFF);
    if (base instanceof COSArray) {
        for (COSBase o : (COSArray) base) {
            COSDictionary dictionary = toDictionary(o);
            if (dictionary == group.getCOSObject()) {
                return false;
            }
        }
    }
    return enabled;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 73 with COSBase

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

the class PDOptionalContentProperties method setGroupEnabled.

/**
 * Enables or disables an optional content group.
 * @param group the group object
 * @param enable true to enable, false to disable
 * @return true if the group already had an on or off setting, false otherwise
 */
public boolean setGroupEnabled(PDOptionalContentGroup group, boolean enable) {
    COSArray on;
    COSArray off;
    COSDictionary d = getD();
    COSBase base = d.getDictionaryObject(COSName.ON);
    if (!(base instanceof COSArray)) {
        on = new COSArray();
        d.setItem(COSName.ON, on);
    } else {
        on = (COSArray) base;
    }
    base = d.getDictionaryObject(COSName.OFF);
    if (!(base instanceof COSArray)) {
        off = new COSArray();
        d.setItem(COSName.OFF, off);
    } else {
        off = (COSArray) base;
    }
    boolean found = false;
    if (enable) {
        for (COSBase o : off) {
            COSDictionary groupDictionary = toDictionary(o);
            if (groupDictionary == group.getCOSObject()) {
                // enable group
                off.remove(o);
                on.add(o);
                found = true;
                break;
            }
        }
    } else {
        for (COSBase o : on) {
            COSDictionary groupDictionary = toDictionary(o);
            if (groupDictionary == group.getCOSObject()) {
                // disable group
                on.remove(o);
                off.add(o);
                found = true;
                break;
            }
        }
    }
    if (!found) {
        if (enable) {
            on.add(group.getCOSObject());
        } else {
            off.add(group.getCOSObject());
        }
    }
    return found;
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 74 with COSBase

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

the class PDOptionalContentProperties method isGroupEnabled.

/**
 * Indicates whether <em>at least one</em> optional content group with this name is enabled.
 * There may be disabled optional content groups with this name even if this function returns
 * true.
 *
 * @param groupName the group name
 * @return true if at least one group is enabled
 */
public boolean isGroupEnabled(String groupName) {
    boolean result = false;
    COSArray ocgs = getOCGs();
    for (COSBase o : ocgs) {
        COSDictionary ocg = toDictionary(o);
        String name = ocg.getString(COSName.NAME);
        if (groupName.equals(name) && isGroupEnabled(new PDOptionalContentGroup(ocg))) {
            result = true;
        }
    }
    return result;
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 75 with COSBase

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

the class PDOptionalContentProperties method setGroupEnabled.

/**
 * Enables or disables all optional content groups with the given name.
 *
 * @param groupName the group name
 * @param enable true to enable, false to disable
 * @return true if at least one group with this name already had an on or off setting, false
 * otherwise
 */
public boolean setGroupEnabled(String groupName, boolean enable) {
    boolean result = false;
    COSArray ocgs = getOCGs();
    for (COSBase o : ocgs) {
        COSDictionary ocg = toDictionary(o);
        String name = ocg.getString(COSName.NAME);
        if (groupName.equals(name) && setGroupEnabled(new PDOptionalContentGroup(ocg), enable)) {
            result = true;
        }
    }
    return result;
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Aggregations

COSBase (com.tom_roush.pdfbox.cos.COSBase)215 COSArray (com.tom_roush.pdfbox.cos.COSArray)108 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)68 COSName (com.tom_roush.pdfbox.cos.COSName)50 COSObject (com.tom_roush.pdfbox.cos.COSObject)42 IOException (java.io.IOException)34 COSString (com.tom_roush.pdfbox.cos.COSString)33 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)29 ArrayList (java.util.ArrayList)29 COSStream (com.tom_roush.pdfbox.cos.COSStream)20 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)14 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)11 HashMap (java.util.HashMap)11 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)10 Map (java.util.Map)10 List (java.util.List)9 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)8 COSFloat (com.tom_roush.pdfbox.cos.COSFloat)7 COSObjectKey (com.tom_roush.pdfbox.cos.COSObjectKey)7 PDFStreamParser (com.tom_roush.pdfbox.pdfparser.PDFStreamParser)6