MCPcopy Create free account
hub / github.com/LibPDF-js/core / addPage

Method addPage

src/api/pdf.ts:1618–1646  ·  view source on GitHub ↗

* Add a new blank page. * * @param options - Page size, rotation, and insertion position * @returns The new page * @throws {RangeError} If insertAt index is out of bounds

(options: AddPageOptions = {})

Source from the content-addressed store, hash-verified

1616 * @throws {RangeError} If insertAt index is out of bounds
1617 */
1618 addPage(options: AddPageOptions = {}): PDFPage {
1619 const { width, height } = resolvePageSize(options);
1620 const rotate = options.rotate ?? 0;
1621
1622 // Create minimal page dict
1623 const pageDict = PdfDict.of({
1624 Type: PdfName.Page,
1625 MediaBox: new PdfArray([
1626 PdfNumber.of(0),
1627 PdfNumber.of(0),
1628 PdfNumber.of(width),
1629 PdfNumber.of(height),
1630 ]),
1631 Resources: new PdfDict(),
1632 });
1633
1634 if (rotate !== 0) {
1635 pageDict.set("Rotate", PdfNumber.of(rotate));
1636 }
1637
1638 // Register the page
1639 const pageRef = this.register(pageDict);
1640
1641 // Insert at specified position or append
1642 const index = options.insertAt ?? this.getPageCount();
1643 this.ctx.pages.insertPage(index, pageRef, pageDict);
1644
1645 return new PDFPage(pageRef, pageDict, index, this.ctx);
1646 }
1647
1648 /**
1649 * Insert an existing page at the given index.

Calls 6

registerMethod · 0.95
getPageCountMethod · 0.95
resolvePageSizeFunction · 0.90
ofMethod · 0.45
setMethod · 0.45
insertPageMethod · 0.45

Tested by

no test coverage detected