Search in sources :

Example 1 with COSObjectKey

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

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

the class PDFXrefStreamParser method parse.

/**
 * Parses through the unfiltered stream and populates the xrefTable HashMap.
 * @throws IOException If there is an error while parsing the stream.
 */
public void parse() throws IOException {
    byte[] currLine = new byte[w[0] + w[1] + w[2]];
    while (!seqSource.isEOF() && objectNumbers.hasNext()) {
        seqSource.read(currLine);
        // get the current objID
        long objID = objectNumbers.next();
        // default value is 1 if w[0] == 0, otherwise parse first field
        int type = w[0] == 0 ? 1 : (int) parseValue(currLine, 0, w[0]);
        // Skip free objects (type 0) and invalid types
        if (type == 0) {
            continue;
        }
        // second field holds the offset (type 1) or the object stream number (type 2)
        long offset = parseValue(currLine, w[0], w[1]);
        // third field holds the generation number for type 1 entries
        int genNum = type == 1 ? (int) parseValue(currLine, w[0] + w[1], w[2]) : 0;
        COSObjectKey objKey = new COSObjectKey(objID, genNum);
        if (type == 1) {
            xrefTrailerResolver.setXRef(objKey, offset);
        } else {
            // For XRef aware parsers we have to know which objects contain object streams. We will store this
            // information in normal xref mapping table but add object stream number with minus sign in order to
            // distinguish from file offsets
            xrefTrailerResolver.setXRef(objKey, -offset);
        }
    }
    close();
}
Also used : COSObjectKey(com.tom_roush.pdfbox.cos.COSObjectKey)

Example 3 with COSObjectKey

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

the class COSWriter method prepareIncrement.

private void prepareIncrement(PDDocument doc) {
    try {
        if (doc != null) {
            COSDocument cosDoc = doc.getDocument();
            Map<COSObjectKey, Long> xrefTable = cosDoc.getXrefTable();
            Set<COSObjectKey> keySet = xrefTable.keySet();
            long highestNumber = doc.getDocument().getHighestXRefObjectNumber();
            for (COSObjectKey cosObjectKey : keySet) {
                COSBase object = cosDoc.getObjectFromPool(cosObjectKey).getObject();
                if (object != null && cosObjectKey != null && !(object instanceof COSNumber)) {
                    objectKeys.put(object, cosObjectKey);
                    keyObject.put(cosObjectKey, object);
                }
                if (cosObjectKey != null) {
                    long num = cosObjectKey.getNumber();
                    if (num > highestNumber) {
                        highestNumber = num;
                    }
                }
            }
            setNumber(highestNumber);
        }
    } catch (IOException e) {
        Log.e("PdfBox-Android", e.getMessage(), e);
    }
}
Also used : COSObjectKey(com.tom_roush.pdfbox.cos.COSObjectKey) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSDocument(com.tom_roush.pdfbox.cos.COSDocument) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException)

Example 4 with COSObjectKey

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

the class COSWriter method writeReference.

/**
 * visitFromObjRef method comment.
 *
 * @param obj The object that is being visited.
 *
 * @throws IOException If there is an exception while visiting this object.
 */
public void writeReference(COSBase obj) throws IOException {
    COSObjectKey key = getObjectKey(obj);
    getStandardOutput().write(String.valueOf(key.getNumber()).getBytes(Charsets.ISO_8859_1));
    getStandardOutput().write(SPACE);
    getStandardOutput().write(String.valueOf(key.getGeneration()).getBytes(Charsets.ISO_8859_1));
    getStandardOutput().write(SPACE);
    getStandardOutput().write(REFERENCE);
}
Also used : COSObjectKey(com.tom_roush.pdfbox.cos.COSObjectKey)

Example 5 with COSObjectKey

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

the class COSWriter method addObjectToWrite.

private void addObjectToWrite(COSBase object) {
    COSBase actual = object;
    if (actual instanceof COSObject) {
        actual = ((COSObject) actual).getObject();
    }
    if (!writtenObjects.contains(object) && !objectsToWriteSet.contains(object) && !actualsAdded.contains(actual)) {
        COSBase cosBase = null;
        COSObjectKey cosObjectKey = null;
        if (actual != null) {
            cosObjectKey = objectKeys.get(actual);
        }
        if (cosObjectKey != null) {
            cosBase = keyObject.get(cosObjectKey);
        }
        if (actual != null && objectKeys.containsKey(actual) && object instanceof COSUpdateInfo && !((COSUpdateInfo) object).isNeedToBeUpdated() && cosBase instanceof COSUpdateInfo && !((COSUpdateInfo) cosBase).isNeedToBeUpdated()) {
            return;
        }
        objectsToWrite.add(object);
        objectsToWriteSet.add(object);
        if (actual != null) {
            actualsAdded.add(actual);
        }
    }
}
Also used : COSObjectKey(com.tom_roush.pdfbox.cos.COSObjectKey) COSUpdateInfo(com.tom_roush.pdfbox.cos.COSUpdateInfo) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Aggregations

COSObjectKey (com.tom_roush.pdfbox.cos.COSObjectKey)17 COSBase (com.tom_roush.pdfbox.cos.COSBase)7 COSObject (com.tom_roush.pdfbox.cos.COSObject)6 IOException (java.io.IOException)6 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)4 COSArray (com.tom_roush.pdfbox.cos.COSArray)2 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)2 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)2 COSStream (com.tom_roush.pdfbox.cos.COSStream)2 COSDocument (com.tom_roush.pdfbox.cos.COSDocument)1 COSInputStream (com.tom_roush.pdfbox.cos.COSInputStream)1 COSString (com.tom_roush.pdfbox.cos.COSString)1 COSUpdateInfo (com.tom_roush.pdfbox.cos.COSUpdateInfo)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 TreeMap (java.util.TreeMap)1