Package com.sun.pdfview
Class PDFObject
java.lang.Object
com.sun.pdfview.PDFObject
a class encapsulating all the possibilities of content for
an object in a PDF file.
A PDF object can be a simple type, like a Boolean, a Number, a String, or the Null value. It can also be a NAME, which looks like a string, but is a special type in PDF files, like "/Name".
A PDF object can also be complex types, including Array; Dictionary; Stream, which is a Dictionary plus an array of bytes; or Indirect, which is a reference to some other PDF object. Indirect references will always be dereferenced by the time any data is returned from one of the methods in this class.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
an array of PDFObjectsstatic final int
a Booleanprivate SoftReference
a cache of translated data.private SoftReference
a cached version of the decoded streamstatic final int
a Hashmap that maps String names to PDFObjectsstatic final int
an indirect referencestatic final int
a special PDF bare word, like R, obj, true, false, etcstatic final int
a special string, seen in PDF files as /Namestatic final int
the NULL object (there is only one)static final PDFObject
the NULL PDFObjectstatic final int
a Number, represented as a doublestatic final int
static final int
private int
private int
private PDFFile
the PDFFile from which this object came, used for dereferencesprivate ByteBuffer
the encoded stream, if this is a STREAM objectstatic final int
a Stream: a Hashmap with a byte arraystatic final int
a Stringprivate int
the type of this objectprivate Object
the value of this object. -
Constructor Summary
ConstructorsConstructorDescriptioncreate a new simple PDFObject with a type and a valuecreate a new PDFObject based on a PDFXrefcreate a new PDFObject that is the closest match to a given Java object. -
Method Summary
Modifier and TypeMethodDescriptionprivate ByteBuffer
Get the decoded stream valueMake sure that this object is dereferenced.boolean
Test whether two PDFObject are equal.getArray()
get the value as a PDFObject[].getAt
(int idx) if this object is an ARRAY, get the PDFObject at some position in the array.boolean
get the value as a boolean.getCache()
get the value in the cache.get the dictionary as a HashMap.get an Iterator over all the keys in the dictionary.getDictRef
(String key) get the value associated with a particular key in the dictionary.double
get the value as a double.float
get the value as a float.int
get the value as an int.int
Get the object generation number of this object; a negative value indicates that the object is not numbered, as it's not a top-level object: if the value isOBJ_NUM_EMBEDDED
, it is because it's embedded within another object.int
Get the object number of this object; a negative value indicates that the object is not numbered, as it's not a top-level object: if the value isOBJ_NUM_EMBEDDED
, it is because it's embedded within another object.byte[]
get the stream from this object.get the stream from this object as a byte buffer.get the value as a String.Get the value as a text string; i.e., a string encoded in UTF-16BE or PDFDocEncoding.int
getType()
get the type of this object.boolean
isDictType
(String match) returns true only if this object is a DICTIONARY or a STREAM, and the "Type" entry in the dictionary matches a given value.boolean
Identify whether the object is currently an indirect/cross-referencevoid
set the cached value.void
setObjectId
(int objNum, int objGen) Set the object identifiersvoid
setStream
(ByteBuffer data) set the stream of this object.toString()
return a representation of this PDFObject as a String.
-
Field Details
-
INDIRECT
public static final int INDIRECTan indirect reference- See Also:
-
BOOLEAN
public static final int BOOLEANa Boolean- See Also:
-
NUMBER
public static final int NUMBERa Number, represented as a double- See Also:
-
STRING
public static final int STRINGa String- See Also:
-
NAME
public static final int NAMEa special string, seen in PDF files as /Name- See Also:
-
ARRAY
public static final int ARRAYan array of PDFObjects- See Also:
-
DICTIONARY
public static final int DICTIONARYa Hashmap that maps String names to PDFObjects- See Also:
-
STREAM
public static final int STREAMa Stream: a Hashmap with a byte array- See Also:
-
NULL
public static final int NULLthe NULL object (there is only one)- See Also:
-
KEYWORD
public static final int KEYWORDa special PDF bare word, like R, obj, true, false, etc- See Also:
-
OBJ_NUM_EMBEDDED
public static final int OBJ_NUM_EMBEDDEDWhen a value ofobjNum
orobjGen
, indicates that the object is not top-level, and is embedded in another object- See Also:
-
OBJ_NUM_TRAILER
public static final int OBJ_NUM_TRAILERWhen a value ofobjNum
orobjGen
, indicates that the object is not top-level, and is embedded directly in the trailer.- See Also:
-
nullObj
the NULL PDFObject -
type
private int typethe type of this object -
value
the value of this object. It can be a wide number of things, defined by type -
stream
the encoded stream, if this is a STREAM object -
decodedStream
a cached version of the decoded stream -
owner
the PDFFile from which this object came, used for dereferences -
cache
a cache of translated data. This data can be garbage collected at any time, after which it will have to be rebuilt. -
objNum
private int objNum- See Also:
-
objGen
private int objGen- See Also:
-
-
Constructor Details
-
PDFObject
create a new simple PDFObject with a type and a value- Parameters:
owner
- the PDFFile in which this object resides, used for dereferencing. This may be null.type
- the type of objectvalue
- the value. For DICTIONARY, this is a HashMap. for ARRAY it's an ArrayList. For NUMBER, it's a Double. for BOOLEAN, it's Boolean.TRUE or Boolean.FALSE. For everything else, it's a String.
-
PDFObject
create a new PDFObject that is the closest match to a given Java object. Possibilities include Double, String, PDFObject[], HashMap, Boolean, or PDFParser.Tok, which should be "true" or "false" to turn into a BOOLEAN.- Parameters:
obj
- the sample Java object to convert to a PDFObject.- Throws:
PDFParseException
- if the object isn't one of the above examples, and can't be turned into a PDFObject.
-
PDFObject
create a new PDFObject based on a PDFXref- Parameters:
owner
- the PDFFile from which the PDFXref was drawnxref
- the PDFXref to turn into a PDFObject
-
-
Method Details
-
getType
get the type of this object. The object will be dereferenced, so INDIRECT will never be returned.- Returns:
- the type of the object
- Throws:
IOException
-
setStream
set the stream of this object. It should have been a DICTIONARY before the call.- Parameters:
data
- the data, as a ByteBuffer.
-
getCache
get the value in the cache. May become null at any time.- Returns:
- the cached value, or null if the value has been garbage collected.
- Throws:
IOException
-
setCache
set the cached value. The object may be garbage collected if no other reference exists to it.- Parameters:
obj
- the object to be cached- Throws:
IOException
-
getStream
get the stream from this object. Will return null if this object isn't a STREAM.- Returns:
- the stream, or null, if this isn't a STREAM.
- Throws:
IOException
-
getStreamBuffer
get the stream from this object as a byte buffer. Will return null if this object isn't a STREAM.- Returns:
- the buffer, or null, if this isn't a STREAM.
- Throws:
IOException
-
decodeStream
Get the decoded stream value- Throws:
IOException
-
getIntValue
get the value as an int. Will return 0 if this object isn't a NUMBER.- Throws:
IOException
-
getFloatValue
get the value as a float. Will return 0 if this object isn't a NUMBER- Throws:
IOException
-
getDoubleValue
get the value as a double. Will return 0 if this object isn't a NUMBER.- Throws:
IOException
-
getStringValue
get the value as a String. Will return null if the object isn't a STRING, NAME, or KEYWORD. This method will NOT convert a NUMBER to a String. If the string is actually a text string (i.e., may be encoded in UTF16-BE or PdfDocEncoding), then one should usegetTextStringValue()
or use one of thePDFStringUtil
methods on the result from this method. The string value represents exactly the sequence of 8 bit characters present in the file, decrypted and decoded as appropriate, into a string containing only 8 bit character values - that is, each char will be between 0 and 255.- Throws:
IOException
-
getTextStringValue
Get the value as a text string; i.e., a string encoded in UTF-16BE or PDFDocEncoding. Simple latin alpha-numeric characters are preserved in both these encodings.- Returns:
- the text string value
- Throws:
IOException
-
getArray
get the value as a PDFObject[]. If this object is an ARRAY, will return the array. Otherwise, will return an array of one element with this object as the element.- Throws:
IOException
-
getBooleanValue
get the value as a boolean. Will return false if this object is not a BOOLEAN- Throws:
IOException
-
getAt
if this object is an ARRAY, get the PDFObject at some position in the array. If this is not an ARRAY, returns null.- Throws:
IOException
-
getDictKeys
get an Iterator over all the keys in the dictionary. If this object is not a DICTIONARY or a STREAM, returns an Iterator over the empty list.- Throws:
IOException
-
getDictionary
get the dictionary as a HashMap. If this isn't a DICTIONARY or a STREAM, returns null- Throws:
IOException
-
getDictRef
get the value associated with a particular key in the dictionary. If this isn't a DICTIONARY or a STREAM, or there is no such key, returns null.- Throws:
IOException
-
isDictType
returns true only if this object is a DICTIONARY or a STREAM, and the "Type" entry in the dictionary matches a given value.- Parameters:
match
- the expected value for the "Type" key in the dictionary- Returns:
- whether the dictionary is of the expected type
- Throws:
IOException
-
getDecrypter
-
setObjectId
public void setObjectId(int objNum, int objGen) Set the object identifiers- Parameters:
objNum
- the object numberobjGen
- the object generation number
-
getObjNum
public int getObjNum()Get the object number of this object; a negative value indicates that the object is not numbered, as it's not a top-level object: if the value isOBJ_NUM_EMBEDDED
, it is because it's embedded within another object. If the value isOBJ_NUM_TRAILER
, it's because it's an object from the trailer.- Returns:
- the object number, if positive
-
getObjGen
public int getObjGen()Get the object generation number of this object; a negative value indicates that the object is not numbered, as it's not a top-level object: if the value isOBJ_NUM_EMBEDDED
, it is because it's embedded within another object. If the value isOBJ_NUM_TRAILER
, it's because it's an object from the trailer.- Returns:
- the object generation number, if positive
-
toString
return a representation of this PDFObject as a String. Does NOT dereference anything: this is the only method that allows you to distinguish an INDIRECT PDFObject. -
dereference
Make sure that this object is dereferenced. Use the cache of an indirect object to cache the dereferenced value, if possible.- Throws:
IOException
-
isIndirect
public boolean isIndirect()Identify whether the object is currently an indirect/cross-reference- Returns:
- whether currently indirect
-
equals
Test whether two PDFObject are equal. Objects are equal IFF they are the same reference OR they are both indirect objects with the same id and generation number in their xref
-