Class PDFObject
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.
Author Mike Wessler
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
an array of PDFObjectsstatic final int
a Booleanstatic 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
When a value ofinvalid reference
objNum
invalid reference
objGen
static final int
When a value ofinvalid reference
objNum
invalid reference
objGen
static final int
a Stream: a Hashmap with a byte arraystatic final int
a String -
Constructor Summary
ConstructorDescriptioncreate a new PDFObject that is the closest match to a given Java object.create a new simple PDFObject with a type and a valuecreate a new PDFObject based on a PDFXref -
Method Summary
Modifier and TypeMethodDescriptionMake sure that this object is dereferenced.boolean
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.getDecrypter.get the dictionary as a HashMap.get an Iterator over all the keys in the dictionary.getDictRef
(String k) 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.getRoot()
Returns the root of this object.byte[]
get the stream from this object.byte[]
Getter for the fieldstream
.get the stream from this object as a byte buffer.getStreamBuffer
(Set<String> filterLimits) 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()
-
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 ofinvalid reference
objNum
invalid reference
objGen
- See Also:
-
OBJ_NUM_TRAILER
public static final int OBJ_NUM_TRAILERWhen a value ofinvalid reference
objNum
invalid reference
objGen
- See Also:
-
nullObj
the NULL PDFObject
-
-
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 any.
-
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
- if any.
-
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
- if any.
-
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
- if any.
-
getStream
Getter for the field
stream
.- Parameters:
filterLimits
- aSet
object.- Returns:
- an array of
invalid reference
byte
- Throws:
IOException
- if any.
-
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
- if any.
-
setStream
set the stream of this object. It should have been a DICTIONARY before the call.- Parameters:
data
- the data, as a ByteBuffer.
-
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
- if any.
-
getStreamBuffer
get the stream from this object as a byte buffer. Will return null if this object isn't a STREAM.- Parameters:
filterLimits
- aSet
object.- Returns:
- the buffer, or null, if this isn't a STREAM.
- Throws:
IOException
- if any.
-
getIntValue
get the value as an int. Will return 0 if this object isn't a NUMBER.- Returns:
- a
Integer
object. - Throws:
IOException
- if any.
-
getFloatValue
get the value as a float. Will return 0 if this object isn't a NUMBER- Returns:
- a float.
- Throws:
IOException
- if any.
-
getDoubleValue
get the value as a double. Will return 0 if this object isn't a NUMBER.- Returns:
- a double.
- Throws:
IOException
- if any.
-
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.- Returns:
- a
String
object. - Throws:
IOException
- if any.
-
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
- if any.
-
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.- Returns:
- an array of
PDFObject
objects. - Throws:
IOException
- if any.
-
getBooleanValue
get the value as a boolean. Will return false if this object is not a BOOLEAN- Returns:
- a boolean.
- Throws:
IOException
- if any.
-
getAt
if this object is an ARRAY, get the PDFObject at some position in the array. If this is not an ARRAY, returns null.- Parameters:
idx
- aInteger
object.- Returns:
- a
PDFObject
object. - Throws:
IOException
- if any.
-
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.- Returns:
- a
Iterator
object. - Throws:
IOException
- if any.
-
getDictionary
get the dictionary as a HashMap. If this isn't a DICTIONARY or a STREAM, returns null- Returns:
- a
Map
object. - Throws:
IOException
- if any.
-
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.- Parameters:
k
- aString
object.- Returns:
- a
PDFObject
object. - Throws:
IOException
- if any.
-
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
- if any.
-
getDecrypter
getDecrypter.
- Returns:
- a
PDFDecrypter
object.
-
setObjectId
public void setObjectId(int objNum, int objGen) Set the object identifiers- Parameters:
objNum
- the object numberobjGen
- the object generation number
-
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.- Returns:
- a
PDFObject
object. - Throws:
IOException
- if any.
-
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
-
getRoot
Returns the root of this object.- Returns:
- a
PDFObject
object.
-