KMBatchAddWatermarkOperation.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // KMBatchAddWatermarkOperation.swift
  3. // Cisdem PDFMaster
  4. //
  5. // Created by liujiajie on 2023/12/11.
  6. //
  7. import Foundation
  8. class KMBatchAddWatermarkOperation: KMBatchOperation{
  9. var waterMark: KMWatermarkModel?
  10. // var waterObject: KMPDFWatermarkObject?
  11. var pdfDocument: CPDFDocument!
  12. init(file: KMBatchOperateFile, waterMarkM: KMWatermarkModel) {
  13. super.init(file: file)
  14. self.waterMark = waterMarkM
  15. }
  16. override func start() {
  17. self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: self.operateFile?.filePath ?? ""))
  18. if let data = self.pdfDocument?.isLocked, data {
  19. self.pdfDocument?.unlock(withPassword: self.operateFile?.password)
  20. }
  21. // let docum = CPDFDocument(url: self.pdfDocument.documentURL)
  22. // if self.operateFile?.password.count ?? 0 > 0 {
  23. // self.pdfDocument?.unlock(withPassword: self.operateFile?.password)
  24. // }
  25. if !self.isCancelled {
  26. self.delegate?.fileBeginOperate?(self.operateFile!, info: self.operateFile!.addWatermarkInfo)
  27. willChangeValue(forKey: "isExecuting")
  28. self.hasExcuting = true
  29. didChangeValue(forKey: "isExecuting")
  30. let newArr = NSMutableArray()
  31. for i in 0..<(self.operateFile?.addWatermarkInfo.pagesArray?.count ?? 0) {
  32. let number = self.operateFile?.addWatermarkInfo.pagesArray![i]
  33. newArr.add(number!.intValue - 1)
  34. }
  35. if !FileManager.default.fileExists(atPath: self.operateFile!.filePath) {
  36. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("File Not Exist", nil)), info: self.operateFile!.addWatermarkInfo)
  37. self.willChangeValue(forKey: "isFinished")
  38. self.hasFinished = true
  39. self.didChangeValue(forKey: "isFinished")
  40. return
  41. }
  42. if self.pdfDocument == nil {
  43. 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)
  44. self.willChangeValue(forKey: "isFinished")
  45. self.hasFinished = true
  46. self.didChangeValue(forKey: "isFinished")
  47. return
  48. }
  49. if !self.pdfDocument!.allowsPrinting || !self.pdfDocument!.allowsCopying {
  50. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("This is a secured document. Editing is not permitted.", nil)), info: self.operateFile!.addWatermarkInfo)
  51. self.willChangeValue(forKey: "isFinished")
  52. self.hasFinished = true
  53. self.didChangeValue(forKey: "isFinished")
  54. return
  55. }
  56. if newArr.count < 1 {
  57. 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)])
  58. self.delegate?.fileOperateFailed?(self.operateFile!, error: err, info: self.operateFile!.addWatermarkInfo)
  59. self.willChangeValue(forKey: "isFinished")
  60. self.hasFinished = true
  61. self.didChangeValue(forKey: "isFinished")
  62. }
  63. self.waterMark?.pagesString = newArr.componentsJoined(by: ",")
  64. KMWatermarkView.addWatermarkForOutSide(document: self.pdfDocument, model: self.waterMark!, toPath: (self.operateFile?.addWatermarkInfo.fetchDestinationFilepath())!) { success in
  65. if success {
  66. self.delegate?.fileOperateSuccessed?(self.operateFile!, info: self.operateFile!.addWatermarkInfo)
  67. }else {
  68. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("Failed", nil)), info: self.operateFile!.addWatermarkInfo)
  69. }
  70. self.willChangeValue(forKey: "isFinished")
  71. self.hasFinished = true
  72. self.didChangeValue(forKey: "isFinished")
  73. }
  74. }else {
  75. willChangeValue(forKey: "isFinished")
  76. willChangeValue(forKey: "isExecuting")
  77. hasExcuting = false
  78. hasFinished = true
  79. didChangeValue(forKey: "isExecuting")
  80. didChangeValue(forKey: "isFinished")
  81. }
  82. }
  83. override func cancel() {
  84. // super.cancel()
  85. if isExecuting {
  86. operateFile!.removeWatermarkInfo.status = .Waiting
  87. if FileManager.default.fileExists(atPath: (self.operateFile?.addWatermarkInfo.outPutPath)!) { try? FileManager.default.removeItem(atPath: (self.operateFile?.addWatermarkInfo.outPutPath)!)
  88. }
  89. self.delegate?.fileOperateCanceled?(self.operateFile!, info: self.operateFile!.addWatermarkInfo)
  90. willChangeValue(forKey: "isFinished")
  91. hasFinished = true
  92. didChangeValue(forKey: "isFinished")
  93. } else {
  94. willChangeValue(forKey: "isCancelled")
  95. hasCanceled = true
  96. didChangeValue(forKey: "isCancelled")
  97. }
  98. }
  99. }