// // KMBatchAddWatermarkOperation.swift // Cisdem PDFMaster // // Created by liujiajie on 2023/12/11. // import Foundation class KMBatchAddWatermarkOperation: KMBatchOperation{ var waterMark: KMWatermarkModel? // var waterObject: KMPDFWatermarkObject? var pdfDocument: CPDFDocument! init(file: KMBatchOperateFile, waterMarkM: KMWatermarkModel) { super.init(file: file) self.waterMark = waterMarkM } override func start() { self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: self.operateFile?.filePath ?? "")) if let data = self.pdfDocument?.isLocked, data { self.pdfDocument?.unlock(withPassword: self.operateFile?.password) } // let docum = CPDFDocument(url: self.pdfDocument.documentURL) // if self.operateFile?.password.count ?? 0 > 0 { // self.pdfDocument?.unlock(withPassword: self.operateFile?.password) // } if !self.isCancelled { self.delegate?.fileBeginOperate?(self.operateFile!, info: self.operateFile!.addWatermarkInfo) willChangeValue(forKey: "isExecuting") self.hasExcuting = true didChangeValue(forKey: "isExecuting") let newArr = NSMutableArray() for i in 0..<(self.operateFile?.addWatermarkInfo.pagesArray?.count ?? 0) { let number = self.operateFile?.addWatermarkInfo.pagesArray![i] newArr.add(number!.intValue - 1) } if !FileManager.default.fileExists(atPath: self.operateFile!.filePath) { self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("File Not Exist", nil)), info: self.operateFile!.addWatermarkInfo) self.willChangeValue(forKey: "isFinished") self.hasFinished = true self.didChangeValue(forKey: "isFinished") return } if self.pdfDocument == nil { self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", nil)), info: self.operateFile!.addWatermarkInfo) self.willChangeValue(forKey: "isFinished") self.hasFinished = true self.didChangeValue(forKey: "isFinished") return } if !self.pdfDocument!.allowsPrinting || !self.pdfDocument!.allowsCopying { self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("This is a secured document. Editing is not permitted.", nil)), info: self.operateFile!.addWatermarkInfo) self.willChangeValue(forKey: "isFinished") self.hasFinished = true self.didChangeValue(forKey: "isFinished") return } if newArr.count < 1 { let err = NSError(domain: "LocalError", code: 0, userInfo: [NSLocalizedDescriptionKey: KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil)]) self.delegate?.fileOperateFailed?(self.operateFile!, error: err, info: self.operateFile!.addWatermarkInfo) self.willChangeValue(forKey: "isFinished") self.hasFinished = true self.didChangeValue(forKey: "isFinished") } self.waterMark?.pagesString = newArr.componentsJoined(by: ",") KMWatermarkView.addWatermarkForOutSide(document: self.pdfDocument, model: self.waterMark!, toPath: (self.operateFile?.addWatermarkInfo.fetchDestinationFilepath())!) { success in if success { self.delegate?.fileOperateSuccessed?(self.operateFile!, info: self.operateFile!.addWatermarkInfo) }else { self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("Failed", nil)), info: self.operateFile!.addWatermarkInfo) } self.willChangeValue(forKey: "isFinished") self.hasFinished = true self.didChangeValue(forKey: "isFinished") } }else { willChangeValue(forKey: "isFinished") willChangeValue(forKey: "isExecuting") hasExcuting = false hasFinished = true didChangeValue(forKey: "isExecuting") didChangeValue(forKey: "isFinished") } } override func cancel() { // super.cancel() if isExecuting { operateFile!.removeWatermarkInfo.status = .Waiting if FileManager.default.fileExists(atPath: (self.operateFile?.addWatermarkInfo.outPutPath)!) { try? FileManager.default.removeItem(atPath: (self.operateFile?.addWatermarkInfo.outPutPath)!) } self.delegate?.fileOperateCanceled?(self.operateFile!, info: self.operateFile!.addWatermarkInfo) willChangeValue(forKey: "isFinished") hasFinished = true didChangeValue(forKey: "isFinished") } else { willChangeValue(forKey: "isCancelled") hasCanceled = true didChangeValue(forKey: "isCancelled") } } }