PDFPage#

CONSTRUCTOR METHODS

doc.loadPage(pno: number)#

Calling loadPage on a PDFDocument returns a PDFPage for the given page number.

Parameters:

pnonumber. Note: zero-indexed! - to get page 1 of the document use 0 here!

Returns:

PDFPage.

EXAMPLE

let page = doc.loadPage(0); // returns the first page of the document

INSTANCE METHODS

getBounds()#

Returns a rectangle containing the page dimensions.

Returns:

Rect.

EXAMPLE

var rect = page.getBounds();
toStructuredText(options: string)#

Extract the text on the page into a StructuredText object.

Parameters:

options

string. A comma separated list of flags:

  • preserve-ligatures

  • preserve-whitespace

  • preserve-spans

  • preserve-images

  • inhibit-spaces

  • dehyphenate

  • structured

  • use-cid-for-unknown-unicode

  • ignore-actualtext

Returns:

StructuredText.

EXAMPLE

let sText = page.toStructuredText("preserve-whitespace,ignore-actualtext");
toPixmap(matrix: Matrix, colorspace: ColorSpace, alpha: boolean = false, showExtras: boolean = true, usage: string = 'View', box: PageBox = 'CropBox')#

Render the page into a Pixmap using the given colorspace and alpha while applying the matrix. Rendering of annotations/widgets can be disabled. A page can be rendered for e.g. “View” or “Print” usage.

Parameters:
  • matrixMatrix.

  • colorspaceColorSpace.

  • alphaboolean.

  • renderExtraboolean. Whether annotations and widgets should be rendered.

  • usagestring. “View” or “Print”.

  • boxPageBox. Default is “CropBox”.

Returns:

Pixmap.

EXAMPLE

var pixmap = pdfPage.toPixmap(mupdfjs.Matrix.identity,
                              mupdfjs.ColorSpace.DeviceRGB,
                              true,
                              false,
                              "View",
                              "CropBox");
toDisplayList(showExtras=true)#

Record the contents on the page into a DisplayList.

If showExtras is true then the operation will include any page annotations and/or widgets.

Parameters:

showExtrasboolean. Default is true.

Returns:

DisplayList.

EXAMPLE

var displayList = page.toDisplayList();
insertText(value: string, point: Point, fontName: string = 'Times-Roman', fontSize: number = 18, graphics: {strokeColor: Color, fillColor: Color, strokeThickness: number} = {strokeColor: [0, 0, 0, 1], fillColor: [0, 0, 0, 1], strokeThickness: 1})#

Inserts text onto a page at the given point along with styling options.

Parameters:
  • valuestring. The value of the text.

  • pointPoint. The Point coordinate for the text.

  • fontNamestring. Defaults to “Times-Roman”

  • fontSizenumber. Font size, default is 18 points.

  • graphics

    {strokeColor:Color, fillColor:Color, strokeThickness:number}. An object with three keys to set the graphics styling for the text.

    • strokeColor. Color for the color of the text border (or stroke).

    • fillColor. Color for the color of the text fill (or body)

    • strokeThickness. number. 0 or above to set the stroke thickness in points. Floating point numbers are accepted.

EXAMPLE

mupdfJSPage.insertText("HELLO WORLD!",
               [0,0],
               "Times-Roman",
               65,
               {strokeColor:[0,0,0,1], fillColor:[1,0,0,0.75], strokeThickness:1.5});
insertImage(data: {image:Image, name:string}, metrics: {x?:number, y?:number, width?:number, height?:number} = {x:0, y:0, width:0, height:0})#

Inserts an image onto a page with a given name and within the given rectangle.

Parameters:
  • data{image:Image, name:string}. Object containing an Image and the name for the image (note this should ideally be unique for the page).

  • metrics{x?:number, y?:number, width?:number, height?:number}. An optional object used to define the position and size for the image. If these values are undefined then x = 0, y = 0, width = inherent image width, height = inherent image height.

EXAMPLE

const imageData = fs.readFileSync("logo.png"));
let logo:mupdfjs.Image = new mupdfjs.Image(imageData);
mupdfJSPage.insertImage({image:logo, name:"MyLogo"},
                        {x:0, y:0, width:200, height:200});

Create a new link with the supplied metrics for the page, linking to the destination URI string.

To create links to other pages within the document see the formatLinkURI() method.

Parameters:
  • metrics{x: number, y: number, width: number, height: number}. Object containing the link metrics.

  • destinationUristring containing URI.

Returns:

Link.

EXAMPLE

// create a link to an external URL
var link = page.insertLink({0,0,100,100}, "https://example.com");

// create a link to another page in the document
var link = page.insertLink({0,0,100,100}, "#page=1&view=FitV,0");
createAnnotation(type: string)#

Create a new blank annotation of a given type.

Parameters:

typestring representing annotation type.

Returns:

PDFAnnotation.

EXAMPLE

var annot = pdfPage.createAnnotation("Text");
delete(ref: PDFAnnotation | PDFWidget | Link | string)#

Deletes a PDFAnnotation, PDFWidget, Link instance or a PDF PDFObject by xref key.

Parameters:

refPDFAnnotation | PDFWidget | Link | string

Note

Use getResourcesXrefObjects() to find PDFObject xref keys which you may want to delete.

EXAMPLE

let annots = getAnnotations();
page.delete(annots[0]);

Search the page text for all instances of the needle value, and return an array of search hits.

Each search hit is an array of Quadpoints corresponding to all characters in the search hit.

Parameters:
  • needlestring.

  • maxHitsnumber. Defaults to 50 unless otherwise specified.

Returns:

Quad[][].

EXAMPLE

let results = page.search("my search phrase");

Note

The array contents are [ulx, uly, urx, ury, llx, lly, lrx, lry] for each result. These sets of numbers are known as quadpoints or “Quads” in the PDF specification.

update()#

Loop through all annotations of the page and update them. Returns true if re-rendering is needed because at least one annotation was changed (due to either events or JavaScript actions or annotation editing).

EXAMPLE

pdfPage.update();
addAnnotation(type: CreatableAnnotationType, metrics: {x:number, y:number, width:number, height:number}, author?:string, contents?:string)#

Creates an annotation of your choice from the set in CreatableAnnotationType at a location on the page defined by the metrics.

This method also has options for defining the author and contents of the annotation.

Parameters:
  • typeCreatableAnnotationType.

  • metrics{x:number, y:number, width:number, height:number}.

  • authorstring | null. The annotation author.

  • contentsstring. The annotation contents. See setContents().

Returns:

PDFAnnotation.

EXAMPLE

let myNote = page.addAnnotation("Text", {x:100, y:200, width:300, height:50}, null, "Hello World!");
addRedaction(metrics: {x: number, y: number, width: number, height: number})#

Creates a redaction annotation at a location on the page defined by the metrics.

Parameters:

metrics{x:number, y:number, width:number, height:number}.

Returns:

PDFAnnotation.

EXAMPLE

let redactionAnnotation = page.addRedaction({x:100, y:200, width:300, height:50})
applyRedactions(blackBoxes: boolean | number = true, imageMethod: number = PDFPage.REDACT_IMAGE_PIXELS, lineArtMethod: number = PDFPage.REDACT_LINE_ART_REMOVE_IF_COVERED, textMethod: number = PDFPage.REDACT_TEXT_REMOVE)#

Applies redactions to the page.

Parameters:
  • blackBoxesboolean | number. Whether to use black boxes at each redaction or not.

  • imageMethodnumber. Default is PDFPage.REDACT_IMAGE_PIXELS.

  • lineArtMethodnumber. Default is PDFPage.REDACT_LINE_ART_REMOVE_IF_COVERED.

  • textMethodnumber. Default is PDFPage.REDACT_TEXT_REMOVE.

Image redaction options

  • PDFPage.REDACT_IMAGE_NONE for no image redactions.

  • PDFPage.REDACT_IMAGE_REMOVE to redact entire images.

  • PDFPage.REDACT_IMAGE_PIXELS for redacting just the covered pixels.

  • PDFPage.REDACT_IMAGE_UNLESS_INVISIBLE only redact visible images.

Line Art redaction options

  • PDFPage.REDACT_LINE_ART_NONE for no line art redactions.

  • PDFPage.REDACT_LINE_ART_REMOVE_IF_COVERED redacts line art if covered.

  • PDFPage.REDACT_LINE_ART_REMOVE_IF_TOUCHED redacts line art if touched.

Text redaction options

  • PDFPage.REDACT_TEXT_REMOVE to redact text.

  • PDFPage.REDACT_TEXT_NONE for no text redaction.

Note

Redactions are secure as they remove the affected content completely.

EXAMPLE

pdfPage.applyRedactions(true, mupdfjs.PDFPage.REDACT_IMAGE_REMOVE);
getText()#

Returns the unstyled, plain text for a page as a string.

Returns:

string.

EXAMPLE

let text = pdfPage.getText();
getAnnotations()#

Returns an array of all annotations on the page.

Returns:

PDFAnnotation[].

EXAMPLE

let annots = pdfPage.getAnnotations();
getWidgets()#

Returns an array of all widgets on the page.

Returns:

PDFWidget[].

EXAMPLE

let widgets = pdfPage.getWidgets();
getImages()#

Returns an array of the page’s images along with their bounding box and transform matrix.

Returns:

{bbox:Rect, matrix:Matrix, image:Image}[].

let images = page.getImages();

Returns an array of all links on the page.

Returns:

Link[].

EXAMPLE

let links = page.getLinks();
getObject()#

Get the underlying PDFObject for a PDFPage.

Returns:

PDFObject.

EXAMPLE

let obj = page.getObject();
getResourcesXrefObjects()#

Returns an array with the key/value pairs for the page resources object.

Returns:

{key:string | number, value:string}[].

EXAMPLE

let xrefObjs = getResourcesXrefObjects();
for (var obj in xrefObjs) {
    console.log(xrefObjs[obj])
}
rotate(r: number)#

Rotating a page allows for 90 increment rotations on a page.

If you send a rotation value which is not one of postive or negative 0, 90, 180, 270 then this method will do nothing.

Parameters:

rnumber. The rotation value to apply to the page.

EXAMPLE

// rotate a page 90 degrees anti-clockwise
page.rotate(-90)

Note

Positive rotation values are clockwise, negative are anti-clockwise.

setPageBox(box: PageBox, rect: Rect)#

Sets the type of box required for the page.

Parameters:

EXAMPLE

page.setPageBox("TrimBox", [10,10, 585, 832]);
setTrimBox(rect: Rect)#

Convenience method for setting the trim box.

Parameters:

rectRect.

setMediaBox(rect: Rect)#

Convenience method for setting the media box.

Parameters:

rectRect.

setCropBox(rect: Rect)#

Convenience method for setting the crop box.

Parameters:

rectRect.

setArtBox(rect: Rect)#

Convenience method for setting the art box.

Parameters:

rectRect.

setBleedBox(rect: Rect)#

Convenience method for setting the bleed box.

Parameters:

rectRect.

run(device: Device, matrix: Matrix)#

Calls device functions for all the contents on the page, using the specified transform matrix. The device can be one of the built-in devices or a JavaScript object with methods for the device calls. The matrix maps from user space points to device space pixels.

Parameters:

EXAMPLE

page.run(device, mupdf.Matrix.identity);
runPageContents(device: Device, matrix: Matrix)#

This is the same as the run method above but it only considers the page itself and omits annotations and widgets.

Parameters:

EXAMPLE

page.runPageContents(device, mupdfjs.Matrix.identity);
runPageAnnots(device: Device, matrix: Matrix)#

This is the same as the run method above but it only considers the page annotations.

Parameters:

EXAMPLE

page.runPageAnnots(device, mupdfjs.Matrix.identity);
runPageWidgets(device: Device, matrix: Matrix)#

This is the same as the run method above but it only considers the page widgets.

Parameters:

EXAMPLE

page.runPageWidgets(device, mupdfjs.Matrix.identity);
getLabel()#

Returns the page number as a string using the numbering scheme of the document.

Returns:

string.

EXAMPLE

var label = page.getLabel();

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"