Stirling-PDF/shared-operations/src/functions/removePages.ts
2024-07-13 23:02:46 +02:00

24 lines
974 B
TypeScript

import { PdfFile } from "../wrappers/PdfFile";
import { Operator, Progress, oneToOne } from ".";
import { getPages } from "./common/getPagesByIndex";
import { invertSelection } from "./common/pageIndexesUtils";
export class RemovePages extends Operator {
/** PDF extraction, specify pages from one pdf and output them to a new pdf */
async run(input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
return oneToOne<PdfFile, PdfFile>(input, async (input, index, max) => {
const pdfDoc = await input.pdfLibDocument;
const pageCount = pdfDoc.getPageCount();
const pagesToKeep = invertSelection(this.actionValues.pageIndexes, pageCount);
const newFile = await getPages(input, pagesToKeep);
newFile.filename += "_removedPages";
progressCallback({ curFileProgress: 1, operationProgress: index/max });
return newFile;
});
}
}