PDFPage#
CONSTRUCTOR METHODS
- doc.loadPage(pno: number)#
Calling loadPage on a PDFDocument returns a PDFPage for the given page number.
- Parameters:
pno –
number
. Note: zero-indexed! - to get page1
of the document use0
here!- Returns:
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:
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
andalpha
while applying thematrix
. Rendering of annotations/widgets can be disabled. A page can be rendered for e.g. “View” or “Print” usage.- Parameters:
matrix – Matrix.
colorspace – ColorSpace.
alpha –
boolean
.renderExtra –
boolean
. Whether annotations and widgets should be rendered.usage –
string
. “View” or “Print”.box – PageBox. Default is “CropBox”.
- Returns:
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:
showExtras –
boolean
. Default is true.- Returns:
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:
value –
string
. The value of the text.point –
Point
. The Point coordinate for the text.fontName –
string
. Defaults to “Times-Roman”fontSize –
number
. 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.
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 areundefined
thenx
=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});
- insertLink(metrics: {x: number, y: number, width: number, height: number}, uri: string)#
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.destinationUri –
string
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:
type –
string
representing annotation type.- Returns:
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:
ref – PDFAnnotation | PDFWidget | Link |
string
Note
Use
getResourcesXrefObjects()
to find PDFObjectxref
keys which you may want to delete.EXAMPLE
let annots = getAnnotations(); page.delete(annots[0]);
- search(needle: string, maxHits: number = 50)#
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:
needle –
string
.maxHits –
number
. 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:
type –
CreatableAnnotationType
.metrics –
{x:number, y:number, width:number, height:number}
.author –
string
|null
. The annotation author.contents –
string
. The annotation contents. SeesetContents()
.
- Returns:
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:
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:
blackBoxes –
boolean
|number
. Whether to use black boxes at each redaction or not.imageMethod –
number
. Default isPDFPage.REDACT_IMAGE_PIXELS
.lineArtMethod –
number
. Default isPDFPage.REDACT_LINE_ART_REMOVE_IF_COVERED
.textMethod –
number
. Default isPDFPage.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();
- getLinks()#
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:
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:
r –
number
. 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.
EXAMPLE
page.setPageBox("TrimBox", [10,10, 585, 832]);
- 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. Thematrix
maps from user space points to device space pixels.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.
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.
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.
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"