use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.
the class COSParser method parseObjectStream.
private void parseObjectStream(int objstmObjNr) throws IOException {
final COSBase objstmBaseObj = parseObjectDynamically(objstmObjNr, 0, true);
if (objstmBaseObj instanceof COSStream) {
// parse object stream
PDFObjectStreamParser parser;
try {
parser = new PDFObjectStreamParser((COSStream) objstmBaseObj, document);
} catch (IOException ex) {
if (isLenient) {
LOG.error("object stream " + objstmObjNr + " could not be parsed due to an exception", ex);
return;
} else {
throw ex;
}
}
try {
parser.parse();
} catch (IOException exception) {
if (isLenient) {
LOG.debug("Stop reading object stream " + objstmObjNr + " due to an exception", exception);
// the error is handled in parseDictObjects
return;
} else {
throw exception;
}
}
// register all objects which are referenced to be contained in object stream
for (COSObject next : parser.getObjects()) {
COSObjectKey stmObjKey = new COSObjectKey(next);
Long offset = xrefTrailerResolver.getXrefTable().get(stmObjKey);
if (offset != null && offset == -objstmObjNr) {
COSObject stmObj = document.getObjectFromPool(stmObjKey);
stmObj.setObject(next.getObject());
}
}
}
}
use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.
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 {
COSBase w = stream.getDictionaryObject(COSName.W);
if (!(w instanceof COSArray)) {
throw new IOException("/W array is missing in Xref stream");
}
COSArray xrefFormat = (COSArray) w;
COSArray indexArray = (COSArray) stream.getDictionaryObject(COSName.INDEX);
/*
* If Index doesn't exist, we will use the default values.
*/
if (indexArray == null) {
indexArray = new COSArray();
indexArray.add(COSInteger.ZERO);
indexArray.add(stream.getDictionaryObject(COSName.SIZE));
}
List<Long> objNums = new ArrayList<>();
/*
* Populates objNums with all object numbers available
*/
Iterator<COSBase> indexIter = indexArray.iterator();
while (indexIter.hasNext()) {
long objID = ((COSInteger) indexIter.next()).longValue();
int size = ((COSInteger) indexIter.next()).intValue();
for (int i = 0; i < size; i++) {
objNums.add(objID + i);
}
}
Iterator<Long> objIter = objNums.iterator();
/*
* Calculating the size of the line in bytes
*/
int w0 = xrefFormat.getInt(0);
int w1 = xrefFormat.getInt(1);
int w2 = xrefFormat.getInt(2);
int lineSize = w0 + w1 + w2;
while (!seqSource.isEOF() && objIter.hasNext()) {
byte[] currLine = new byte[lineSize];
seqSource.read(currLine);
int type;
if (w0 == 0) {
// "If the first element is zero,
// the type field shall not be present, and shall default to type 1"
type = 1;
} else {
type = 0;
/*
* Grabs the number of bytes specified for the first column in
* the W array and stores it.
*/
for (int i = 0; i < w0; i++) {
type += (currLine[i] & 0x00ff) << ((w0 - i - 1) * 8);
}
}
// Need to remember the current objID
Long objID = objIter.next();
/*
* 3 different types of entries.
*/
switch(type) {
case 0:
/*
* Skipping free objects
*/
break;
case 1:
int offset = 0;
for (int i = 0; i < w1; i++) {
offset += (currLine[i + w0] & 0x00ff) << ((w1 - i - 1) * 8);
}
int genNum = 0;
for (int i = 0; i < w2; i++) {
genNum += (currLine[i + w0 + w1] & 0x00ff) << ((w2 - i - 1) * 8);
}
COSObjectKey objKey = new COSObjectKey(objID, genNum);
xrefTrailerResolver.setXRef(objKey, offset);
break;
case 2:
/*
* object stored in object stream:
* 2nd argument is object number of object stream
* 3rd argument is index of object within object stream
*
* For sequential PDFParser we do not need this information
* because
* These objects are handled by the dereferenceObjects() method
* since they're only pointing to object numbers
*
* However 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
*/
int objstmObjNr = 0;
for (int i = 0; i < w1; i++) {
objstmObjNr += (currLine[i + w0] & 0x00ff) << ((w1 - i - 1) * 8);
}
objKey = new COSObjectKey(objID, 0);
xrefTrailerResolver.setXRef(objKey, -objstmObjNr);
break;
default:
break;
}
}
}
use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.
the class COSWriter method prepareIncrement.
private void prepareIncrement(PDDocument doc) {
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);
}
}
use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.
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);
}
}
}
use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.
the class ActionManagerFactory method getNextActions.
/**
* Returns all actions contained by the Next entry. If the action dictionary doesn't have Next action, the result is
* an empty list.
*
* @param ctx the preflight context.
* @param actionDictionary the dictionary to retrieve the actions from.
* @return the list of actions from the given dictionary.
* @throws ValidationException
*/
public final List<AbstractActionManager> getNextActions(PreflightContext ctx, COSDictionary actionDictionary) throws ValidationException {
List<AbstractActionManager> result = new ArrayList<>(0);
Map<COSObjectKey, Boolean> alreadyCreated = new HashMap<>();
COSBase nextDict = actionDictionary.getDictionaryObject(ACTION_DICTIONARY_KEY_NEXT);
if (nextDict != null) {
COSDocument cosDocument = ctx.getDocument().getDocument();
if (COSUtils.isArray(nextDict, cosDocument)) {
COSArray array = COSUtils.getAsArray(nextDict, cosDocument);
// ---- Next may contains an array of Action dictionary
for (int i = 0; i < array.size(); ++i) {
callCreateAction(array.get(i), ctx, result, alreadyCreated);
}
} else {
// ---- Next field contains a Dictionary or a reference to a Dictionary
callCreateAction(nextDict, ctx, result, alreadyCreated);
}
}
return result;
}
Aggregations