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);
}
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);
}
}
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);
}
}
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;
}
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);
}
}
}
Aggregations