Skip to content

Commit

Permalink
Use graft map when copying multiple pages and objects between documents.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccxvii committed Feb 5, 2025
1 parent 94b951a commit d5ac34b
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/mupdfjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export class PDFDocument extends mupdf.PDFDocument {

const sourcePageCount = sourcePDF.countPages();
const targetPageCount = this.countPages();
const graftMap = this.newGraftMap()

// Normalize page numbers
fromPage = Math.max(0, Math.min(fromPage, sourcePageCount - 1));
Expand All @@ -427,13 +428,13 @@ export class PDFDocument extends mupdf.PDFDocument {
// Copy page contents
const contents = pageObj.get("Contents");
if (contents) {
newPageObj.put("Contents", this.graftObject(contents));
newPageObj.put("Contents", graftMap.graftObject(contents));
}

// Copy page resources
const resources = pageObj.get("Resources");
if (resources) {
newPageObj.put("Resources", this.graftObject(resources));
newPageObj.put("Resources", graftMap.graftObject(resources));
}

// Insert the new page at the specified position
Expand Down Expand Up @@ -517,11 +518,12 @@ export class PDFDocument extends mupdf.PDFDocument {
var n: number = 0;
while (n < ranges.length) {
let newDoc = new mupdf.PDFDocument() as PDFDocument;
let graftMap = newDoc.newGraftMap()

if (ranges[n] != undefined) {
for (let o: number = 0; o < ranges[n]!.length; o++) {
// note: "o" is the "to" number for graftPage()
newDoc.graftPage(o, document, ranges[n]![o]!);
graftMap.graftPage(o, document, ranges[n]![o]!);
}
documents.push(newDoc);
}
Expand Down

0 comments on commit d5ac34b

Please sign in to comment.