protected PdfIndirectReference copyIndirect(PRIndirectReference in) throws BadPdfFormatException, IOException {
PdfObject srcObj = PdfReader.getPdfObjectRelease(in);
ByteStore streamKey = null;
boolean validStream = false;
if (srcObj.isStream()) {
streamKey = new ByteStore((PRStream)srcObj);
validStream = true;
PdfIndirectReference streamRef = (PdfIndirectReference) streamMap.get(streamKey);
if (streamRef != null) {
return streamRef;
}
}
PdfIndirectReference theRef;
RefKey key = new RefKey(in);
IndirectReferences iRef = (IndirectReferences) indirects.get(key);
if (iRef != null) {
theRef = iRef.getRef();
if (iRef.getCopied()) {
return theRef;
}
} else {
theRef = body.getPdfIndirectReference();
iRef = new IndirectReferences(theRef);
indirects.put(key, iRef);
}
if (srcObj.isDictionary()) {
PdfObject type = PdfReader.getPdfObjectRelease(((PdfDictionary)srcObj).get(PdfName.TYPE));
if (type != null && PdfName.PAGE.equals(type)) {
return theRef;
}
}
iRef.setCopied();
if (validStream) {
streamMap.put(streamKey, theRef);
}
PdfObject obj = copyObject(srcObj);
addToBody(obj, theRef);
return theRef;
}
Translate a PRIndirectReference to a PdfIndirectReference
In addition, translates the object numbers, and copies the
referenced object to the output file if it wasn't available
in the cache yet. If it's in the cache, the reference to
the already used stream is returned.
NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
file they came from, because each file has its own namespace. The translation
we do from their namespace to ours is *at best* heuristic, and guaranteed to
fail under some circumstances. |