123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- //
- // KMBatchRemoveWatermarkOperation.swift
- // PDF Reader Pro
- //
- // Created by liujiajie on 2023/11/6.
- //
- import Foundation
- class KMBatchRemoveWatermarkOperation: KMBatchOperation{
- var pdfDocument: CPDFDocument?
-
- override init(file: KMBatchOperateFile) {
- super.init(file: file)
-
- }
- 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)
- }
- if !self.isCancelled {
- self.delegate?.fileBeginOperate?(self.operateFile!, info: self.operateFile!.removeWatermarkInfo)
- willChangeValue(forKey: "isExecuting")
- self.hasExcuting = true
- didChangeValue(forKey: "isExecuting")
- if !FileManager.default.fileExists(atPath: self.operateFile!.filePath) {
- self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("File Not Exist", nil)), info: self.operateFile!.removeWatermarkInfo)
-
- 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!.removeWatermarkInfo)
- 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!.removeWatermarkInfo)
- self.willChangeValue(forKey: "isFinished")
- self.hasFinished = true
- self.didChangeValue(forKey: "isFinished")
- return
- }
- KMBatchRemoveWatermarkOperation.saveAsPDFRemoveAllWatermark(self.pdfDocument!, password: self.operateFile?.password, toPath: self.operateFile!.removeWatermarkInfo.fetchDestinationFilepath()!) { result in
- if result == 1 {
- self.delegate?.fileOperateSuccessed?(self.operateFile!, info: self.operateFile!.removeWatermarkInfo)
- } else {
- self.delegate?.fileOperateFailed?(self.operateFile!, error: self.defaultError() as NSError, info: self.operateFile!.removeWatermarkInfo)
- }
- 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: operateFile!.removeWatermarkInfo.outPutPath!) { try? FileManager.default.removeItem(atPath: operateFile!.removeWatermarkInfo.outPutPath!)
- }
- self.delegate?.fileOperateCanceled?(self.operateFile!, info: self.operateFile!.removeWatermarkInfo)
-
- willChangeValue(forKey: "isFinished")
- hasFinished = true
- didChangeValue(forKey: "isFinished")
- } else {
- willChangeValue(forKey: "isCancelled")
- hasCanceled = true
- didChangeValue(forKey: "isCancelled")
- }
- }
-
- func defaultError() -> Error {
- let errorDict: [String: Any] = [NSLocalizedDescriptionKey: NSLocalizedString("Remove Watermark Failed", comment: "")]
- let error = NSError(domain: "PDFReaderBatchErrorDomain", code: -1, userInfo: errorDict)
- return error
- }
- class func saveAsPDFRemoveAllWatermark(_ pdfDocument: CPDFDocument, password: String?, toPath path: String, completionHandler handler: @escaping (Int) -> Void) {
- let document = CPDFDocument(url: pdfDocument.documentURL)
- if let password = password {
- document?.unlock(withPassword: password)
- }
- let watermarks = document?.watermarks()
- for watermark in watermarks ?? [] {
- document?.removeWatermark(watermark)
- }
- let result = document?.write(to: URL(fileURLWithPath: path))
- DispatchQueue.main.async {
- if let result = result {
- handler(result ? 1 : 0)
- }
- }
- }
- }
|