Skip to content

Commit ce296d8

Browse files
committed
Add the possibility to order the pages in an extracted pdf (bug 1997379)
or in a merged one.
1 parent 8188c87 commit ce296d8

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

src/core/editor/pdf_editor.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ class PDFEditor {
470470
* included ranges (inclusive) or indices.
471471
* @property {Array<Array<number>|number>} [excludePages]
472472
* excluded ranges (inclusive) or indices.
473+
* @property {Array<number>} [pageIndices]
474+
* position of the pages in the final document.
473475
*/
474476

475477
/**
@@ -482,10 +484,18 @@ class PDFEditor {
482484
let newIndex = 0;
483485
this.hasSingleFile = pageInfos.length === 1;
484486
const allDocumentData = [];
485-
for (const { document, includePages, excludePages } of pageInfos) {
487+
for (const {
488+
document,
489+
includePages,
490+
excludePages,
491+
pageIndices,
492+
} of pageInfos) {
486493
if (!document) {
487494
continue;
488495
}
496+
if (pageIndices) {
497+
newIndex = -1;
498+
}
489499
const documentData = new DocumentData(document);
490500
allDocumentData.push(documentData);
491501
promises.push(this.#collectDocumentData(documentData));
@@ -504,6 +514,7 @@ class PDFEditor {
504514
(deletedIndices ||= new Set()).add(page);
505515
}
506516
}
517+
let pageIndex = 0;
507518
for (let i = 0, ii = document.numPages; i < ii; i++) {
508519
if (deletedIndices?.has(i)) {
509520
continue;
@@ -539,7 +550,23 @@ class PDFEditor {
539550
if (!takePage) {
540551
continue;
541552
}
542-
const newPageIndex = newIndex++;
553+
let newPageIndex;
554+
if (pageIndices) {
555+
newPageIndex = pageIndices[pageIndex++];
556+
}
557+
if (newPageIndex === undefined) {
558+
if (newIndex !== -1) {
559+
newPageIndex = newIndex++;
560+
} else {
561+
for (
562+
newPageIndex = 0;
563+
this.oldPages[newPageIndex] === undefined;
564+
newPageIndex++
565+
) {
566+
/* empty */
567+
}
568+
}
569+
}
543570
promises.push(
544571
document.getPage(i).then(page => {
545572
this.oldPages[newPageIndex] = new PageData(page, documentData);

test/unit/api_spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5968,5 +5968,43 @@ small scripts as well as for`);
59685968
await loadingTask.destroy();
59695969
});
59705970
});
5971+
5972+
describe("Extract pages and reorganize them", function () {
5973+
it("extract page and check destinations", async function () {
5974+
let loadingTask = getDocument(
5975+
buildGetDocumentParams("tracemonkey.pdf")
5976+
);
5977+
let pdfDoc = await loadingTask.promise;
5978+
const data = await pdfDoc.extractPages([
5979+
{ document: null, includePages: [1, 3, 5], pageIndices: [1, 2, 0] },
5980+
]);
5981+
await loadingTask.destroy();
5982+
loadingTask = getDocument(data);
5983+
pdfDoc = await loadingTask.promise;
5984+
5985+
expect(pdfDoc.numPages).toEqual(3);
5986+
5987+
// Page 6 in the original document.
5988+
const firstPage = await pdfDoc.getPage(1);
5989+
let { items: textItems } = await firstPage.getTextContent();
5990+
expect(
5991+
mergeText(textItems).includes("4. Nested Trace Tree Formation")
5992+
).toBeTrue();
5993+
5994+
// Page 2 in the original document.
5995+
const secondPage = await pdfDoc.getPage(2);
5996+
({ items: textItems } = await secondPage.getTextContent());
5997+
expect(
5998+
mergeText(textItems).includes("2. Overview: Example Tracing Run")
5999+
).toBeTrue();
6000+
6001+
// Page 4 in the original document.
6002+
const thirdPage = await pdfDoc.getPage(3);
6003+
({ items: textItems } = await thirdPage.getTextContent());
6004+
expect(mergeText(textItems).includes("3. Trace Trees")).toBeTrue();
6005+
6006+
await loadingTask.destroy();
6007+
});
6008+
});
59716009
});
59726010
});

0 commit comments

Comments
 (0)