* 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 = {})
| 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 |
no test coverage detected