PDFDocument#
STATIC METHODS
- createBlankDocument(width: number = 595, height: number = 842)#
Creates and returns a one paged PDFDocument. If no width or height is supplied then the default values for an A4 sized document will be used.
- Parameters:
width –
number
. Width of document.height –
number
. Height of document.
- Returns:
EXAMPLE
let document = mupdfjs.PDFDocument.createBlankDocument()
- openDocument(from: Buffer | ArrayBuffer | Uint8Array | Stream, fileType: string)#
Opens a document from a supplied Buffer,
Array
orStream
.- Parameters:
from – Buffer |
ArrayBuffer
|Uint8Array
| StreamfileType –
string
. Used to denote fie type, e.g. “application/pdf”.
- Returns:
EXAMPLE
let document = mupdfjs.PDFDocument.openDocument(fs.readFileSync("test.pdf"), "application/pdf")
INSTANCE METHODS
- newPage(pno: number = -1, width: number = 595, height: number = 842)#
Creates and returns a PDFPage at a given place location in a document. If no width or height is supplied then the default values for an A4 sized document will be used.
- Parameters:
pno –
number
. The page location in the document to insert the page0
= start of document,-1
= end of document.width –
number
. Width of document.height –
number
. Height of document.
- Returns:
- copyPage(pno: number, to: number = -1)#
Copys a page from one index to another in the document.
- Parameters:
pno –
number
. The page location in the document to copy the page from,0
= start of document,-1
= end of document.to –
number
. The page location in the document to copy the page to,0
= start of document,-1
= end of document.
- deletePage(index: number)#
Deletes a page at a specific index. Zero-indexed.
- Parameters:
index –
number
.0
= first page of document.
- deletePages(...args: any[])#
A convenience method for deleting a range of pages.
- Parameters:
...args –
any[]
.
Using a range
Use
number, number
to delete a range of pages including the start and end index.Using keywords
Use
{fromPage:number, toPage:number}
to delete a range of pages between thefromPage
and thetoPage
(and including thefromPage
and thetoPage
).For example if you called:
document.deletePages({fromPage:2, toPage:5})
it would delete pages at indexes 2,3,4 & 5.Using a set
Use
[number, ...]
to define the pages you want to delete.For example if you called:
document.deletePages([0, 4, 6, 7])
it would delete pages at indexes 0,4,6 & 7.Note
Remember pages indexes are zero-indexed! Thus
document.deletePages({fromPage:1, toPage:3})
is actually deleting from page 2 of your document.
- split(range: number[] | undefined)#
Splits a document into multiple documents with defined page ranges and returns a new set of documents.
Supply a range of page numbers to be considered for how to split the document pages.
For example if you wanted to split out the first two pages of a document then use:
[0,2]
- this supplies the page indicies to be used - page’s referenced by0
&1
will be in one document, all pages from index2
will be in the other document.- Parameters:
range –
number[]
orundefined
. Page indicies for operation. Ifundefined
then the document splits the document pages into single page document instances (one page for each document).- Returns:
PDFDocument[]
.
EXAMPLE
// split out 3 documents, the first two pages, then page three, then everything from page 4 onwards var documents = document.split([0, 2, 3])
Note
Remember page indexes are zero-indexed! i.e. Page 1 = index
0
!
- merge(sourcePDF: PDFDocument, fromPage: number = 0, toPage: number = -1, startAt: number = -1, rotate: 0 | 90 | 180 | 270 = 0, copyLinks: boolean = true, copyAnnotations: boolean = true)#
Merges two documents together with options.
- Parameters:
sourcePDF – PDFDocument. The source PDF to merge into the document instance.
fromPage –
number
. The start page, defaults to the first page of the document (0
).toPage –
number
. The end page, defaults to the last page of the document (-1)
.startAt –
number
. Where to insert thesourcePDF
pages in the document instance, defaults to the last page (-1)
.rotate –
number
. Sets rotstion of inserted pages, defaults to no rotation (0
).copyLinks –
boolean
. Whether to copy document links from thesourcePDF
or not, defaults totrue
.copyAnnotations –
boolean
. Whether to copy document annotations from thesourcePDF
or not, defaults totrue
.
EXAMPLE
// merge another document (sourcePDF) onto page 2 of our document instance document.merge(sourcePDF, 0, -1, 1);
- bake(bakeAnnots: boolean = true, bakeWidgets: boolean = true)#
Baking a document changes all the annotations and/or form fields (otherwise known as widgets) in the document into static content. It “bakes” the appearance of the annotations and fields onto the page, before removing the interactive objects so they can no longer be changed.
Effectively this removes the “annotation or “widget” type of these objects, but keeps the appearance of the objects.
- Parameters:
bakeAnnots –
boolean
Whether to bake annotations or not. Defaults totrue
.bakeWidgets –
boolean
Whether to bake widgets or not. Defaults totrue
.
- newGraftMap()#
Create a graft map on the destination document, so that objects that have already been copied can be found again. Each graft map should only be used with one source document. Make sure to create a new graft map for each source document used.
- Returns:
EXAMPLE
var graftMap = pdfDocument.newGraftMap();
- graftObject(obj: PDFObject)#
Deep copy an object into the destination document. This function will not remember previously copied objects. If you are copying several objects from the same source document using multiple calls, you should use a graft map instead.
- Parameters:
obj – PDFObject to graft.
EXAMPLE
pdfDocument.graftObject(obj);
- graftPage(to: number, srcDoc: PDFDocument, srcPage: number)#
Graft a page and its resources at the given page number from the source document to the requested page number in the document.
- Parameters:
to –
number
. The page number to insert the page before. Page numbers start at0
and-1
means at the end of the document.srcDoc – PDFDocument. Source document.
srcPage –
number
. Source page number.
EXAMPLE
This would copy the first page of the source document (
0
) to the last page (-1) of the current PDF document.pdfDocument.graftPage(-1, srcDoc, 0);
- attachFile(name: string, data: Buffer | ArrayBuffer | Uint8Array, options?: {filename?: string; creationDate?: Date; modificationDate?: Date;})#
Attach a file to a document by supplying a name and buffer of data.
- Parameters:
name –
string
. The name of the file.data –
Buffer | ArrayBuffer | Uint8Array
. Data for file.options –
{filename?: string; creationDate?: Date; modificationDate?: Date;}
. Optional metadata.filename
. Optionally supply a file name separately from the previousname
parameter. (Defaults toname
if not supplied)creationDate
. Optionally supply a JavaScriptDate
object for the creation date. (Defaults to “now”Date()
if not supplied))modificationDate
. Optionally supply a JavaScriptDate
object for the modification date. (Defaults to “now”Date()
if not supplied))
EXAMPLE
const content = "Test content"; const buffer = new Buffer(); buffer.writeLine(content); pdfDocument.attachFile("test.txt", buffer);
- deleteEmbeddedFile(filename: string)#
Delete an embedded file by name.
- Parameters:
filename –
string
. The name of the file.
EXAMPLE
pdfDocument.deleteEmbeddedFile("test.txt");
- getEmbeddedFiles()#
Returns a record of any embedded files on the PDFDocument.
- Returns:
Record<string,PDFObject>
- getEmbeddedFileParams(ref: PDFObject)#
Gets the embedded file parameters from a PDFObject reference.
- Parameters:
ref – PDFObject.
- Returns:
{filename:string, mimetype:string, size:number, creationDate:Date, modificationDate:Date}
- needsPassword()#
Returns
true
if a password is required to open a password protected PDF.- Returns:
boolean
.
EXAMPLE
var needsPassword = document.needsPassword();
- authenticate(password: string)#
Returns a bitfield value against the password authentication result.
- Parameters:
password –
string
. The password to attempt authentication with.- Returns:
number
.
Return values
Bitfield value
Description
0
Failed
1
No password needed
2
Is User password and is okay
4
Is Owner password and is okay
6
Is both User & Owner password and is okay
EXAMPLE
var auth = document.authenticate("abracadabra");
- hasPermission(permission: string)#
Returns
true
if the document has permission for the suppliedpermission
parameter.- Parameters:
permission –
string
The permission to seek for, e.g. “edit”.- Returns:
boolean
.
Permission strings
String
Description
print
Can print
edit
Can edit
copy
Can copy
annotate
Can annotate
form
Can fill out forms
accessibility
Can copy for accessibility
assemble
Can manage document pages
print-hq
Can print high-quality
EXAMPLE
var canEdit = document.hasPermission("edit");
- getMetaData(key: string)#
Return various meta data information. The common keys are:
format
,encryption
,info:ModDate
, andinfo:Title
.- Parameters:
key –
string
.- Returns:
string
.
EXAMPLE
var format = document.getMetaData("format"); var modificationDate = doc.getMetaData("info:ModDate"); var author = doc.getMetaData("info:Author");
- setMetaData(key: string, value: string)#
Set document meta data information field to a new value.
- Parameters:
key –
string
.value –
string
.
EXAMPLE
document.setMetaData("info:Author", "My Name");
- countPages()#
Count the number of pages in the document.
- Returns:
number
.
EXAMPLE
var numPages = document.countPages();
- loadOutline()#
Returns an array with the outline (also known as “table of contents” or “bookmarks”). In the array is an object for each heading with the property ‘title’, and a property ‘page’ containing the page number. If the object has a ‘down’ property, it contains an array with all the sub-headings for that entry.
- Returns:
[OutlineItem]
. An array of OutlineItem objects.
EXAMPLE
var outline = document.loadOutline();
- outlineIterator()#
Returns an OutlineIterator for the document outline.
- Returns:
EXAMPLE
var obj = document.outlineIterator();
- resolveLink(link: string | Link)#
Resolve a document internal link URI to a page index.
- Parameters:
uri –
string
| Link.- Returns:
number
.
EXAMPLE
var pageNumber = document.resolveLink(uri);
- resolveLinkDestination(uri: string)#
Resolve a document internal link URI to a link destination.
- Parameters:
uri –
string
.- Returns:
EXAMPLE
var linkDestination = document.resolveLinkDestination(uri);
- formatLinkURI(dest: LinkDest)#
Format a document internal link destination object to a URI string suitable for
createLink()
.- Parameters:
dest –
LinkDest
. Link destination.- Returns:
string
.
EXAMPLE
var uri = document.formatLinkURI({chapter:0, page:42, type:"FitV", x:0, y:0, width:100, height:50, zoom:1}); document.createLink([0,0,100,100], uri);
- getPageLabels()#
Extract the list of page label definitions. Typically used for modifications before feeding into
setPageLabels()
.This method returns an array of PageLabelRule objects.
- Returns:
PageLabelRule[]
- setPageLabels(index: number, style: string = 'D', prefix: string = '', start: number = 1)#
Sets the page label numbering for the page and all pages following it, until the next page with an attached label.
- Parameters:
index –
number
. The start page index to start labelling from.style –
string
. Can be one of the following strings:""
(none),"D"
(decimal),"R"
(roman numerals upper-case),"r"
(roman numerals lower-case),"A"
(alpha upper-case), or"a"
(alpha lower-case).prefix –
string
. Define a prefix for the labels.start –
number
The ordinal with which to start numbering.
EXAMPLE
pdfDocument.setPageLabels(0, "D", "Prefix", 1);
- setPageLabelsArray(labels: PageLabelRule[])#
Define a set of labels for your pages using this method.
- Parameters:
labels –
PageLabelRule[]
. See PageLabelRule.
EXAMPLE
const labels = [ { startpage: 0, style: "D" }, { startpage: 5, prefix: "B-" }, { startpage: 10, firstpagenum: 5 }, ]; document.setPageLabelsArray(labels);
- deletePageLabels(index: number)#
Removes any associated page label from the page.
- Parameters:
index –
number
.
EXAMPLE
pdfDocument.deletePageLabels(0);
- getPageNumbers(label: string, onlyOne: boolean = false)#
Gets the page numbers with an associated label.
- Parameters:
label –
string
. The label to search for.onlyOne –
boolean
. Set totrue
if you only want to return the first result of a found label.
- Returns:
number[]
// find all the pages labelled as "Appendix-A" let result = pdfDocument.getPageNumbers("Appendix-A");
- getTrailer()#
The trailer dictionary. This contains indirect references to the “Root” and “Info” dictionaries. See: PDF object access.
- Returns:
PDFObject. The trailer dictionary.
EXAMPLE
var dict = pdfDocument.getTrailer();
- countObjects()#
Return the number of objects in the PDF. Object number
0
is reserved, and may not be used for anything. See: PDF object access.- Returns:
number
Object count.
EXAMPLE
var num = pdfDocument.countObjects();
- createObject()#
Allocate a new numbered object in the PDF, and return an indirect reference to it. The object itself is uninitialized.
- Returns:
PDFObject. The new object.
EXAMPLE
var obj = pdfDocument.createObject();
- deleteObject(num: number | PDFObject)#
Delete the object referred to by an indirect reference or its object number.
- Parameters:
num –
number | PDFObject
.
EXAMPLE
pdfDocument.deleteObject(obj);
- saveToBuffer(options: string = '')#
Saves the document to a buffer. The options are a string of comma separated options.
- Parameters:
options –
string
.- Returns:
EXAMPLE
var buffer = pdfDocument.saveToBuffer("garbage=2,compress=yes,user-password=PASSWORD")
The options which you can use to compose your string parameter are as follows:
Name
Description
Values
Default
decompress
Decompress all streams (except compress-fonts/images).
yes
|no
no
compress
Compress all streams.
yes
|no
no
compress-fonts
Compress embedded fonts.
yes
|no
no
compress-images
Compress images.
yes
|no
no
ascii
ASCII hex encode binary streams.
yes
|no
no
pretty
Pretty-print objects with indentation.
yes
|no
no
clean
Pretty-print graphics commands in content streams.
yes
|no
no
sanitize
Sanitize graphics commands in content streams.
yes
|no
no
incremental
Write changes as incremental update.
yes
|no
no
continue-on-error
Continue saving the document even if there is an error.
yes
|no
no
garbage
Garbage collect unused objects.
yes
|compact
(and compact cross reference table.) |deduplicate
(and remove duplicate objects.)no
decrypt
Write unencrypted document.
yes
|no
no
encrypt
Write encrypted document.
rc4-40
|rc4-128
|aes-128
|aes-256
no
permissions
Document permissions to grant when encrypting.
number
, see PDF Reference 1.7 - page 123, table 3.20no
user-password
Password required to read document.
your password
owner-password
Password required to edit document.
your password
regenerate-id
Regenerate document id.
yes
|no
yes
PDF Object Access#
A PDF document contains objects, similar to those in JavaScript: arrays, dictionaries, strings, booleans, and numbers. At the root of the PDF document is the trailer object; which contains pointers to the meta data dictionary and the catalog object which contains the pages and other information.
Pointers in PDF are also called indirect references, and are of the form “32 0 R” (where 32 is the object number, 0 is the generation, and R is magic syntax). All functions in MuPDF dereference indirect references automatically.
PDF has two types of strings: /Names
and (Strings)
. All dictionary keys are names.
Some dictionaries in PDF also have attached binary data. These are called streams, and may be compressed.
Note
PDFObjects
are always bound to the document that created them. Do NOT mix and match objects from one document with another document!
- addObject(obj: any)#
Add
obj
to the PDF as a numbered object, and return an indirect reference to it.- Parameters:
obj –
any
. Object to add.- Returns:
EXAMPLE
var ref = pdfDocument.addObject(obj);
- addStream(buf: AnyBuffer, obj: any)#
Create a stream object with the contents of
buffer
, add it to the PDF, and return an indirect reference to it. Ifobject
is defined, it will be used as the stream object dictionary.- Parameters:
buf –
AnyBuffer
object.obj –
any
. The object to add the stream to.
- Returns:
EXAMPLE
var stream = pdfDocument.addStream(buffer, object);
- addRawStream(buf: AnyBuffer, obj: any)#
Create a stream object with the contents of
buffer
, add it to the PDF, and return an indirect reference to it. Ifobject
is defined, it will be used as the stream object dictionary. Thebuffer
must contain already compressed data that matches “Filter” and “DecodeParms” set in the stream object dictionary.- Parameters:
buf –
AnyBuffer
object.obj –
any
. The object to add the stream to.
- Returns:
EXAMPLE
var stream = pdfDocument.addRawStream(buffer, object);
- newBoolean(v: boolean)#
Create a new boolean object.
- Parameters:
v –
boolean
.- Returns:
EXAMPLE
var obj = pdfDocument.newBoolean(true);
- newInteger(v: number)#
Create a new integer object.
- Parameters:
v –
number
.- Returns:
EXAMPLE
var obj = pdfDocument.newInteger(1);
- newReal(v: number)#
Create a new real number object.
- Parameters:
v –
number
.- Returns:
EXAMPLE
var obj = pdfDocument.newReal(7.3);
- newString(v: string)#
Create a new string object.
- Parameters:
v –
string
.- Returns:
EXAMPLE
var obj = pdfDocument.newString("hello");
- newByteString(v: Uint8Array)#
Create a new byte string object.
- Parameters:
v –
Uint8Array
.- Returns:
EXAMPLE
var obj = pdfDocument.newByteString([21, 31]);
- newName(v: string)#
Create a new name object.
- Parameters:
v –
string
.- Returns:
EXAMPLE
var obj = pdfDocument.newName("hello");
- newIndirect(v: number)#
Create a new indirect object.
- Parameters:
v –
number
.- Returns:
EXAMPLE
var obj = pdfDocument.newIndirect(100);
- newArray(cap: number = 8)#
Create a new array object.
- Parameters:
cap –
number
. Defaults to8
.- Returns:
EXAMPLE
var obj = pdfDocument.newArray();
- newDictionary(cap: number = 8)#
Create a new dictionary object.
- Parameters:
cap –
number
. Defaults to8
.- Returns:
EXAMPLE
var obj = pdfDocument.newDictionary();
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"