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

Method copyPagesFrom

src/api/pdf.ts:1734–1789  ·  view source on GitHub ↗
(
    source: PDF,
    indices: number[],
    options: CopyPagesOptions = {},
  )

Source from the content-addressed store, hash-verified

1732 */
1733 // oxlint-disable-next-line typescript/require-await -- Public async API kept for backward compat; ObjectCopier is sync.
1734 async copyPagesFrom(
1735 source: PDF,
1736 indices: number[],
1737 options: CopyPagesOptions = {},
1738 ): Promise<PDFPage[]> {
1739 const copier = new ObjectCopier(source, this, {
1740 includeAnnotations: options.includeAnnotations ?? true,
1741 includeBeads: options.includeBeads ?? false,
1742 includeThumbnails: options.includeThumbnails ?? false,
1743 includeStructure: options.includeStructure ?? false,
1744 });
1745
1746 const copiedPages: PDFPage[] = [];
1747
1748 // Fail-fast: validate all indices first
1749 for (const index of indices) {
1750 if (index < 0 || index >= source.getPageCount()) {
1751 throw new RangeError(`Page index ${index} out of bounds (0-${source.getPageCount() - 1})`);
1752 }
1753 }
1754
1755 // Copy each page
1756 const copiedRefs: PdfRef[] = [];
1757
1758 for (const index of indices) {
1759 const srcPage = source.getPage(index);
1760
1761 if (!srcPage) {
1762 throw new Error(`Source page ${index} not found`);
1763 }
1764
1765 const copiedPageRef = copier.copyPage(srcPage.ref);
1766 copiedRefs.push(copiedPageRef);
1767 }
1768
1769 // Insert copied pages at specified position (or append)
1770 let insertIndex = options.insertAt ?? this.getPageCount();
1771
1772 for (const copiedRef of copiedRefs) {
1773 const copiedDict = this.getObject(copiedRef);
1774
1775 if (!(copiedDict instanceof PdfDict)) {
1776 throw new Error("Copied page is not a dictionary");
1777 }
1778
1779 this.ctx.pages.insertPage(insertIndex, copiedRef, copiedDict);
1780
1781 // Create PDFPage wrapper and add to result
1782 const page = new PDFPage(copiedRef, copiedDict, insertIndex, this.ctx);
1783 copiedPages.push(page);
1784
1785 insertIndex++;
1786 }
1787
1788 return copiedPages;
1789 }
1790
1791 /**

Callers 8

mergeMethod · 0.80
extractPagesMethod · 0.80
pdf.test.tsFile · 0.80
mainFunction · 0.80
mainFunction · 0.80
copying.bench.tsFile · 0.80
buildSyntheticPdfFunction · 0.80

Calls 7

copyPageMethod · 0.95
getPageCountMethod · 0.95
getObjectMethod · 0.95
pushMethod · 0.80
getPageCountMethod · 0.65
getPageMethod · 0.45
insertPageMethod · 0.45

Tested by

no test coverage detected