Search in sources :

Example 1 with COSObject

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

the class PDDocument method prepareVisibleSignature.

private void prepareVisibleSignature(PDSignatureField signatureField, PDAcroForm acroForm, COSDocument visualSignature) {
    // Obtain visual signature object
    boolean annotNotFound = true;
    boolean sigFieldNotFound = true;
    for (COSObject cosObject : visualSignature.getObjects()) {
        if (!annotNotFound && !sigFieldNotFound) {
            break;
        }
        COSBase base = cosObject.getObject();
        if (base instanceof COSDictionary) {
            COSDictionary cosBaseDict = (COSDictionary) base;
            // Search for signature annotation
            COSBase type = cosBaseDict.getDictionaryObject(COSName.TYPE);
            if (annotNotFound && COSName.ANNOT.equals(type)) {
                assignSignatureRectangle(signatureField, cosBaseDict);
                annotNotFound = false;
            }
            // Search for signature field
            COSBase fieldType = cosBaseDict.getDictionaryObject(COSName.FT);
            COSBase apDict = cosBaseDict.getDictionaryObject(COSName.AP);
            if (sigFieldNotFound && COSName.SIG.equals(fieldType) && apDict instanceof COSDictionary) {
                assignAppearanceDictionary(signatureField, (COSDictionary) apDict);
                assignAcroFormDefaultResource(acroForm, cosBaseDict);
                sigFieldNotFound = false;
            }
        }
    }
    if (annotNotFound || sigFieldNotFound) {
        throw new IllegalArgumentException("Template is missing required objects");
    }
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 2 with COSObject

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

the class BaseParser method parseCOSArray.

/**
 * This will parse a PDF array object.
 *
 * @return The parsed PDF array.
 *
 * @throws IOException If there is an error parsing the stream.
 */
protected COSArray parseCOSArray() throws IOException {
    long startPosition = seqSource.getPosition();
    readExpectedChar('[');
    COSArray po = new COSArray();
    COSBase pbo;
    skipSpaces();
    int i;
    while (((i = seqSource.peek()) > 0) && ((char) i != ']')) {
        pbo = parseDirObject();
        if (pbo instanceof COSObject) {
            // We have to check if the expected values are there or not PDFBOX-385
            if (po.size() > 0 && po.get(po.size() - 1) instanceof COSInteger) {
                COSInteger genNumber = (COSInteger) po.remove(po.size() - 1);
                if (po.size() > 0 && po.get(po.size() - 1) instanceof COSInteger) {
                    COSInteger number = (COSInteger) po.remove(po.size() - 1);
                    COSObjectKey key = new COSObjectKey(number.longValue(), genNumber.intValue());
                    pbo = getObjectFromPool(key);
                } else {
                    // the object reference is somehow wrong
                    pbo = null;
                }
            } else {
                pbo = null;
            }
        }
        if (pbo != null) {
            po.add(pbo);
        } else {
            // it could be a bad object in the array which is just skipped
            Log.w("PdfBox-Android", "Corrupt object reference at offset " + seqSource.getPosition() + ", start offset: " + startPosition);
            // This could also be an "endobj" or "endstream" which means we can assume that
            // the array has ended.
            String isThisTheEnd = readString();
            seqSource.unread(isThisTheEnd.getBytes(ISO_8859_1));
            if (ENDOBJ_STRING.equals(isThisTheEnd) || ENDSTREAM_STRING.equals(isThisTheEnd)) {
                return po;
            }
        }
        skipSpaces();
    }
    // read ']'
    seqSource.read();
    skipSpaces();
    return po;
}
Also used : COSObjectKey(com.tom_roush.pdfbox.cos.COSObjectKey) COSInteger(com.tom_roush.pdfbox.cos.COSInteger) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 3 with COSObject

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

the class BaseParser method parseDirObject.

/**
 * This will parse a directory object from the stream.
 *
 * @return The parsed object.
 *
 * @throws IOException If there is an error during parsing.
 */
protected COSBase parseDirObject() throws IOException {
    COSBase retval = null;
    skipSpaces();
    int nextByte = seqSource.peek();
    char c = (char) nextByte;
    switch(c) {
        case '<':
            {
                // pull off first left bracket
                int leftBracket = seqSource.read();
                // check for second left bracket
                c = (char) seqSource.peek();
                seqSource.unread(leftBracket);
                if (c == '<') {
                    retval = parseCOSDictionary();
                    skipSpaces();
                } else {
                    retval = parseCOSString();
                }
                break;
            }
        case '[':
            {
                // array
                retval = parseCOSArray();
                break;
            }
        case '(':
            retval = parseCOSString();
            break;
        case '/':
            // name
            retval = parseCOSName();
            break;
        case 'n':
            {
                // null
                readExpectedString(NULL);
                retval = COSNull.NULL;
                break;
            }
        case 't':
            {
                String trueString = new String(seqSource.readFully(4), ISO_8859_1);
                if (trueString.equals(TRUE)) {
                    retval = COSBoolean.TRUE;
                } else {
                    throw new IOException("expected true actual='" + trueString + "' " + seqSource + "' at offset " + seqSource.getPosition());
                }
                break;
            }
        case 'f':
            {
                String falseString = new String(seqSource.readFully(5), ISO_8859_1);
                if (falseString.equals(FALSE)) {
                    retval = COSBoolean.FALSE;
                } else {
                    throw new IOException("expected false actual='" + falseString + "' " + seqSource + "' at offset " + seqSource.getPosition());
                }
                break;
            }
        case 'R':
            seqSource.read();
            retval = new COSObject(null);
            break;
        case (char) -1:
            return null;
        default:
            {
                if (Character.isDigit(c) || c == '-' || c == '+' || c == '.') {
                    StringBuilder buf = new StringBuilder();
                    int ic = seqSource.read();
                    c = (char) ic;
                    while (Character.isDigit(c) || c == '-' || c == '+' || c == '.' || c == 'E' || c == 'e') {
                        buf.append(c);
                        ic = seqSource.read();
                        c = (char) ic;
                    }
                    if (ic != -1) {
                        seqSource.unread(ic);
                    }
                    retval = COSNumber.get(buf.toString());
                } else {
                    // This is not suppose to happen, but we will allow for it
                    // so we are more compatible with POS writers that don't
                    // follow the spec
                    String badString = readString();
                    if (badString.isEmpty()) {
                        int peek = seqSource.peek();
                        // we can end up in an infinite loop otherwise
                        throw new IOException("Unknown dir object c='" + c + "' cInt=" + (int) c + " peek='" + (char) peek + "' peekInt=" + peek + " at offset " + seqSource.getPosition());
                    }
                    // if it's an endstream/endobj, we want to put it back so the caller will see it
                    if (ENDOBJ_STRING.equals(badString) || ENDSTREAM_STRING.equals(badString)) {
                        seqSource.unread(badString.getBytes(ISO_8859_1));
                    }
                }
            }
    }
    return retval;
}
Also used : COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString) IOException(java.io.IOException)

Example 4 with COSObject

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

the class PDFCloneUtility method cloneMerge.

/**
 * Merges two objects of the same type by deep-cloning its members.
 * <br>
 * Base and target must be instances of the same class.
 * @param base the base object to be cloned
 * @param target the merge target
 * @throws IOException if an I/O error occurs
 */
public void cloneMerge(final COSObjectable base, COSObjectable target) throws IOException {
    if (base == null) {
        return;
    }
    COSBase retval = clonedVersion.get(base);
    if (retval != null) {
        return;
    // we are done, it has already been converted. // ### Is that correct for cloneMerge???
    }
    // TODO what when clone-merging a clone? Does it ever happen?
    if (!(base instanceof COSBase)) {
        cloneMerge(base.getCOSObject(), target.getCOSObject());
    } else if (base instanceof COSObject) {
        if (target instanceof COSObject) {
            cloneMerge(((COSObject) base).getObject(), ((COSObject) target).getObject());
        } else if (target instanceof COSDictionary || target instanceof COSArray) {
            cloneMerge(((COSObject) base).getObject(), target);
        }
    } else if (base instanceof COSArray) {
        COSArray array = (COSArray) base;
        for (int i = 0; i < array.size(); i++) {
            ((COSArray) target).add(cloneForNewDocument(array.get(i)));
        }
    } else if (base instanceof COSStream) {
        // does that make sense???
        COSStream originalStream = (COSStream) base;
        COSStream stream = destination.getDocument().createCOSStream();
        OutputStream output = stream.createOutputStream(originalStream.getFilters());
        IOUtils.copy(originalStream.createInputStream(), output);
        output.close();
        clonedVersion.put(base, stream);
        for (Map.Entry<COSName, COSBase> entry : originalStream.entrySet()) {
            stream.setItem(entry.getKey(), cloneForNewDocument(entry.getValue()));
        }
        retval = stream;
    } else if (base instanceof COSDictionary) {
        COSDictionary dic = (COSDictionary) base;
        clonedVersion.put(base, retval);
        for (Map.Entry<COSName, COSBase> entry : dic.entrySet()) {
            COSName key = entry.getKey();
            COSBase value = entry.getValue();
            if (((COSDictionary) target).getItem(key) != null) {
                cloneMerge(value, ((COSDictionary) target).getItem(key));
            } else {
                ((COSDictionary) target).setItem(key, cloneForNewDocument(value));
            }
        }
    } else {
        retval = (COSBase) base;
    }
    clonedVersion.put(base, retval);
    clonedValues.add(retval);
}
Also used : COSStream(com.tom_roush.pdfbox.cos.COSStream) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSName(com.tom_roush.pdfbox.cos.COSName) COSObject(com.tom_roush.pdfbox.cos.COSObject) OutputStream(java.io.OutputStream) COSBase(com.tom_roush.pdfbox.cos.COSBase) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with COSObject

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

the class COSWriter method visitFromArray.

@Override
public Object visitFromArray(COSArray obj) throws IOException {
    int count = 0;
    getStandardOutput().write(ARRAY_OPEN);
    for (Iterator<COSBase> i = obj.iterator(); i.hasNext(); ) {
        COSBase current = i.next();
        if (current instanceof COSDictionary) {
            if (current.isDirect()) {
                visitFromDictionary((COSDictionary) current);
            } else {
                addObjectToWrite(current);
                writeReference(current);
            }
        } else if (current instanceof COSObject) {
            COSBase subValue = ((COSObject) current).getObject();
            if (willEncrypt || incrementalUpdate || subValue instanceof COSDictionary || subValue == null) {
                // PDFBOX-4308: added willEncrypt to prevent an object
                // that is referenced several times from being written
                // direct and indirect, thus getting encrypted
                // with wrong object number or getting encrypted twice
                addObjectToWrite(current);
                writeReference(current);
            } else {
                subValue.accept(this);
            }
        } else if (current == null) {
            COSNull.NULL.accept(this);
        } else {
            current.accept(this);
        }
        count++;
        if (i.hasNext()) {
            if (count % 10 == 0) {
                getStandardOutput().writeEOL();
            } else {
                getStandardOutput().write(SPACE);
            }
        }
    }
    getStandardOutput().write(ARRAY_CLOSE);
    getStandardOutput().writeEOL();
    return null;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Aggregations

COSObject (com.tom_roush.pdfbox.cos.COSObject)48 COSBase (com.tom_roush.pdfbox.cos.COSBase)40 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)21 COSArray (com.tom_roush.pdfbox.cos.COSArray)16 IOException (java.io.IOException)16 COSName (com.tom_roush.pdfbox.cos.COSName)11 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)7 ArrayList (java.util.ArrayList)7 COSObjectKey (com.tom_roush.pdfbox.cos.COSObjectKey)6 PDFStreamParser (com.tom_roush.pdfbox.pdfparser.PDFStreamParser)6 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)5 COSStream (com.tom_roush.pdfbox.cos.COSStream)5 HashMap (java.util.HashMap)5 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)4 Map (java.util.Map)4 OutputStream (java.io.OutputStream)3 COSString (com.tom_roush.pdfbox.cos.COSString)2 COSObjectable (com.tom_roush.pdfbox.pdmodel.common.COSObjectable)2 List (java.util.List)2 COSNull (com.tom_roush.pdfbox.cos.COSNull)1