PDFObject
¶
All functions that take PDFObjects
, do automatic translation between
JavaScript objects and PDFObjects
using a few basic rules:
Null, booleans, and numbers are translated directly.
JavaScript strings are translated to PDF names, unless
they are surrounded by parentheses: “Foo” becomes the PDF name /Foo and “(Foo)” becomes the PDF string (Foo). - Arrays and dictionaries are recursively translated to PDF arrays and dictionaries. Be aware of cycles though! The translation does NOT cope with cyclic references! - The translation goes both ways: PDF dictionaries and arrays can be accessed similarly to JavaScript objects and arrays by getting and setting their properties.
Instance properties
length
Length of the array.
Instance methods
- get(ref)¶
Access dictionaries and arrays in the
PDFObject
.- Arguments:
ref – Key or index.
- Returns:
The value for the key or index.
EXAMPLE
var dict = pdfDocument.newDictionary(); var value = dict.get("my_key"); var arr = pdfDocument.newArray(); var value = arr.get(1);
- put(ref, value)¶
Put information into dictionaries and arrays in the
PDFObject
. Dictionaries and arrays can also be accessed using normal property syntax:obj.Foo = 42; delete obj.Foo; x = obj[5]
.- Arguments:
ref – Key or index.
value – The value for the key or index.
EXAMPLE
var dict = pdfDocument.newDictionary(); dict.put("my_key", "my_value"); var arr = pdfDocument.newArray(); arr.put(0, 42);
- delete(ref)¶
Delete a reference from a
PDFObject
.- Arguments:
ref – Key or index.
EXAMPLE
pdfObj.delete("my_key"); var dict = pdfDocument.newDictionary(); dict.put("my_key", "my_value"); dict.delete("my_key"); var arr = pdfDocument.newArray(); arr.put(1, 42); arr.delete(1);
- resolve()¶
If the object is an indirect reference, return the object it points to; otherwise return the object itself.
- Returns:
Object.
EXAMPLE
var resolvedObj = pdfObj.resolve();
- compare(other_obj)¶
mutool only
Compare the object to another one. Returns 0 on match, non-zero on mismatch. Streams always mismatch.
- Arguments:
other –
PDFObject
.
- Returns:
Boolean
.
EXAMPLE
var match = pdfObj.compare(other_obj);
- isArray()¶
- Returns:
Boolean
.
EXAMPLE
var result = pdfObj.isArray();
- isDictionary()¶
- Returns:
Boolean
.
EXAMPLE
var result = pdfObj.isDictionary();
- forEach(fun)¶
Iterate over all the entries in a dictionary or array and call a function for each value-key pair.
- Arguments:
fun – Function in the format
function(value,key){...}
.
EXAMPLE
pdfObj.forEach(function(value,key){console.log("value="+value+",key="+key)});
- push(item)¶
Append
item
to the end of an array.- Arguments:
item – Item to add.
EXAMPLE
pdfObj.push("item");
- toString()¶
Returns the object as a pretty-printed string.
- Returns:
String
.
EXAMPLE
var str = pdfObj.toString();
- valueOf()¶
Convert primitive PDF objects to a corresponding primitive
Null
,Boolean
,Number
orString
JavaScript objects. Indirect PDF objects get converted to the string “R” while PDF names are converted to plain strings. PDF arrays or dictionaries are returned unchanged.- Returns:
Null
|Boolean
|Number
|String
.
EXAMPLE
var val = pdfObj.valueOf();
- isIndirect()¶
Is the object an indirect reference.
- Returns:
Boolean
.
EXAMPLE
var val = pdfObj.isIndirect();
- asIndirect()¶
Return the object number the indirect reference points to.
- Returns:
Integer
.
EXAMPLE
var val = pdfObj.asIndirect();
- isFilespec()¶
Is the object a file specification (or a reference to a file specification).
- Returns:
Boolean
.
EXAMPLE
var val = pdfObj.isFilespec();
PDF streams¶
The only way to access a stream is via an indirect object, since all streams are numbered objects.
- isStream()¶
True if the object is an indirect reference pointing to a stream.
- Returns:
Boolean
.
EXAMPLE
var val = pdfObj.isStream();
- readStream()¶
Read the contents of the stream object into a
Buffer
.- Returns:
Buffer
.
EXAMPLE
var buffer = pdfObj.readStream();
- readRawStream()¶
Read the raw, uncompressed, contents of the stream object into a
Buffer
.- Returns:
Buffer
.
EXAMPLE
var buffer = pdfObj.readRawStream();
- writeObject(obj)¶
Update the object the indirect reference points to.
- Arguments:
obj – Object to update.
EXAMPLE
pdfObj.writeObject(obj);
- writeStream(buffer)¶
Update the contents of the stream the indirect reference points to. This will update the “Length”, “Filter” and “DecodeParms” automatically.
- Arguments:
buffer –
Buffer
.
EXAMPLE
pdfObj.writeStream(buffer);
- writeRawStream(buffer)¶
Update the contents of the stream the indirect reference points to. The buffer must contain already compressed data that matches the “Filter” and “DecodeParms”. This will update the “Length” automatically, but leave the “Filter” and “DecodeParms” untouched.
- Arguments:
buffer –
Buffer
.
EXAMPLE
pdfObj.writeRawStream(buffer);
Primitive Objects¶
Primitive PDF objects such as booleans, names, and numbers can usually be treated like JavaScript values. When that is not sufficient use these functions:
- isNull()¶
Returns true if the object is a
null
object.- Returns:
Boolean
.
EXAMPLE
var val = pdfObj.isNull();
- isBoolean()¶
Returns true if the object is a
Boolean
object.- Returns:
Boolean
.
EXAMPLE
var val = pdfObj.isBoolean();
- asBoolean()¶
Get the boolean primitive value.
- Returns:
Boolean
.
EXAMPLE
var val = pdfObj.asBoolean();
- isInteger()¶
Returns true if the object is an
Integer
object.- Returns:
Boolean
.
EXAMPLE
var val = pdfObj.isInteger();
- isNumber()¶
Returns true if the object is a
Number
object.- Returns:
Boolean
.
EXAMPLE
var val = pdfObj.isNumber();
- asNumber()¶
Get the number primitive value.
- Returns:
Integer
.
EXAMPLE
var val = pdfObj.asNumber();
- isName()¶
Returns true if the object is a
Name
object.- Returns:
Boolean
.
EXAMPLE
var val = pdfObj.isName();
- asName()¶
Get the name as a string.
- Returns:
String
.
EXAMPLE
var val = pdfObj.asName();
- isReal()¶
Returns true if the object is a
Real
object.- Returns:
Boolean
.
EXAMPLE
var val = pdfObj.isReal();
- isString()¶
Returns true if the object is a
String
object.- Returns:
Boolean
.
EXAMPLE
var val = pdfObj.isString();
- asString()¶
Convert a “text string” to a JavaScript unicode string.
- Returns:
String
.
EXAMPLE
var val = pdfObj.asString();
- asByteString()¶
Convert a string to an array of byte values.
- Returns:
[...]
.
EXAMPLE
var val = pdfObj.asByteString();