123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- //
- // KMBatchRemoveHeaderFooterOperation.swift
- // PDF Reader Pro
- //
- // Created by liujiajie on 2023/12/1.
- //
- import Foundation
- class KMBatchRemoveHeaderFooterOperation: KMBatchOperation{
- var isBates: Bool = false
- var pdfDocument: CPDFDocument?
-
- override init(file: KMBatchOperateFile) {
- super.init(file: file)
-
- }
- override func cancel() {
- if isExecuting {
- operateFile!.removeWatermarkInfo.status = .Waiting
- if FileManager.default.fileExists(atPath: self.currentParameter().outPutPath!) { try? FileManager.default.removeItem(atPath: self.currentParameter().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 currentParameter() -> KMBatchBaseParameter {
- if self.isBates {
- return self.operateFile!.removeBatesInfo
- }
- return self.operateFile!.removeHeaderFooterInfo
- }
- 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.currentParameter())
- 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")), info: self.operateFile!.currentOperateInfo!)
-
- 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.")), info: self.operateFile!.currentOperateInfo!)
- 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.")), info: self.operateFile!.currentOperateInfo!)
- self.willChangeValue(forKey: "isFinished")
- self.hasFinished = true
- self.didChangeValue(forKey: "isFinished")
- return
- }
- if self.isBates {
- KMBatchRemoveHeaderFooterOperation.saveAsPDFRemoveAllHeaderFooterBates(self.pdfDocument!, password: self.operateFile?.password, toPath: self.operateFile!.currentOperateInfo!.fetchDestinationFilepath()!) { result in
- if result == 1 {
- self.delegate?.fileOperateSuccessed?(self.operateFile!, info: self.currentParameter())
- } else {
- self.delegate?.fileOperateFailed?(self.operateFile!, error: self.defaultError() as NSError, info: self.currentParameter())
- }
- self.willChangeValue(forKey: "isFinished")
- self.hasFinished = true
- self.didChangeValue(forKey: "isFinished")
- }
- } else {
- KMBatchRemoveHeaderFooterOperation.saveAsPDFRemoveAllHeaderFooter(self.pdfDocument!, password: self.operateFile?.password, toPath: self.operateFile!.currentOperateInfo!.fetchDestinationFilepath()!) { result in
- if result == 1 {
- self.delegate?.fileOperateSuccessed?(self.operateFile!, info: self.currentParameter())
- } else {
- self.delegate?.fileOperateFailed?(self.operateFile!, error: self.defaultError() as NSError, info: self.currentParameter())
- }
- 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")
- }
- }
- class func saveAsPDFRemoveAllHeaderFooterBates(_ pdfDocument: CPDFDocument, password: String?, toPath path: String, completionHandler handler: @escaping (Int) -> Void) {
- // let filePath = pdfDocument.documentURL.path
- let document = CPDFDocument(url: pdfDocument.documentURL)
- if let password = password {
- document?.unlock(withPassword: password)
- }
-
- let tBates = document?.bates()
- tBates?.clear()
-
- let documentPath = NSTemporaryDirectory()
- let tempPath = documentPath.stringByAppendingPathComponent(path.lastPathComponent)
- if FileManager.default.fileExists(atPath: tempPath) {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
-
- let result = document?.write(to: URL(fileURLWithPath: tempPath))
- if result ?? false {
- if FileManager.default.fileExists(atPath: path) {
- try?FileManager.default.removeItem(atPath: path)
- }
- try?FileManager.default.moveItem(atPath: tempPath, toPath: path)
- } else {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
- DispatchQueue.main.async {
- if let result = result {
- handler(result ? 1 : 0)
- }
- }
- }
- class func saveAsPDFRemoveAllHeaderFooter(_ pdfDocument: CPDFDocument, password: String?, toPath path: String, completionHandler handler: @escaping (Int) -> Void) {
- // let filePath = pdfDocument.documentURL.path
- let document = CPDFDocument(url: pdfDocument.documentURL)
- if let password = password {
- document?.unlock(withPassword: password)
- }
-
- let tHeaderFooter = document?.headerFooter()
- tHeaderFooter?.clear()
-
- let documentPath = NSTemporaryDirectory()
- let tempPath = documentPath.stringByAppendingPathComponent(path.lastPathComponent)
- if FileManager.default.fileExists(atPath: tempPath) {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
-
- let result = document?.write(to: URL(fileURLWithPath: tempPath))
- if result ?? false {
- if FileManager.default.fileExists(atPath: path) {
- try?FileManager.default.removeItem(atPath: path)
- }
- try?FileManager.default.moveItem(atPath: tempPath, toPath: path)
- } else {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
- DispatchQueue.main.async {
- if let result = result {
- handler(result ? 1 : 0)
- }
- }
- }
- func defaultError() -> Error {
- return self.errorWithMsg(KMLocalizedString("Failed"))
- }
- }
|