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:
  • widthnumber. Width of document.

  • heightnumber. Height of document.

Returns:

PDFDocument.

EXAMPLE

let document = mupdfjs.PDFDocument.createBlankDocument()
openDocument(from: Buffer | ArrayBuffer | Uint8Array | Stream, fileType: string)#

Opens a document from a supplied Buffer, Array or Stream.

Parameters:
  • fromBuffer | ArrayBuffer | Uint8Array | Stream

  • fileTypestring. Used to denote fie type, e.g. “application/pdf”.

Returns:

PDFDocument.

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:
  • pnonumber. The page location in the document to insert the page 0 = start of document, -1 = end of document.

  • widthnumber. Width of document.

  • heightnumber. Height of document.

Returns:

PDFPage.

copyPage(pno: number, to: number = -1)#

Copys a page from one index to another in the document.

Parameters:
  • pnonumber. The page location in the document to copy the page from, 0 = start of document, -1 = end of document.

  • tonumber. 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:

indexnumber. 0 = first page of document.

deletePages(...args: any[])#

A convenience method for deleting a range of pages.

Parameters:

...argsany[].

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 the fromPage and the toPage (and including the fromPage and the toPage).

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 by 0 & 1 will be in one document, all pages from index 2 will be in the other document.

Parameters:

rangenumber[] or undefined. Page indicies for operation. If undefined 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:
  • sourcePDFPDFDocument. The source PDF to merge into the document instance.

  • fromPagenumber. The start page, defaults to the first page of the document (0).

  • toPagenumber. The end page, defaults to the last page of the document (-1).

  • startAtnumber. Where to insert the sourcePDF pages in the document instance, defaults to the last page (-1).

  • rotatenumber. Sets rotstion of inserted pages, defaults to no rotation (0).

  • copyLinksboolean. Whether to copy document links from the sourcePDF or not, defaults to true.

  • copyAnnotationsboolean. Whether to copy document annotations from the sourcePDF or not, defaults to true.

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:
  • bakeAnnotsboolean Whether to bake annotations or not. Defaults to true.

  • bakeWidgetsboolean Whether to bake widgets or not. Defaults to true.

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:

PDFGraftMap.

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:

objPDFObject 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:
  • tonumber. The page number to insert the page before. Page numbers start at 0 and -1 means at the end of the document.

  • srcDocPDFDocument. Source document.

  • srcPagenumber. 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:
  • namestring. The name of the file.

  • dataBuffer | ArrayBuffer | Uint8Array. Data for file.

  • options

    {filename?: string; creationDate?: Date; modificationDate?: Date;}. Optional metadata.

    • filename. Optionally supply a file name separately from the previous name parameter. (Defaults to name if not supplied)

    • creationDate. Optionally supply a JavaScript Date object for the creation date. (Defaults to “now” Date() if not supplied))

    • modificationDate. Optionally supply a JavaScript Date 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:

filenamestring. 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:

refPDFObject.

Returns:

{filename:string, mimetype:string, size:number, creationDate:Date, modificationDate:Date}

getEmbeddedFileContents(ref: PDFObject)#

Gets the embedded file content from a PDFObject reference.

Parameters:

refPDFObject.

Returns:

Buffer | null.

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:

passwordstring. 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 supplied permission parameter.

Parameters:

permissionstring 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, and info:Title.

Parameters:

keystring.

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:
  • keystring.

  • valuestring.

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:

OutlineIterator.

EXAMPLE

var obj = document.outlineIterator();

Resolve a document internal link URI to a page index.

Parameters:

uristring | Link.

Returns:

number.

EXAMPLE

var pageNumber = document.resolveLink(uri);
resolveLinkDestination(uri: string)#

Resolve a document internal link URI to a link destination.

Parameters:

uristring.

Returns:

Link destination.

EXAMPLE

var linkDestination = document.resolveLinkDestination(uri);
formatLinkURI(dest: LinkDest)#

Format a document internal link destination object to a URI string suitable for createLink().

Parameters:

destLinkDest. 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:
  • indexnumber. The start page index to start labelling from.

  • stylestring. 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).

  • prefixstring. Define a prefix for the labels.

  • startnumber 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:

labelsPageLabelRule[]. 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:

indexnumber.

EXAMPLE

pdfDocument.deletePageLabels(0);
getPageNumbers(label: string, onlyOne: boolean = false)#

Gets the page numbers with an associated label.

Parameters:
  • labelstring. The label to search for.

  • onlyOneboolean. Set to true 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:

numnumber | PDFObject.

EXAMPLE

pdfDocument.deleteObject(obj);
saveToBuffer(options: string = '')#

Saves the document to a buffer. The options are a string of comma separated options.

Parameters:

optionsstring.

Returns:

Buffer.

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.20

no

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:

objany. Object to add.

Returns:

PDFObject.

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. If object is defined, it will be used as the stream object dictionary.

Parameters:
  • bufAnyBuffer object.

  • objany. The object to add the stream to.

Returns:

PDFObject.

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. If object is defined, it will be used as the stream object dictionary. The buffer must contain already compressed data that matches “Filter” and “DecodeParms” set in the stream object dictionary.

Parameters:
  • bufAnyBuffer object.

  • objany. The object to add the stream to.

Returns:

PDFObject.

EXAMPLE

var stream = pdfDocument.addRawStream(buffer, object);
newNull()#

Create a new null object.

Returns:

PDFObject.

EXAMPLE

var obj = pdfDocument.newNull();
newBoolean(v: boolean)#

Create a new boolean object.

Parameters:

vboolean.

Returns:

PDFObject.

EXAMPLE

var obj = pdfDocument.newBoolean(true);
newInteger(v: number)#

Create a new integer object.

Parameters:

vnumber.

Returns:

PDFObject.

EXAMPLE

var obj = pdfDocument.newInteger(1);
newReal(v: number)#

Create a new real number object.

Parameters:

vnumber.

Returns:

PDFObject.

EXAMPLE

var obj = pdfDocument.newReal(7.3);
newString(v: string)#

Create a new string object.

Parameters:

vstring.

Returns:

PDFObject.

EXAMPLE

var obj = pdfDocument.newString("hello");
newByteString(v: Uint8Array)#

Create a new byte string object.

Parameters:

vUint8Array.

Returns:

PDFObject.

EXAMPLE

var obj = pdfDocument.newByteString([21, 31]);
newName(v: string)#

Create a new name object.

Parameters:

vstring.

Returns:

PDFObject.

EXAMPLE

var obj = pdfDocument.newName("hello");
newIndirect(v: number)#

Create a new indirect object.

Parameters:

vnumber.

Returns:

PDFObject.

EXAMPLE

var obj = pdfDocument.newIndirect(100);
newArray(cap: number = 8)#

Create a new array object.

Parameters:

capnumber. Defaults to 8.

Returns:

PDFObject.

EXAMPLE

var obj = pdfDocument.newArray();
newDictionary(cap: number = 8)#

Create a new dictionary object.

Parameters:

capnumber. Defaults to 8.

Returns:

PDFObject.

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"