29 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-05-13 20:46:04 +02:00
import { Operator, Progress, oneToOne } from ".";
2024-02-23 23:55:29 +01:00
import { PdfFile } from "../wrappers/PdfFile";
import { detectEmptyPages } from "./common/detectEmptyPages";
import { getPages } from "./common/getPagesByIndex";
import { invertSelection } from "./common/pageIndexesUtils";
2024-05-13 20:46:04 +02:00
export class RemoveBlankPages extends Operator {
/** Detect and remove white pages */
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();
progressCallback({ curFileProgress: 0, operationProgress: index/max });
const emptyPages = await detectEmptyPages(input, this.actionValues.whiteThreashold);
progressCallback({ curFileProgress: 0.6, operationProgress: index/max });
const pagesToKeep = invertSelection(emptyPages, pageCount);
progressCallback({ curFileProgress: 0.3, operationProgress: index/max });
const result = await getPages(input, pagesToKeep);
progressCallback({ curFileProgress: 1, operationProgress: index/max });
result.filename += "_removedBlanks";
return result;
});
}
}