123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- //
- // 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")
- }
- }
-
- }
|