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

Method merge

src/api/pdf.ts:682–704  ·  view source on GitHub ↗

* Merge multiple PDF documents into one. * * Creates a new document containing all pages from the source documents * in order. The resulting document is a fresh copy — original documents * are not modified. * * @param sources Array of PDF bytes to merge * @param options Load and

(sources: Uint8Array[], options: MergeOptions = {})

Source from the content-addressed store, hash-verified

680 * ```
681 */
682 static async merge(sources: Uint8Array[], options: MergeOptions = {}): Promise<PDF> {
683 if (sources.length === 0) {
684 return PDF.create();
685 }
686
687 // Load first document as the base
688 const result = await PDF.load(sources[0], options);
689
690 // Copy pages from remaining documents
691 for (let i = 1; i < sources.length; i++) {
692 const source = await PDF.load(sources[i], options);
693 const pageCount = source.getPageCount();
694
695 if (pageCount > 0) {
696 const indices = Array.from({ length: pageCount }, (_, j) => j);
697 await result.copyPagesFrom(source, indices, {
698 includeAnnotations: options.includeAnnotations ?? true,
699 });
700 }
701 }
702
703 return result;
704 }
705
706 // ─────────────────────────────────────────────────────────────────────────────
707 // Document info

Callers 6

pdf.test.tsFile · 0.80
mainFunction · 0.80
mainFunction · 0.80
mainFunction · 0.80
copying.bench.tsFile · 0.80

Calls 5

fromMethod · 0.80
copyPagesFromMethod · 0.80
createMethod · 0.65
getPageCountMethod · 0.65
loadMethod · 0.45

Tested by

no test coverage detected