KMBatchRemoveHeaderFooterOperation.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // KMBatchRemoveHeaderFooterOperation.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/12/1.
  6. //
  7. import Foundation
  8. class KMBatchRemoveHeaderFooterOperation: KMBatchOperation{
  9. var isBates: Bool = false
  10. var pdfDocument: CPDFDocument?
  11. override init(file: KMBatchOperateFile) {
  12. super.init(file: file)
  13. }
  14. override func cancel() {
  15. if isExecuting {
  16. operateFile!.removeWatermarkInfo.status = .Waiting
  17. if FileManager.default.fileExists(atPath: self.currentParameter().outPutPath!) { try? FileManager.default.removeItem(atPath: self.currentParameter().outPutPath!)
  18. }
  19. self.delegate?.fileOperateCanceled?(self.operateFile!, info: self.operateFile!.removeWatermarkInfo)
  20. willChangeValue(forKey: "isFinished")
  21. hasFinished = true
  22. didChangeValue(forKey: "isFinished")
  23. } else {
  24. willChangeValue(forKey: "isCancelled")
  25. hasCanceled = true
  26. didChangeValue(forKey: "isCancelled")
  27. }
  28. }
  29. func currentParameter() -> KMBatchBaseParameter {
  30. if self.isBates {
  31. return self.operateFile!.removeBatesInfo
  32. }
  33. return self.operateFile!.removeHeaderFooterInfo
  34. }
  35. override func start() {
  36. self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: self.operateFile?.filePath ?? ""))
  37. if let data = self.pdfDocument?.isLocked, data {
  38. self.pdfDocument?.unlock(withPassword: self.operateFile?.password)
  39. }
  40. if !self.isCancelled {
  41. self.delegate?.fileBeginOperate?(self.operateFile!, info: self.currentParameter())
  42. willChangeValue(forKey: "isExecuting")
  43. self.hasExcuting = true
  44. didChangeValue(forKey: "isExecuting")
  45. if !FileManager.default.fileExists(atPath: self.operateFile!.filePath) {
  46. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("File Not Exist", nil)), info: self.operateFile!.currentOperateInfo!)
  47. self.willChangeValue(forKey: "isFinished")
  48. self.hasFinished = true
  49. self.didChangeValue(forKey: "isFinished")
  50. return
  51. }
  52. if self.pdfDocument == nil {
  53. 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!.currentOperateInfo!)
  54. self.willChangeValue(forKey: "isFinished")
  55. self.hasFinished = true
  56. self.didChangeValue(forKey: "isFinished")
  57. return
  58. }
  59. if !self.pdfDocument!.allowsPrinting || !self.pdfDocument!.allowsCopying {
  60. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("This is a secured document. Editing is not permitted.", nil)), info: self.operateFile!.currentOperateInfo!)
  61. self.willChangeValue(forKey: "isFinished")
  62. self.hasFinished = true
  63. self.didChangeValue(forKey: "isFinished")
  64. return
  65. }
  66. if self.isBates {
  67. KMBatchRemoveHeaderFooterOperation.saveAsPDFRemoveAllHeaderFooterBates(self.pdfDocument!, password: self.operateFile?.password, toPath: self.operateFile!.currentOperateInfo!.fetchDestinationFilepath()!) { result in
  68. if result == 1 {
  69. self.delegate?.fileOperateSuccessed?(self.operateFile!, info: self.currentParameter())
  70. } else {
  71. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.defaultError() as NSError, info: self.currentParameter())
  72. }
  73. self.willChangeValue(forKey: "isFinished")
  74. self.hasFinished = true
  75. self.didChangeValue(forKey: "isFinished")
  76. }
  77. } else {
  78. KMBatchRemoveHeaderFooterOperation.saveAsPDFRemoveAllHeaderFooter(self.pdfDocument!, password: self.operateFile?.password, toPath: self.operateFile!.currentOperateInfo!.fetchDestinationFilepath()!) { result in
  79. if result == 1 {
  80. self.delegate?.fileOperateSuccessed?(self.operateFile!, info: self.currentParameter())
  81. } else {
  82. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.defaultError() as NSError, info: self.currentParameter())
  83. }
  84. self.willChangeValue(forKey: "isFinished")
  85. self.hasFinished = true
  86. self.didChangeValue(forKey: "isFinished")
  87. }
  88. }
  89. }else {
  90. willChangeValue(forKey: "isFinished")
  91. willChangeValue(forKey: "isExecuting")
  92. hasExcuting = false
  93. hasFinished = true
  94. didChangeValue(forKey: "isExecuting")
  95. didChangeValue(forKey: "isFinished")
  96. }
  97. }
  98. class func saveAsPDFRemoveAllHeaderFooterBates(_ pdfDocument: CPDFDocument, password: String?, toPath path: String, completionHandler handler: @escaping (Int) -> Void) {
  99. // let filePath = pdfDocument.documentURL.path
  100. let document = CPDFDocument(url: pdfDocument.documentURL)
  101. if let password = password {
  102. document?.unlock(withPassword: password)
  103. }
  104. let tBates = document?.bates()
  105. tBates?.clear()
  106. let documentPath = NSTemporaryDirectory()
  107. let tempPath = documentPath.stringByAppendingPathComponent(path.lastPathComponent)
  108. if FileManager.default.fileExists(atPath: tempPath) {
  109. try?FileManager.default.removeItem(atPath: tempPath)
  110. }
  111. let result = document?.write(to: URL(fileURLWithPath: tempPath))
  112. if result ?? false {
  113. if FileManager.default.fileExists(atPath: path) {
  114. try?FileManager.default.removeItem(atPath: path)
  115. }
  116. try?FileManager.default.moveItem(atPath: tempPath, toPath: path)
  117. } else {
  118. try?FileManager.default.removeItem(atPath: tempPath)
  119. }
  120. DispatchQueue.main.async {
  121. if let result = result {
  122. handler(result ? 1 : 0)
  123. }
  124. }
  125. }
  126. class func saveAsPDFRemoveAllHeaderFooter(_ pdfDocument: CPDFDocument, password: String?, toPath path: String, completionHandler handler: @escaping (Int) -> Void) {
  127. // let filePath = pdfDocument.documentURL.path
  128. let document = CPDFDocument(url: pdfDocument.documentURL)
  129. if let password = password {
  130. document?.unlock(withPassword: password)
  131. }
  132. let tHeaderFooter = document?.headerFooter()
  133. tHeaderFooter?.clear()
  134. let documentPath = NSTemporaryDirectory()
  135. let tempPath = documentPath.stringByAppendingPathComponent(path.lastPathComponent)
  136. if FileManager.default.fileExists(atPath: tempPath) {
  137. try?FileManager.default.removeItem(atPath: tempPath)
  138. }
  139. let result = document?.write(to: URL(fileURLWithPath: tempPath))
  140. if result ?? false {
  141. if FileManager.default.fileExists(atPath: path) {
  142. try?FileManager.default.removeItem(atPath: path)
  143. }
  144. try?FileManager.default.moveItem(atPath: tempPath, toPath: path)
  145. } else {
  146. try?FileManager.default.removeItem(atPath: tempPath)
  147. }
  148. DispatchQueue.main.async {
  149. if let result = result {
  150. handler(result ? 1 : 0)
  151. }
  152. }
  153. }
  154. func defaultError() -> Error {
  155. return self.errorWithMsg(KMLocalizedString("Failed", nil))
  156. }
  157. }