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

Method embedPage

src/api/pdf.ts:1861–1910  ·  view source on GitHub ↗
(source: PDF, pageIndex: number)

Source from the content-addressed store, hash-verified

1859 */
1860 // oxlint-disable-next-line typescript/require-await -- Public async API kept for backward compat; ObjectCopier is sync.
1861 async embedPage(source: PDF, pageIndex: number): Promise<PDFEmbeddedPage> {
1862 const srcPage = source.getPage(pageIndex);
1863
1864 if (!srcPage) {
1865 throw new RangeError(`Page index ${pageIndex} out of bounds`);
1866 }
1867
1868 // Get page content streams and concatenate
1869 const contentData = this.getPageContentData(source, srcPage);
1870
1871 // Copy resources from source to dest
1872 const copier = new ObjectCopier(source, this, { includeAnnotations: false });
1873
1874 const srcResources = srcPage.dict.getDict("Resources", source.getObject.bind(source));
1875
1876 let resources: PdfDict;
1877
1878 if (srcResources) {
1879 const copied = copier.copyObject(srcResources);
1880
1881 // This is guaranteed by our checks above
1882 resources = copied instanceof PdfDict ? copied : new PdfDict();
1883 } else {
1884 resources = new PdfDict();
1885 }
1886
1887 // Get the MediaBox
1888 const mediaBox = srcPage.getMediaBox();
1889
1890 // Create Form XObject
1891 const formXObject = PdfStream.fromDict(
1892 {
1893 Type: PdfName.of("XObject"),
1894 Subtype: PdfName.of("Form"),
1895 FormType: PdfNumber.of(1),
1896 BBox: new PdfArray([
1897 PdfNumber.of(mediaBox.x),
1898 PdfNumber.of(mediaBox.y),
1899 PdfNumber.of(mediaBox.x + mediaBox.width),
1900 PdfNumber.of(mediaBox.y + mediaBox.height),
1901 ]),
1902 Resources: resources,
1903 },
1904 contentData,
1905 );
1906
1907 const ref = this.register(formXObject);
1908
1909 return new PDFEmbeddedPage(ref, mediaBox, srcPage.width, srcPage.height);
1910 }
1911
1912 /**
1913 * Get the concatenated content stream data from a page.

Callers 6

drawing.test.tsFile · 0.80
pdf.test.tsFile · 0.80
mainFunction · 0.80
mainFunction · 0.80
mainFunction · 0.80

Calls 8

getPageContentDataMethod · 0.95
copyObjectMethod · 0.95
registerMethod · 0.95
getMediaBoxMethod · 0.80
fromDictMethod · 0.80
getDictMethod · 0.65
getPageMethod · 0.45
ofMethod · 0.45

Tested by

no test coverage detected