/**
* 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.
*/
protected override PdfIndirectReference CopyIndirect(PRIndirectReference inp)
{
PdfObject srcObj = PdfReader.GetPdfObjectRelease(inp);
ByteStore streamKey = null;
bool validStream = false;
if (srcObj.IsStream())
{
streamKey = new ByteStore((PRStream)srcObj);
validStream = true;
PdfIndirectReference streamRef = (PdfIndirectReference)streamMap[streamKey];
if (streamRef != null)
{
return(streamRef);
}
}
PdfIndirectReference theRef;
RefKey key = new RefKey(inp);
IndirectReferences iRef = (IndirectReferences)indirects[key];
if (iRef != null)
{
theRef = iRef.Ref;
if (iRef.Copied)
{
return(theRef);
}
}
else
{
theRef = body.PdfIndirectReference;
iRef = new IndirectReferences(theRef);
indirects[key] = iRef;
}
if (srcObj != null && 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[streamKey] = theRef;
}
PdfObject obj = CopyObject(srcObj);
AddToBody(obj, theRef);
return(theRef);
}