Search in sources :

Example 46 with COSName

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

the class BaseParser method parseCOSDictionaryNameValuePair.

private boolean parseCOSDictionaryNameValuePair(COSDictionary obj) throws IOException {
    COSName key = parseCOSName();
    COSBase value = parseCOSDictionaryValue();
    skipSpaces();
    if (value == null) {
        Log.w("PdfBox-Android", "Bad dictionary declaration at offset " + seqSource.getPosition());
        return false;
    } else {
        // label this item as direct, to avoid signature problems.
        value.setDirect(true);
        obj.setItem(key, value);
    }
    return true;
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 47 with COSName

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

the class PDFCloneUtility method cloneForNewDocument.

/**
 * Deep-clones the given object for inclusion into a different PDF document identified by
 * the destination parameter.
 * @param base the initial object as the root of the deep-clone operation
 * @return the cloned instance of the base object
 * @throws IOException if an I/O error occurs
 */
public COSBase cloneForNewDocument(Object base) throws IOException {
    if (base == null) {
        return null;
    }
    COSBase retval = clonedVersion.get(base);
    if (retval != null) {
        // we are done, it has already been converted.
        return retval;
    }
    if (base instanceof COSBase && clonedValues.contains(base)) {
        // Don't clone a clone
        return (COSBase) base;
    }
    if (base instanceof List) {
        COSArray array = new COSArray();
        List<?> list = (List<?>) base;
        for (Object obj : list) {
            array.add(cloneForNewDocument(obj));
        }
        retval = array;
    } else if (base instanceof COSObjectable && !(base instanceof COSBase)) {
        retval = cloneForNewDocument(((COSObjectable) base).getCOSObject());
    } else if (base instanceof COSObject) {
        COSObject object = (COSObject) base;
        retval = cloneForNewDocument(object.getObject());
    } else if (base instanceof COSArray) {
        COSArray newArray = new COSArray();
        COSArray array = (COSArray) base;
        for (int i = 0; i < array.size(); i++) {
            newArray.add(cloneForNewDocument(array.get(i)));
        }
        retval = newArray;
    } else if (base instanceof COSStream) {
        COSStream originalStream = (COSStream) base;
        COSStream stream = destination.getDocument().createCOSStream();
        OutputStream output = stream.createRawOutputStream();
        InputStream input = originalStream.createRawInputStream();
        IOUtils.copy(input, output);
        input.close();
        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;
        retval = new COSDictionary();
        clonedVersion.put(base, retval);
        for (Map.Entry<COSName, COSBase> entry : dic.entrySet()) {
            ((COSDictionary) retval).setItem(entry.getKey(), cloneForNewDocument(entry.getValue()));
        }
    } else {
        retval = (COSBase) base;
    }
    clonedVersion.put(base, retval);
    clonedValues.add(retval);
    return retval;
}
Also used : COSStream(com.tom_roush.pdfbox.cos.COSStream) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) COSObjectable(com.tom_roush.pdfbox.pdmodel.common.COSObjectable) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSName(com.tom_roush.pdfbox.cos.COSName) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) List(java.util.List) COSObject(com.tom_roush.pdfbox.cos.COSObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 48 with COSName

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

the class PDFMergerUtility method mergeRoleMap.

private void mergeRoleMap(PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree) {
    COSDictionary srcDict = srcStructTree.getCOSObject().getCOSDictionary(COSName.ROLE_MAP);
    COSDictionary destDict = destStructTree.getCOSObject().getCOSDictionary(COSName.ROLE_MAP);
    if (srcDict == null) {
        return;
    }
    if (destDict == null) {
        // clone not needed
        destStructTree.getCOSObject().setItem(COSName.ROLE_MAP, srcDict);
        return;
    }
    for (Map.Entry<COSName, COSBase> entry : srcDict.entrySet()) {
        COSBase destValue = destDict.getDictionaryObject(entry.getKey());
        if (destValue != null && destValue.equals(entry.getValue())) {
            // already exists, but identical
            continue;
        }
        if (destDict.containsKey(entry.getKey())) {
            Log.w("PdfBox-Android", "key " + entry.getKey() + " already exists in destination RoleMap");
        } else {
            destDict.setItem(entry.getKey(), entry.getValue());
        }
    }
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSName(com.tom_roush.pdfbox.cos.COSName) COSBase(com.tom_roush.pdfbox.cos.COSBase) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 49 with COSName

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

the class COSWriter method doWriteXRefInc.

private void doWriteXRefInc(COSDocument doc, long hybridPrev) throws IOException {
    if (doc.isXRefStream() || hybridPrev != -1) {
        // the file uses XrefStreams, so we need to update
        // it with an xref stream. We create a new one and fill it
        // with data available here
        // create a new XRefStrema object
        PDFXRefStream pdfxRefStream = new PDFXRefStream(doc);
        // add all entries from the incremental update.
        List<COSWriterXRefEntry> xRefEntries2 = getXRefEntries();
        for (COSWriterXRefEntry cosWriterXRefEntry : xRefEntries2) {
            pdfxRefStream.addEntry(cosWriterXRefEntry);
        }
        COSDictionary trailer = doc.getTrailer();
        if (incrementalUpdate) {
            // use previous startXref value as new PREV value
            trailer.setLong(COSName.PREV, doc.getStartXref());
        } else {
            trailer.removeItem(COSName.PREV);
        }
        pdfxRefStream.addTrailerInfo(trailer);
        // the size is the highest object number+1. we add one more
        // for the xref stream object we are going to write
        pdfxRefStream.setSize(getNumber() + 2);
        setStartxref(getStandardOutput().getPos());
        COSStream stream2 = pdfxRefStream.getStream();
        doWriteObject(stream2);
    }
    if (!doc.isXRefStream() || hybridPrev != -1) {
        COSDictionary trailer = doc.getTrailer();
        trailer.setLong(COSName.PREV, doc.getStartXref());
        if (hybridPrev != -1) {
            COSName xrefStm = COSName.XREF_STM;
            trailer.removeItem(xrefStm);
            trailer.setLong(xrefStm, getStartxref());
        }
        doWriteXRefTable();
        doWriteTrailer(doc);
    }
}
Also used : COSStream(com.tom_roush.pdfbox.cos.COSStream) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSName(com.tom_roush.pdfbox.cos.COSName) PDFXRefStream(com.tom_roush.pdfbox.pdfparser.PDFXRefStream)

Example 50 with COSName

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

the class COSWriter method visitFromDictionary.

@Override
public Object visitFromDictionary(COSDictionary obj) throws IOException {
    if (!reachedSignature) {
        COSBase itemType = obj.getItem(COSName.TYPE);
        if (COSName.SIG.equals(itemType) || COSName.DOC_TIME_STAMP.equals(itemType)) {
            reachedSignature = true;
        }
    }
    getStandardOutput().write(DICT_OPEN);
    getStandardOutput().writeEOL();
    for (Map.Entry<COSName, COSBase> entry : obj.entrySet()) {
        COSBase value = entry.getValue();
        if (value != null) {
            entry.getKey().accept(this);
            getStandardOutput().write(SPACE);
            if (value instanceof COSDictionary) {
                COSDictionary dict = (COSDictionary) value;
                if (!incrementalUpdate) {
                    // write all XObjects as direct objects, this will save some size
                    // PDFBOX-3684: but avoid dictionary that references itself
                    COSBase item = dict.getItem(COSName.XOBJECT);
                    if (item != null && !COSName.XOBJECT.equals(entry.getKey())) {
                        item.setDirect(true);
                    }
                    item = dict.getItem(COSName.RESOURCES);
                    if (item != null && !COSName.RESOURCES.equals(entry.getKey())) {
                        item.setDirect(true);
                    }
                }
                if (dict.isDirect()) {
                    // If the object should be written direct, we need
                    // to pass the dictionary to the visitor again.
                    visitFromDictionary(dict);
                } else {
                    addObjectToWrite(dict);
                    writeReference(dict);
                }
            } else if (value instanceof COSObject) {
                COSBase subValue = ((COSObject) value).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(value);
                    writeReference(value);
                } else {
                    subValue.accept(this);
                }
            } else {
                // content and byterange
                if (reachedSignature && COSName.CONTENTS.equals(entry.getKey())) {
                    signatureOffset = getStandardOutput().getPos();
                    value.accept(this);
                    signatureLength = getStandardOutput().getPos() - signatureOffset;
                } else if (reachedSignature && COSName.BYTERANGE.equals(entry.getKey())) {
                    byteRangeArray = (COSArray) entry.getValue();
                    byteRangeOffset = getStandardOutput().getPos() + 1;
                    value.accept(this);
                    byteRangeLength = getStandardOutput().getPos() - 1 - byteRangeOffset;
                    reachedSignature = false;
                } else {
                    value.accept(this);
                }
            }
            getStandardOutput().writeEOL();
        } else {
        // then we won't write anything, there are a couple cases
        // were the value of an entry in the COSDictionary will
        // be a dangling reference that points to nothing
        // so we will just not write out the entry if that is the case
        }
    }
    getStandardOutput().write(DICT_CLOSE);
    getStandardOutput().writeEOL();
    return null;
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

COSName (com.tom_roush.pdfbox.cos.COSName)83 COSBase (com.tom_roush.pdfbox.cos.COSBase)50 COSArray (com.tom_roush.pdfbox.cos.COSArray)27 IOException (java.io.IOException)24 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)23 COSObject (com.tom_roush.pdfbox.cos.COSObject)11 COSString (com.tom_roush.pdfbox.cos.COSString)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)9 COSStream (com.tom_roush.pdfbox.cos.COSStream)7 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)7 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)5 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)5 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)5 PDXObject (com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)5 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)4 PDTransparencyGroup (com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup)4 OutputStream (java.io.OutputStream)4 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)3