Search in sources :

Example 91 with COSDictionary

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

the class PDOptionalContentGroup method getRenderState.

// TODO Add support for "Intent"
/**
 * @param destination to be rendered
 * @return state or null if undefined
 */
public RenderState getRenderState(RenderDestination destination) {
    COSName state = null;
    COSDictionary usage = (COSDictionary) dict.getDictionaryObject("Usage");
    if (usage != null) {
        if (RenderDestination.PRINT.equals(destination)) {
            COSDictionary print = (COSDictionary) usage.getDictionaryObject("Print");
            state = print == null ? null : (COSName) print.getDictionaryObject("PrintState");
        } else if (RenderDestination.VIEW.equals(destination)) {
            COSDictionary view = (COSDictionary) usage.getDictionaryObject("View");
            state = view == null ? null : (COSName) view.getDictionaryObject("ViewState");
        }
        // Fallback to export
        if (state == null) {
            COSDictionary export = (COSDictionary) usage.getDictionaryObject("Export");
            state = export == null ? null : (COSName) export.getDictionaryObject("ExportState");
        }
    }
    return state == null ? null : RenderState.valueOf(state);
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary)

Example 92 with COSDictionary

use of com.tom_roush.pdfbox.cos.COSDictionary 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 93 with COSDictionary

use of com.tom_roush.pdfbox.cos.COSDictionary 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 94 with COSDictionary

use of com.tom_roush.pdfbox.cos.COSDictionary 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 95 with COSDictionary

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

the class PDOptionalContentProperties method setBaseState.

/**
 * Sets the base state for optional content groups.
 * @param state the base state
 */
public void setBaseState(BaseState state) {
    COSDictionary d = getD();
    d.setItem(COSName.BASE_STATE, state.getName());
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary)

Aggregations

COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)221 COSArray (com.tom_roush.pdfbox.cos.COSArray)68 COSBase (com.tom_roush.pdfbox.cos.COSBase)68 IOException (java.io.IOException)25 ArrayList (java.util.ArrayList)24 COSName (com.tom_roush.pdfbox.cos.COSName)23 COSObject (com.tom_roush.pdfbox.cos.COSObject)22 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)15 HashMap (java.util.HashMap)14 COSStream (com.tom_roush.pdfbox.cos.COSStream)13 Map (java.util.Map)12 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)10 COSString (com.tom_roush.pdfbox.cos.COSString)7 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)6 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 List (java.util.List)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 Test (org.junit.Test)5