Search in sources :

Example 1 with COSBoolean

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

the class BaseParser method parseBoolean.

/**
 * This will parse a boolean object from the stream.
 *
 * @return The parsed boolean object.
 *
 * @throws IOException If an IO error occurs during parsing.
 */
protected COSBoolean parseBoolean() throws IOException {
    COSBoolean retval;
    char c = (char) seqSource.peek();
    if (c == 't') {
        String trueString = new String(seqSource.readFully(4), ISO_8859_1);
        if (!trueString.equals(TRUE)) {
            throw new IOException("Error parsing boolean: expected='true' actual='" + trueString + "' at offset " + seqSource.getPosition());
        } else {
            retval = COSBoolean.TRUE;
        }
    } else if (c == 'f') {
        String falseString = new String(seqSource.readFully(5), ISO_8859_1);
        if (!falseString.equals(FALSE)) {
            throw new IOException("Error parsing boolean: expected='true' actual='" + falseString + "' at offset " + seqSource.getPosition());
        } else {
            retval = COSBoolean.FALSE;
        }
    } else {
        throw new IOException("Error parsing boolean expected='t or f' actual='" + c + "' at offset " + seqSource.getPosition());
    }
    return retval;
}
Also used : COSString(com.tom_roush.pdfbox.cos.COSString) IOException(java.io.IOException) COSBoolean(com.tom_roush.pdfbox.cos.COSBoolean)

Example 2 with COSBoolean

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

the class ContentStreamWriter method writeObject.

private void writeObject(Object o) throws IOException {
    if (o instanceof COSString) {
        COSWriter.writeString((COSString) o, output);
        output.write(SPACE);
    } else if (o instanceof COSFloat) {
        ((COSFloat) o).writePDF(output);
        output.write(SPACE);
    } else if (o instanceof COSInteger) {
        ((COSInteger) o).writePDF(output);
        output.write(SPACE);
    } else if (o instanceof COSBoolean) {
        ((COSBoolean) o).writePDF(output);
        output.write(SPACE);
    } else if (o instanceof COSName) {
        ((COSName) o).writePDF(output);
        output.write(SPACE);
    } else if (o instanceof COSArray) {
        COSArray array = (COSArray) o;
        output.write(COSWriter.ARRAY_OPEN);
        for (int i = 0; i < array.size(); i++) {
            writeObject(array.get(i));
            output.write(SPACE);
        }
        output.write(COSWriter.ARRAY_CLOSE);
    } else if (o instanceof COSDictionary) {
        COSDictionary obj = (COSDictionary) o;
        output.write(COSWriter.DICT_OPEN);
        for (Map.Entry<COSName, COSBase> entry : obj.entrySet()) {
            if (entry.getValue() != null) {
                writeObject(entry.getKey());
                output.write(SPACE);
                writeObject(entry.getValue());
                output.write(SPACE);
            }
        }
        output.write(COSWriter.DICT_CLOSE);
        output.write(SPACE);
    } else if (o instanceof Operator) {
        Operator op = (Operator) o;
        if (op.getName().equals(OperatorName.BEGIN_INLINE_IMAGE)) {
            output.write(OperatorName.BEGIN_INLINE_IMAGE.getBytes(Charsets.ISO_8859_1));
            COSDictionary dic = op.getImageParameters();
            for (COSName key : dic.keySet()) {
                Object value = dic.getDictionaryObject(key);
                key.writePDF(output);
                output.write(SPACE);
                writeObject(value);
                output.write(EOL);
            }
            output.write(OperatorName.BEGIN_INLINE_IMAGE_DATA.getBytes(Charsets.ISO_8859_1));
            output.write(EOL);
            output.write(op.getImageData());
            output.write(EOL);
            output.write(OperatorName.END_INLINE_IMAGE.getBytes(Charsets.ISO_8859_1));
            output.write(EOL);
        } else {
            output.write(op.getName().getBytes(Charsets.ISO_8859_1));
            output.write(EOL);
        }
    } else {
        throw new IOException("Error:Unknown type in content stream:" + o);
    }
}
Also used : Operator(com.tom_roush.pdfbox.contentstream.operator.Operator) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) IOException(java.io.IOException) COSBoolean(com.tom_roush.pdfbox.cos.COSBoolean) COSInteger(com.tom_roush.pdfbox.cos.COSInteger) COSName(com.tom_roush.pdfbox.cos.COSName) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString) Map(java.util.Map) COSFloat(com.tom_roush.pdfbox.cos.COSFloat)

Example 3 with COSBoolean

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

the class COSDictionaryMap method convertBasicTypesToMap.

/**
 * This will take a COS dictionary and convert it into COSDictionaryMap.  All cos
 * objects will be converted to their primitive form.
 *
 * @param map The COS mappings.
 * @return A standard java map.
 * @throws IOException If there is an error during the conversion.
 */
public static COSDictionaryMap<String, Object> convertBasicTypesToMap(COSDictionary map) throws IOException {
    COSDictionaryMap<String, Object> retval = null;
    if (map != null) {
        Map<String, Object> actualMap = new HashMap<String, Object>();
        for (COSName key : map.keySet()) {
            COSBase cosObj = map.getDictionaryObject(key);
            Object actualObject = null;
            if (cosObj instanceof COSString) {
                actualObject = ((COSString) cosObj).getString();
            } else if (cosObj instanceof COSInteger) {
                actualObject = ((COSInteger) cosObj).intValue();
            } else if (cosObj instanceof COSName) {
                actualObject = ((COSName) cosObj).getName();
            } else if (cosObj instanceof COSFloat) {
                actualObject = ((COSFloat) cosObj).floatValue();
            } else if (cosObj instanceof COSBoolean) {
                actualObject = ((COSBoolean) cosObj).getValue() ? Boolean.TRUE : Boolean.FALSE;
            } else {
                throw new IOException("Error:unknown type of object to convert:" + cosObj);
            }
            actualMap.put(key.getName(), actualObject);
        }
        retval = new COSDictionaryMap<String, Object>(actualMap, map);
    }
    return retval;
}
Also used : COSInteger(com.tom_roush.pdfbox.cos.COSInteger) COSName(com.tom_roush.pdfbox.cos.COSName) HashMap(java.util.HashMap) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString) IOException(java.io.IOException) COSString(com.tom_roush.pdfbox.cos.COSString) COSBoolean(com.tom_roush.pdfbox.cos.COSBoolean) COSFloat(com.tom_roush.pdfbox.cos.COSFloat)

Example 4 with COSBoolean

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

the class PDEncryption method isEncryptMetaData.

/**
 * Will get the EncryptMetaData dictionary info.
 *
 * @return true if EncryptMetaData is explicitly set to false (the default is true)
 */
public boolean isEncryptMetaData() {
    // default is true (see 7.6.3.2 Standard Encryption Dictionary PDF 32000-1:2008)
    boolean encryptMetaData = true;
    COSBase value = dictionary.getDictionaryObject(COSName.ENCRYPT_META_DATA);
    if (value instanceof COSBoolean) {
        encryptMetaData = ((COSBoolean) value).getValue();
    }
    return encryptMetaData;
}
Also used : COSBase(com.tom_roush.pdfbox.cos.COSBase) COSBoolean(com.tom_roush.pdfbox.cos.COSBoolean)

Aggregations

COSBoolean (com.tom_roush.pdfbox.cos.COSBoolean)4 COSBase (com.tom_roush.pdfbox.cos.COSBase)3 COSString (com.tom_roush.pdfbox.cos.COSString)3 IOException (java.io.IOException)3 COSFloat (com.tom_roush.pdfbox.cos.COSFloat)2 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)2 COSName (com.tom_roush.pdfbox.cos.COSName)2 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)1 COSArray (com.tom_roush.pdfbox.cos.COSArray)1 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1