Search in sources :

Example 11 with COSBase

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

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

the class PDFMergerUtility method acroFormJoinFieldsMode.

/*
     * Merge the contents of the source form into the destination form for the
     * destination file.
     *
     * @param cloner the object cloner for the destination document
     * @param destAcroForm the destination form
     * @param srcAcroForm the source form
     * @throws IOException If an error occurs while adding the field.
     */
private void acroFormJoinFieldsMode(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm) throws IOException {
    List<PDField> srcFields = srcAcroForm.getFields();
    COSArray destFields;
    if (srcFields != null && !srcFields.isEmpty()) {
        // get the destinations root fields. Could be that the entry doesn't exist
        // or is of wrong type
        COSBase base = destAcroForm.getCOSObject().getItem(COSName.FIELDS);
        if (base instanceof COSArray) {
            destFields = (COSArray) base;
        } else {
            destFields = new COSArray();
        }
        for (PDField srcField : srcAcroForm.getFieldTree()) {
            // if the form already has a field with this name then we need to rename this field
            // to prevent merge conflicts.
            PDField destinationField = destAcroForm.getField(srcField.getFullyQualifiedName());
            if (destinationField == null) {
                // field doesn't exist - can safely add it
                COSDictionary importedField = (COSDictionary) cloner.cloneForNewDocument(srcField.getCOSObject());
                destFields.add(importedField);
            } else {
                mergeFields(cloner, destinationField, srcField);
            }
        }
        destAcroForm.getCOSObject().setItem(COSName.FIELDS, destFields);
    }
}
Also used : PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 13 with COSBase

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

use of com.tom_roush.pdfbox.cos.COSBase 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)

Example 15 with COSBase

use of com.tom_roush.pdfbox.cos.COSBase 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

COSBase (com.tom_roush.pdfbox.cos.COSBase)215 COSArray (com.tom_roush.pdfbox.cos.COSArray)108 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)68 COSName (com.tom_roush.pdfbox.cos.COSName)50 COSObject (com.tom_roush.pdfbox.cos.COSObject)42 IOException (java.io.IOException)34 COSString (com.tom_roush.pdfbox.cos.COSString)33 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)29 ArrayList (java.util.ArrayList)29 COSStream (com.tom_roush.pdfbox.cos.COSStream)20 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)14 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)11 HashMap (java.util.HashMap)11 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)10 Map (java.util.Map)10 List (java.util.List)9 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)8 COSFloat (com.tom_roush.pdfbox.cos.COSFloat)7 COSObjectKey (com.tom_roush.pdfbox.cos.COSObjectKey)7 PDFStreamParser (com.tom_roush.pdfbox.pdfparser.PDFStreamParser)6