happypdf / PDFPage
Class: PDFPage
Defined in: src/api/PDFPage.ts:70
Represents a single page of a [[PDFDocument]].
Properties
doc
readonly doc: PDFDocument;Defined in: src/api/PDFPage.ts:109
The document to which this page belongs.
node
readonly node: PDFPageLeaf;Defined in: src/api/PDFPage.ts:103
The low-level PDFDictionary wrapped by this page.
ref
readonly ref: PDFRef;Defined in: src/api/PDFPage.ts:106
The unique reference assigned to this page within the document.
Methods
create()
static create(doc): PDFPage;Defined in: src/api/PDFPage.ts:94
> NOTE: You probably don't want to call this method directly. Instead, > consider using the [[PDFDocument.addPage]] and [[PDFDocument.insertPage]] > methods, which can create instances of [[PDFPage]] for you.
Create an instance of [[PDFPage]].
Parameters
| Parameter | Type | Description |
|---|---|---|
doc | PDFDocument | The document to which the page will belong. |
Returns
PDFPage
drawCircle()
drawCircle(options?): void;Defined in: src/api/PDFPage.ts:1658
Draw a circle on this page. For example:
import { grayscale, rgb } from 'pdf-lib'
page.drawCircle({
x: 200,
y: 150,
size: 100,
borderWidth: 5,
borderColor: grayscale(0.5),
color: rgb(0.75, 0.2, 0.2),
opacity: 0.5,
borderOpacity: 0.75,
})Parameters
| Parameter | Type | Description |
|---|---|---|
options | PDFPageDrawCircleOptions | The options to be used when drawing the ellipse. |
Returns
void
drawEllipse()
drawEllipse(options?): void;Defined in: src/api/PDFPage.ts:1579
Draw an ellipse on this page. For example:
import { grayscale, rgb } from 'pdf-lib'
page.drawEllipse({
x: 200,
y: 75,
xScale: 100,
yScale: 50,
borderWidth: 5,
borderColor: grayscale(0.5),
color: rgb(0.75, 0.2, 0.2),
opacity: 0.5,
borderOpacity: 0.75,
})Parameters
| Parameter | Type | Description |
|---|---|---|
options | PDFPageDrawEllipseOptions | The options to be used when drawing the ellipse. |
Returns
void
drawImage()
drawImage(image, options?): void;Defined in: src/api/PDFPage.ts:1155
Draw an image on this page. For example:
import { degrees } from 'pdf-lib'
const jpgUrl = 'https://pdf-lib.js.org/assets/cat_riding_unicorn.jpg'
const jpgImageBytes = await fetch(jpgUrl).then((res) => res.arrayBuffer())
const jpgImage = await pdfDoc.embedJpg(jpgImageBytes)
const jpgDims = jpgImage.scale(0.5)
const page = pdfDoc.addPage()
page.drawImage(jpgImage, {
x: 25,
y: 25,
width: jpgDims.width,
height: jpgDims.height,
rotate: degrees(30),
opacity: 0.75,
})Parameters
| Parameter | Type | Description |
|---|---|---|
image | PDFImage | The image to be drawn. |
options | PDFPageDrawImageOptions | The options to be used when drawing the image. |
Returns
void
drawLine()
drawLine(options): void;Defined in: src/api/PDFPage.ts:1398
Draw a line on this page. For example:
import { rgb } from 'pdf-lib'
page.drawLine({
start: { x: 25, y: 75 },
end: { x: 125, y: 175 },
thickness: 2,
color: rgb(0.75, 0.2, 0.2),
opacity: 0.75,
})Parameters
| Parameter | Type | Description |
|---|---|---|
options | PDFPageDrawLineOptions | The options to be used when drawing the line. |
Returns
void
drawPage()
drawPage(embeddedPage, options?): void;Defined in: src/api/PDFPage.ts:1224
Draw an embedded PDF page on this page. For example:
import { degrees } from 'pdf-lib'
const pdfDoc = await PDFDocument.create()
const page = pdfDoc.addPage()
const sourcePdfUrl = 'https://pdf-lib.js.org/assets/with_large_page_count.pdf'
const sourcePdf = await fetch(sourcePdfUrl).then((res) => res.arrayBuffer())
// Embed page 74 from the PDF
const [embeddedPage] = await pdfDoc.embedPdf(sourcePdf, 73)
page.drawPage(embeddedPage, {
x: 250,
y: 200,
xScale: 0.5,
yScale: 0.5,
rotate: degrees(30),
opacity: 0.75,
})The options argument accepts both width/height and xScale/yScale as options. Since each of these options defines the size of the drawn page, if both options are given, width and height take precedence and the corresponding scale variants are ignored.
Parameters
| Parameter | Type | Description |
|---|---|---|
embeddedPage | PDFEmbeddedPage | The embedded page to be drawn. |
options | PDFPageDrawPageOptions | The options to be used when drawing the embedded page. |
Returns
void
drawRectangle()
drawRectangle(options?): void;Defined in: src/api/PDFPage.ts:1465
Draw a rectangle on this page. For example:
import { degrees, grayscale, rgb } from 'pdf-lib'
page.drawRectangle({
x: 25,
y: 75,
rx: 5, // This is the border radius
ry: 5,
width: 250,
height: 75,
rotate: degrees(-15),
borderWidth: 5,
borderColor: grayscale(0.5),
color: rgb(0.75, 0.2, 0.2),
opacity: 0.5,
borderOpacity: 0.75,
})Parameters
| Parameter | Type | Description |
|---|---|---|
options | PDFPageDrawRectangleOptions | The options to be used when drawing the rectangle. |
Returns
void
drawSquare()
drawSquare(options?): void;Defined in: src/api/PDFPage.ts:1554
Draw a square on this page. For example:
import { degrees, grayscale, rgb } from 'pdf-lib'
page.drawSquare({
x: 25,
y: 75,
size: 100,
rotate: degrees(-15),
borderWidth: 5,
borderColor: grayscale(0.5),
color: rgb(0.75, 0.2, 0.2),
opacity: 0.5,
borderOpacity: 0.75,
})Parameters
| Parameter | Type | Description |
|---|---|---|
options | PDFPageDrawSquareOptions | The options to be used when drawing the square. |
Returns
void
drawSvg()
drawSvg(svg, options?): void;Defined in: src/api/PDFPage.ts:1697
Draw an SVG on this page. For example:
const svg = '<svg><path d="M 0,20 L 100,160 Q 130,200 150,120 C 190,-40 200,200 300,150 L 400,90"></path></svg>'
// Draw svg
page.drawSvg(svg, { x: 25, y: 75 })If the svg contains images, you must call embedSvg from the document that contains that page first:
const svg = '<svg><image href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII="/></svg>'
// Draw svg
const pdfSvg = await pdfDoc.embedSvg(svg)
page.drawSvg(pdfSvg, { x: 25, y: 75 })Parameters
| Parameter | Type | Description |
|---|---|---|
svg | string | PDFSvg | The SVG to be drawn. |
options | PDFPageDrawSVGElementOptions | The options to be used when drawing the SVG. |
Returns
void
drawSvgPath()
drawSvgPath(path, options?): void;Defined in: src/api/PDFPage.ts:1320
Draw an SVG path on this page. For example:
import { rgb } from 'pdf-lib'
const svgPath = 'M 0,20 L 100,160 Q 130,200 150,120 C 190,-40 200,200 300,150 L 400,90'
// Draw path as black line
page.drawSvgPath(svgPath, { x: 25, y: 75 })
// Change border style and opacity
page.drawSvgPath(svgPath, {
x: 25,
y: 275,
borderColor: rgb(0.5, 0.5, 0.5),
borderWidth: 2,
borderOpacity: 0.75,
})
// Set fill color and opacity
page.drawSvgPath(svgPath, {
x: 25,
y: 475,
color: rgb(1.0, 0, 0),
opacity: 0.75,
})
// Draw 50% of original size
page.drawSvgPath(svgPath, {
x: 25,
y: 675,
scale: 0.5,
})Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The SVG path to be drawn. |
options | PDFPageDrawSVGOptions | The options to be used when drawing the SVG path. |
Returns
void
drawText()
drawText(text, options?): void;Defined in: src/api/PDFPage.ts:1037
Draw one or more lines of text on this page. For example:
import { StandardFonts, rgb } from 'pdf-lib'
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica)
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman)
const page = pdfDoc.addPage()
page.setFont(helveticaFont)
page.moveTo(5, 200)
page.drawText('The Life of an Egg', { size: 36 })
page.moveDown(36)
page.drawText('An Epic Tale of Woe', { size: 30 })
page.drawText(
`Humpty Dumpty sat on a wall \n` +
`Humpty Dumpty had a great fall; \n` +
`All the king's horses and all the king's men \n` +
`Couldn't put Humpty together again. \n`,
{
x: 25,
y: 100,
font: timesRomanFont,
size: 24,
color: rgb(1, 0, 0),
lineHeight: 24,
opacity: 0.75,
},
)Passing a maxWidth wraps the text, and align controls how the wrapped lines sit within that width:
page.drawText(paragraph, {
x: 25,
y: 700,
maxWidth: 400,
lineHeight: 16,
align: 'justify',
})Word boundaries come from Intl.Segmenter, so text in scripts that do not separate words with spaces — Khmer, Thai, Lao, Japanese — wraps and justifies at real word boundaries.
Parameters
| Parameter | Type | Description |
|---|---|---|
text | string | The text to be drawn. |
options | PDFPageDrawTextOptions | The options to be used when drawing the text. |
Returns
void
extractContents()
extractContents(): PdfAsset[];Defined in: src/api/PDFPage.ts:460
Extract typed content assets (text runs and images) from this page.
Parses the page content streams, decodes show-text operators via each font's ToUnicode CMap (or WinAnsi for standard fonts), converts painted Image XObjects into JPEG or PNG bytes, and approximates vector paths as SVG (kind: 'graphics'). Form XObjects are traversed recursively. Each text asset includes page-space x/y, fontSize, and fontFamily. Path-outlined text is not extracted as text.
For example:
const assets = page.extractContents()
for (const asset of assets) {
if (asset.kind === 'text') console.log(asset.getText())
if (asset.kind === 'image') console.log(asset.mimeType, asset.width)
if (asset.kind === 'graphics') console.log(asset.getSvg())
}Returns
PdfAsset[]
Extracted text, image, and graphics assets in paint order.
getArtBox()
getArtBox(): {
height: number;
width: number;
x: number;
y: number;
};Defined in: src/api/PDFPage.ts:564
Get the rectangle defining this page's ArtBox. For example:
const { x, y, width, height } = page.getArtBox()The ArtBox of a page defines the extent of the page's meaningful content (including potential white space).
The ArtBox's default value is the page's CropBox.
Returns
{
height: number;
width: number;
x: number;
y: number;
}An object defining the lower left corner of the ArtBox and its width & height.
height
height: number;width
width: number;x
x: number;y
y: number;getBleedBox()
getBleedBox(): {
height: number;
width: number;
x: number;
y: number;
};Defined in: src/api/PDFPage.ts:524
Get the rectangle defining this page's BleedBox. For example:
const { x, y, width, height } = page.getBleedBox()The BleedBox of a page defines the region to which the contents of the page shall be clipped when output in a production environment. This may include any extra bleed area needed to accommodate the physical limitations of cutting, folding, and trimming equipment. The actual printed page may include printing marks that fall outside the BleedBox.
The BleedBox's default value is the page's CropBox.
Returns
{
height: number;
width: number;
x: number;
y: number;
}An object defining the lower left corner of the BleedBox and its width & height.
height
height: number;width
width: number;x
x: number;y
y: number;getCropBox()
getCropBox(): {
height: number;
width: number;
x: number;
y: number;
};Defined in: src/api/PDFPage.ts:502
Get the rectangle defining this page's CropBox. For example:
const { x, y, width, height } = page.getCropBox()The CropBox of a page defines the region to which the contents of the page shall be clipped when displayed or printed. Unlike the other boxes, the CropBox does not necessarily represent the physical page geometry. It merely imposes clipping on the page contents.
The CropBox's default value is the page's MediaBox.
Returns
{
height: number;
width: number;
x: number;
y: number;
}An object defining the lower left corner of the CropBox and its width & height.
height
height: number;width
width: number;x
x: number;y
y: number;getFont()
getFont(): [PDFFont, PDFName];Defined in: src/api/PDFPage.ts:1718
Returns
getHeight()
getHeight(): number;Defined in: src/api/PDFPage.ts:434
Get this page's height. For example:
const height = page.getHeight()This method uses [[PDFPage.getSize]] to obtain the page's size.
Returns
number
The height of the page.
getJavaScriptActions()
getJavaScriptActions():
| JavaScriptActionMap
| undefined;Defined in: src/api/PDFPage.ts:755
Get the JavaScript actions associated with this page. Returns a map of action types to JavaScript actions. For example:
const page = pdfDoc.getPage(0)
const actions = page.getJavaScriptActions()
if (actions && actions.pageOpen) {
console.log('Page open script:', actions.pageOpen.getScript())
}
if (actions && actions.pageClose) {
console.log('Page close script:', actions.pageClose.getScript())
}Returns
| JavaScriptActionMap | undefined
A map of JavaScript actions for this page, or undefined if none exist.
getMediaBox()
getMediaBox(): {
height: number;
width: number;
x: number;
y: number;
};Defined in: src/api/PDFPage.ts:481
Get the rectangle defining this page's MediaBox. For example:
const { x, y, width, height } = page.getMediaBox()The MediaBox of a page defines the boundaries of the physical medium on which the page is to be displayed/printed. It may include extended area surrounding the page content for bleed marks, printing marks, etc... It may also include areas close to the edges of the medium that cannot be marked because of physical limitations of the output device. Content falling outside this boundary may safely be discarded without affecting the meaning of the PDF file.
Returns
{
height: number;
width: number;
x: number;
y: number;
}An object defining the lower left corner of the MediaBox and its width & height.
height
height: number;width
width: number;x
x: number;y
y: number;getPosition()
getPosition(): {
x: number;
y: number;
};Defined in: src/api/PDFPage.ts:829
Get the default position of this page. For example:
const { x, y } = page.getPosition()Returns
{
x: number;
y: number;
}The default position of the page.
x
x: number;y
y: number;getRotation()
getRotation(): Rotation;Defined in: src/api/PDFPage.ts:158
Get this page's rotation angle in degrees. For example:
const rotationAngle = page.getRotation().angle;Returns
The rotation angle of the page in degrees (always a multiple of 90 degrees).
getSize()
getSize(): {
height: number;
width: number;
};Defined in: src/api/PDFPage.ts:405
Get this page's width and height. For example:
const { width, height } = page.getSize()This method uses [[PDFPage.getMediaBox]] to obtain the page's width and height.
Returns
{
height: number;
width: number;
}The width and height of the page.
height
height: number;width
width: number;getTrimBox()
getTrimBox(): {
height: number;
width: number;
x: number;
y: number;
};Defined in: src/api/PDFPage.ts:545
Get the rectangle defining this page's TrimBox. For example:
const { x, y, width, height } = page.getTrimBox()The TrimBox of a page defines the intended dimensions of the finished page after trimming. It may be smaller than the MediaBox to allow for production-related content, such as printing instructions, cut marks, or color bars.
The TrimBox's default value is the page's CropBox.
Returns
{
height: number;
width: number;
x: number;
y: number;
}An object defining the lower left corner of the TrimBox and its width & height.
height
height: number;width
width: number;x
x: number;y
y: number;getWidth()
getWidth(): number;Defined in: src/api/PDFPage.ts:420
Get this page's width. For example:
const width = page.getWidth()This method uses [[PDFPage.getSize]] to obtain the page's size.
Returns
number
The width of the page.
getX()
getX(): number;Defined in: src/api/PDFPage.ts:840
Get the default x coordinate of this page. For example:
const x = page.getX()Returns
number
The default x coordinate of the page.
getY()
getY(): number;Defined in: src/api/PDFPage.ts:851
Get the default y coordinate of this page. For example:
const y = page.getY()Returns
number
The default y coordinate of the page.
moveDown()
moveDown(yDecrease): void;Defined in: src/api/PDFPage.ts:890
Change the default position of this page to be further down the y-axis. For example:
page.moveTo(50, 50)
page.drawText('I will be drawn at (50, 50)')
page.moveDown(10)
page.drawText('I will be drawn at (50, 40)')Parameters
| Parameter | Type | Description |
|---|---|---|
yDecrease | number | The amount by which the page's default position along the y-axis should be decreased. |
Returns
void
moveLeft()
moveLeft(xDecrease): void;Defined in: src/api/PDFPage.ts:926
Change the default position of this page to be further left on the x-axis. For example:
page.moveTo(50, 50)
page.drawText('I will be drawn at (50, 50)')
page.moveLeft(10)
page.drawText('I will be drawn at (40, 50)')Parameters
| Parameter | Type | Description |
|---|---|---|
xDecrease | number | The amount by which the page's default position along the x-axis should be decreased. |
Returns
void
moveRight()
moveRight(xIncrease): void;Defined in: src/api/PDFPage.ts:944
Change the default position of this page to be further right on the y-axis. For example:
page.moveTo(50, 50)
page.drawText('I will be drawn at (50, 50)')
page.moveRight(10)
page.drawText('I will be drawn at (60, 50)')Parameters
| Parameter | Type | Description |
|---|---|---|
xIncrease | number | The amount by which the page's default position along the x-axis should be increased. |
Returns
void
moveTo()
moveTo(x, y): void;Defined in: src/api/PDFPage.ts:870
Change the default position of this page. For example:
page.moveTo(0, 0)
page.drawText('I will be drawn at the origin')
page.moveTo(0, 25)
page.drawText('I will be drawn 25 units up')
page.moveTo(25, 25)
page.drawText('I will be drawn 25 units up and 25 units to the right')Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | The new default position on the x-axis for this page. |
y | number | The new default position on the y-axis for this page. |
Returns
void
moveUp()
moveUp(yIncrease): void;Defined in: src/api/PDFPage.ts:908
Change the default position of this page to be further up the y-axis. For example:
page.moveTo(50, 50)
page.drawText('I will be drawn at (50, 50)')
page.moveUp(10)
page.drawText('I will be drawn at (50, 60)')Parameters
| Parameter | Type | Description |
|---|---|---|
yIncrease | number | The amount by which the page's default position along the y-axis should be increased. |
Returns
void
of()
static of(
leafNode,
ref,
doc): PDFPage;Defined in: src/api/PDFPage.ts:82
> NOTE: You probably don't want to call this method directly. Instead, > consider using the [[PDFDocument.addPage]] and [[PDFDocument.insertPage]] > methods, which can create instances of [[PDFPage]] for you.
Create an instance of [[PDFPage]] from an existing leaf node.
Parameters
| Parameter | Type | Description |
|---|---|---|
leafNode | PDFPageLeaf | The leaf node to be wrapped. |
ref | PDFRef | The unique reference for the page. |
doc | PDFDocument | The document to which the page will belong. |
Returns
PDFPage
pushOperators()
pushOperators(...operator): void;Defined in: src/api/PDFPage.ts:978
Push one or more operators to the end of this page's current content stream. For example:
import {
pushGraphicsState,
moveTo,
lineTo,
closePath,
setFillingColor,
rgb,
fill,
popGraphicsState,
} from 'pdf-lib'
// Draw a green triangle in the lower-left corner of the page
page.pushOperators(
pushGraphicsState(),
moveTo(0, 0),
lineTo(100, 0),
lineTo(50, 100),
closePath(),
setFillingColor(rgb(0.0, 1.0, 0.0)),
fill(),
popGraphicsState(),
)Parameters
| Parameter | Type | Description |
|---|---|---|
...operator | PDFOperator[] | The operators to be pushed. |
Returns
void
resetPosition()
resetPosition(): void;Defined in: src/api/PDFPage.ts:706
Reset the x and y coordinates of this page to (0, 0). This operation is often useful after calling [[translateContent]]. For example:
// Shift the page's contents up and to the right by 50 units
page.translateContent(50, 50)
// This text will shifted - it will be drawn at (50, 50)
page.drawText('I am shifted')
// Move back to (0, 0)
page.resetPosition()
// This text will not be shifted - it will be drawn at (0, 0)
page.drawText('I am not shifted')Returns
void
scale()
scale(x, y): void;Defined in: src/api/PDFPage.ts:618
Scale the size, content, and annotations of a page.
For example:
page.scale(0.5, 0.5);Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | The factor by which the width for the page should be scaled (e.g. 0.5 is 50%). |
y | number | The factor by which the height for the page should be scaled (e.g. 2.0 is 200%). |
Returns
void
scaleAnnotations()
scaleAnnotations(x, y): void;Defined in: src/api/PDFPage.ts:676
Scale the annotations of a page. This is useful if you want to scale a page with comments or other annotations.
// Scale the content of the page down by 50% in x and y
page.scaleContent(0.5, 0.5);
// Scale the content of the page down by 50% in x and y
page.scaleAnnotations(0.5, 0.5);See also: [[scaleContent]]
Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | The factor by which the x-axis for the annotations should be scaled (e.g. 0.5 is 50%). |
y | number | The factor by which the y-axis for the annotations should be scaled (e.g. 2.0 is 200%). |
Returns
void
scaleContent()
scaleContent(x, y): void;Defined in: src/api/PDFPage.ts:644
Scale the content of a page. This is useful after resizing an existing page. This scales only the content, not the annotations.
For example:
// Bisect the size of the page
page.setSize(page.getWidth() / 2, page.getHeight() / 2);
// Scale the content of the page down by 50% in x and y
page.scaleContent(0.5, 0.5);See also: [[scaleAnnotations]]
Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | The factor by which the x-axis for the content should be scaled (e.g. 0.5 is 50%). |
y | number | The factor by which the y-axis for the content should be scaled (e.g. 2.0 is 200%). |
Returns
void
setArtBox()
setArtBox(
x,
y,
width,
height): void;Defined in: src/api/PDFPage.ts:385
Set the ArtBox of this page. For example:
const artBox = page.getArtBox()
page.setArtBox(0, 0, 250, 500)
page.setArtBox(artBox.x, artBox.y, 50, 100)
page.setArtBox(15, 5, artBox.width - 50, artBox.height - 100)See [[PDFPage.getArtBox]] for details about what the ArtBox represents.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | The x coordinate of the lower left corner of the new ArtBox. |
y | number | The y coordinate of the lower left corner of the new ArtBox. |
width | number | The width of the new ArtBox. |
height | number | The height of the new ArtBox. |
Returns
void
setBleedBox()
setBleedBox(
x,
y,
width,
height): void;Defined in: src/api/PDFPage.ts:333
Set the BleedBox of this page. For example:
const bleedBox = page.getBleedBox()
page.setBleedBox(0, 0, 250, 500)
page.setBleedBox(bleedBox.x, bleedBox.y, 50, 100)
page.setBleedBox(15, 5, bleedBox.width - 50, bleedBox.height - 100)See [[PDFPage.getBleedBox]] for details about what the BleedBox represents.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | The x coordinate of the lower left corner of the new BleedBox. |
y | number | The y coordinate of the lower left corner of the new BleedBox. |
width | number | The width of the new BleedBox. |
height | number | The height of the new BleedBox. |
Returns
void
setCropBox()
setCropBox(
x,
y,
width,
height): void;Defined in: src/api/PDFPage.ts:307
Set the CropBox of this page. For example:
const cropBox = page.getCropBox()
page.setCropBox(0, 0, 250, 500)
page.setCropBox(cropBox.x, cropBox.y, 50, 100)
page.setCropBox(15, 5, cropBox.width - 50, cropBox.height - 100)See [[PDFPage.getCropBox]] for details about what the CropBox represents.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | The x coordinate of the lower left corner of the new CropBox. |
y | number | The y coordinate of the lower left corner of the new CropBox. |
width | number | The width of the new CropBox. |
height | number | The height of the new CropBox. |
Returns
void
setFont()
setFont(font): void;Defined in: src/api/PDFPage.ts:732
Choose a default font for this page. The default font will be used whenever text is drawn on this page and no font is specified. For example:
import { StandardFonts } from 'pdf-lib'
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman)
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica)
const courierFont = await pdfDoc.embedFont(StandardFonts.Courier)
const page = pdfDoc.addPage()
page.setFont(helveticaFont)
page.drawText('I will be drawn in Helvetica')
page.setFont(timesRomanFont)
page.drawText('I will be drawn in Courier', { font: courierFont })Parameters
| Parameter | Type | Description |
|---|---|---|
font | PDFFont | The default font to be used when drawing text on this page. |
Returns
void
setFontColor()
setFontColor(fontColor): void;Defined in: src/api/PDFPage.ts:796
Choose a default font color for this page. The default font color will be used whenever text is drawn on this page and no font color is specified. For example:
import { rgb, cmyk, grayscale } from 'pdf-lib'
page.setFontColor(rgb(0.97, 0.02, 0.97))
page.drawText('I will be drawn in pink')
page.setFontColor(cmyk(0.4, 0.7, 0.39, 0.15))
page.drawText('I will be drawn in gray', { color: grayscale(0.5) })Parameters
| Parameter | Type | Description |
|---|---|---|
fontColor | Color | The default font color to be used when drawing text on this page. |
Returns
void
setFontSize()
setFontSize(fontSize): void;Defined in: src/api/PDFPage.ts:775
Choose a default font size for this page. The default font size will be used whenever text is drawn on this page and no font size is specified. For example:
page.setFontSize(12)
page.drawText('I will be drawn in size 12')
page.setFontSize(36)
page.drawText('I will be drawn in size 24', { fontSize: 24 })Parameters
| Parameter | Type | Description |
|---|---|---|
fontSize | number | The default font size to be used when drawing text on this page. |
Returns
void
setHeight()
setHeight(height): void;Defined in: src/api/PDFPage.ts:259
Resize this page by increasing or decreasing its height. For example:
page.setHeight(500)
page.setHeight(page.getWidth() + 100)
page.setHeight(page.getWidth() - 100)This method uses [[PDFPage.setSize]] to set the page's height.
Parameters
| Parameter | Type | Description |
|---|---|---|
height | number | The new height of the page. |
Returns
void
setLineHeight()
setLineHeight(lineHeight): void;Defined in: src/api/PDFPage.ts:817
Choose a default line height for this page. The default line height will be used whenever text is drawn on this page and no line height is specified. For example:
page.setLineHeight(12);
page.drawText('These lines will be vertically \n separated by 12 units')
page.setLineHeight(36);
page.drawText('These lines will be vertically \n separated by 24 units', {
lineHeight: 24
})Parameters
| Parameter | Type | Description |
|---|---|---|
lineHeight | number | The default line height to be used when drawing text on this page. |
Returns
void
setMediaBox()
setMediaBox(
x,
y,
width,
height): void;Defined in: src/api/PDFPage.ts:281
Set the MediaBox of this page. For example:
const mediaBox = page.getMediaBox()
page.setMediaBox(0, 0, 250, 500)
page.setMediaBox(mediaBox.x, mediaBox.y, 50, 100)
page.setMediaBox(15, 5, mediaBox.width - 50, mediaBox.height - 100)See [[PDFPage.getMediaBox]] for details about what the MediaBox represents.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | The x coordinate of the lower left corner of the new MediaBox. |
y | number | The y coordinate of the lower left corner of the new MediaBox. |
width | number | The width of the new MediaBox. |
height | number | The height of the new MediaBox. |
Returns
void
setRotation()
setRotation(angle): void;Defined in: src/api/PDFPage.ts:144
Rotate this page by a multiple of 90 degrees. For example:
import { degrees } from 'pdf-lib'
page.setRotation(degrees(-90))
page.setRotation(degrees(0))
page.setRotation(degrees(90))
page.setRotation(degrees(180))
page.setRotation(degrees(270))Parameters
| Parameter | Type | Description |
|---|---|---|
angle | Rotation | The angle to rotate this page. |
Returns
void
setSize()
setSize(width, height): void;Defined in: src/api/PDFPage.ts:199
Resize this page by increasing or decreasing its width and height. For example:
page.setSize(250, 500)
page.setSize(page.getWidth() + 50, page.getHeight() + 100)
page.setSize(page.getWidth() - 50, page.getHeight() - 100)Note that the PDF specification does not allow for pages to have explicit widths and heights. Instead it defines the "size" of a page in terms of five rectangles: the MediaBox, CropBox, BleedBox, TrimBox, and ArtBox. As a result, this method cannot directly change the width and height of a page. Instead, it works by adjusting these five boxes.
This method performs the following steps:
- Set width & height of MediaBox.
- Set width & height of CropBox, if it has same dimensions as MediaBox.
- Set width & height of BleedBox, if it has same dimensions as MediaBox.
- Set width & height of TrimBox, if it has same dimensions as MediaBox.
- Set width & height of ArtBox, if it has same dimensions as MediaBox.
This approach works well for most PDF documents as all PDF pages must have a MediaBox, but relatively few have a CropBox, BleedBox, TrimBox, or ArtBox. And when they do have these additional boxes, they often have the same dimensions as the MediaBox. However, if you find this method does not work for your document, consider setting the boxes directly:
- [[PDFPage.setMediaBox]]
- [[PDFPage.setCropBox]]
- [[PDFPage.setBleedBox]]
- [[PDFPage.setTrimBox]]
- [[PDFPage.setArtBox]]
Parameters
| Parameter | Type | Description |
|---|---|---|
width | number | The new width of the page. |
height | number | The new height of the page. |
Returns
void
setTrimBox()
setTrimBox(
x,
y,
width,
height): void;Defined in: src/api/PDFPage.ts:359
Set the TrimBox of this page. For example:
const trimBox = page.getTrimBox()
page.setTrimBox(0, 0, 250, 500)
page.setTrimBox(trimBox.x, trimBox.y, 50, 100)
page.setTrimBox(15, 5, trimBox.width - 50, trimBox.height - 100)See [[PDFPage.getTrimBox]] for details about what the TrimBox represents.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | The x coordinate of the lower left corner of the new TrimBox. |
y | number | The y coordinate of the lower left corner of the new TrimBox. |
width | number | The width of the new TrimBox. |
height | number | The height of the new TrimBox. |
Returns
void
setWidth()
setWidth(width): void;Defined in: src/api/PDFPage.ts:242
Resize this page by increasing or decreasing its width. For example:
page.setWidth(250)
page.setWidth(page.getWidth() + 50)
page.setWidth(page.getWidth() - 50)This method uses [[PDFPage.setSize]] to set the page's width.
Parameters
| Parameter | Type | Description |
|---|---|---|
width | number | The new width of the page. |
Returns
void
translateContent()
translateContent(x, y): void;Defined in: src/api/PDFPage.ts:586
Translate this page's content to a new location on the page. This operation is often useful after resizing the page with [[setSize]]. For example:
// Add 50 units of whitespace to the top and right of the page
page.setSize(page.getWidth() + 50, page.getHeight() + 50)
// Move the page's content from the lower-left corner of the page
// to the top-right corner.
page.translateContent(50, 50)
// Now there are 50 units of whitespace to the left and bottom of the pageSee also: [[resetPosition]]
Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | The new position on the x-axis for this page's content. |
y | number | The new position on the y-axis for this page's content. |
Returns
void