package com.tom_roush.pdfbox.pdmodel.documentinterchange.logicalstructure; import com.tom_roush.pdfbox.cos.COSBase; import com.tom_roush.pdfbox.cos.COSDictionary; import com.tom_roush.pdfbox.cos.COSName; import com.tom_roush.pdfbox.pdmodel.common.COSObjectable; import com.tom_roush.pdfbox.pdmodel.graphics.PDXObject; import com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation; import com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationUnknown; import java.io.IOException; /** * An object reference. * * @author Ben Litchfield */ public class PDObjectReference implements COSObjectable { /** * TYPE of this object. */ public static final String TYPE = "OBJR"; private final COSDictionary dictionary; /** * Returns the underlying dictionary. * * @return the dictionary */ @Override public COSDictionary getCOSObject() { return this.dictionary; } /** * Default Constructor. * */ public PDObjectReference() { this.dictionary = new COSDictionary(); this.dictionary.setName(COSName.TYPE, TYPE); } /** * Constructor for an existing object reference. * * @param theDictionary The existing dictionary. */ public PDObjectReference(COSDictionary theDictionary) { dictionary = theDictionary; } /** * Gets a higher-level object for the referenced object. * Currently this method may return a {@link PDAnnotation}, * a {@link PDXObject} or <code>null</code>. * * @return a higher-level object for the referenced object */ public COSObjectable getReferencedObject() { COSBase obj = this.getCOSObject().getDictionaryObject(COSName.OBJ); if (!(obj instanceof COSDictionary)) { return null; } try { PDXObject xobject = PDXObject.createXObject(obj, null); // <-- TODO: valid? if (xobject != null) { return xobject; } COSDictionary objDictionary = (COSDictionary)obj; PDAnnotation annotation = PDAnnotation.createAnnotation(obj); /* * COSName.TYPE is optional, so if annotation is of type unknown and * COSName.TYPE is not COSName.ANNOT it still may be an annotation. * TODO shall we return the annotation object instead of null? * what else can be the target of the object reference? */ if (!(annotation instanceof PDAnnotationUnknown) || COSName.ANNOT.equals(objDictionary.getDictionaryObject(COSName.TYPE))) { return annotation; } } catch (IOException exception) { // this can only happen if the target is an XObject. } return null; } /** * Sets the referenced annotation. * * @param annotation the referenced annotation */ public void setReferencedObject(PDAnnotation annotation) { this.getCOSObject().setItem(COSName.OBJ, annotation); } /** * Sets the referenced XObject. * * @param xobject the referenced XObject */ public void setReferencedObject(PDXObject xobject) { this.getCOSObject().setItem(COSName.OBJ, xobject); } }