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 PDFObject.
INSTANCE METHODS
- get(...path: PDFObjectPath)#
Access dictionaries and arrays in the PDFObject.
- Parameters:
...path – PDFObjectPath.
- 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(key: number | string | PDFObject, value: any)#
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]
.- Parameters:
key –
number
|string
| PDFObject.value –
any
. 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(key: number | string | PDFObject)#
Delete a reference from a PDFObject.
- Parameters:
key –
number
|string
| PDFObject.
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:
EXAMPLE
var resolvedObj = pdfObj.resolve();
- isArray()#
- Returns:
boolean
.
EXAMPLE
var result = pdfObj.isArray();
- isDictionary()#
- Returns:
boolean
.
EXAMPLE
var result = pdfObj.isDictionary();
- forEach(fn: (val: PDFObject, key: number | string, self: PDFObject) => void)#
Iterate over all the entries in a dictionary or array and call a function for each value-key pair.
- Parameters:
fn – Function in the format
function(value,key){...}
.
EXAMPLE
pdfObj.forEach(function(value,key){console.log("value="+value+",key="+key)});
- push(value: any)#
Append
value
to the end of the object.- Parameters:
value –
any
. 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:
number
.
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:
EXAMPLE
var buffer = pdfObj.readStream();
- readRawStream()#
Read the raw, uncompressed, contents of the stream object into a Buffer.
- Returns:
EXAMPLE
var buffer = pdfObj.readRawStream();
- writeObject(obj: any)#
Update the object the indirect reference points to.
- Parameters:
obj –
any
.
EXAMPLE
pdfObj.writeObject(obj);
- writeStream(buf: AnyBuffer)#
Update the contents of the stream the indirect reference points to. This will update the “Length”, “Filter” and “DecodeParms” automatically.
- Parameters:
buf –
AnyBuffer
.
EXAMPLE
pdfObj.writeStream(buffer);
- writeRawStream(buf: AnyBuffer)#
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.
- Parameters:
buf –
AnyBuffer
.
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:
number
.
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();
- 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:
Uint8Array
.
EXAMPLE
var val = pdfObj.asByteString();
PDFObjectPath#
This represents a type alias as follows:
type PDFObjectPath = Array<number | string | PDFObject>
Code samples
Code samples are in TypeScript and assume that the following requirements are defined in your TypeScript file header as follows:
import * as fs from "fs"
import * as mupdfjs from "mupdf/mupdfjs"