KMBatchOperateFile.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // KMBatchOperateFile.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/10/25.
  6. //
  7. import Cocoa
  8. @objcMembers class KMBatchOperateFile: NSObject{
  9. /**基本信息*/
  10. var password: String = ""
  11. var filePath: String = ""
  12. var outputFilePath: String = ""
  13. lazy var sizeString: String = {
  14. guard let dict = try? FileManager.default.attributesOfItem(atPath: self.filePath) as NSDictionary else {
  15. return "0"
  16. }
  17. let _sizeString = self.fileSizeString(fSize: Float(dict.fileSize()))
  18. return _sizeString
  19. }()
  20. var fileType: KMBatchOperateFileType?
  21. // 是否使用OCR
  22. var isAllowOCR = false
  23. var ocrLanguage: COCRLanguage?
  24. /*当前正在进行的任务状态*/
  25. var error: NSError? {
  26. return self.currentOperateInfo?.error
  27. }
  28. var status: KMBatchOperateStatus?{
  29. return self.currentOperateInfo?.status
  30. }
  31. var dpi: Int? {
  32. if self.currentOperateType == .Convert{
  33. return self.currentConvertParameter?.dpi
  34. }
  35. return 0
  36. }
  37. var progress: Float?{
  38. return self.currentOperateInfo?.progress
  39. }
  40. var currentOperateType: KMBatchOperationType?
  41. var convertType: KMConvertWithPDFType = .WordAdvance
  42. /*当前的操作详细信息**/
  43. lazy var createPDFInfo: KMBatchBaseParameter = {
  44. let info = KMBatchBaseParameter(file: self)
  45. return info
  46. }()
  47. var convertInfo: KMBatchConvertParameter?{
  48. return self.currentConvertParameter
  49. }
  50. lazy var mergeInfo: KMBatchBaseParameter = {
  51. let info = KMBatchBaseParameter(file: self)
  52. return info
  53. }()
  54. lazy var compressInfo: KMBatchBaseParameter = {
  55. let info = KMBatchBaseParameter(file: self)
  56. return info
  57. }()
  58. lazy var ocrInfo: KMBatchBaseParameter = {
  59. let info = KMBatchBaseParameter(file: self)
  60. return info
  61. }()
  62. lazy var addPasswordInfo: KMBatchBaseParameter = {
  63. let info = KMBatchBaseParameter(file: self)
  64. return info
  65. }()
  66. lazy var removePasswordInfo: KMBatchBaseParameter = {
  67. let info = KMBatchBaseParameter(file: self)
  68. return info
  69. }()
  70. lazy var addWatermarkInfo: KMBatchBaseParameter = {
  71. let info = KMBatchBaseParameter(file: self)
  72. return info
  73. }()
  74. lazy var removeWatermarkInfo: KMBatchBaseParameter = {
  75. let info = KMBatchBaseParameter(file: self)
  76. return info
  77. }()
  78. lazy var addBackgroundInfo: KMBatchBaseParameter = {
  79. let info = KMBatchBaseParameter(file: self)
  80. return info
  81. }()
  82. lazy var removeBackgroundInfo: KMBatchBaseParameter = {
  83. let info = KMBatchBaseParameter(file: self)
  84. return info
  85. }()
  86. lazy var addHeaderFooterInfo: KMBatchBaseParameter = {
  87. let info = KMBatchBaseParameter(file: self)
  88. return info
  89. }()
  90. lazy var removeHeaderFooterInfo: KMBatchBaseParameter = {
  91. let info = KMBatchBaseParameter(file: self)
  92. return info
  93. }()
  94. lazy var addBatesInfo: KMBatchBaseParameter = {
  95. let info = KMBatchBaseParameter(file: self)
  96. return info
  97. }()
  98. lazy var removeBatesInfo: KMBatchBaseParameter = {
  99. let info = KMBatchBaseParameter(file: self)
  100. return info
  101. }()
  102. var currentOperateInfo: KMBatchBaseParameter?{
  103. switch self.currentOperateType {
  104. case .CreatePDF:
  105. return self.createPDFInfo
  106. case .Convert:
  107. return self.convertInfo
  108. case .Merge:
  109. return self.mergeInfo
  110. case .Compress:
  111. return self.compressInfo
  112. case .OCR:
  113. return self.ocrInfo
  114. case .AddPassword:
  115. return self.addPasswordInfo
  116. case .RemovePassword:
  117. return self.removePasswordInfo
  118. case .AddWatermark:
  119. return self.addWatermarkInfo
  120. case .RemoveWatermark:
  121. return self.removeWatermarkInfo
  122. case .AddBackground:
  123. return self.addBackgroundInfo
  124. case .RemoveBackground:
  125. return self.removeBackgroundInfo
  126. case .AddHeaderFooter:
  127. return self.addHeaderFooterInfo
  128. case .RemoveHeaderFooter:
  129. return self.removeHeaderFooterInfo
  130. case .AddBates:
  131. return self.addBatesInfo
  132. case .RemoveBates:
  133. return self.removeBatesInfo
  134. case .none:
  135. return nil
  136. case .some(.NotDetermined):
  137. return nil
  138. }
  139. }
  140. /*转换详细信息**/
  141. lazy var standardWordParameter: KMBatchConvertParameter = {
  142. let paramter = KMBatchConvertParameter(file: self)
  143. paramter.convertType = .WordStandard
  144. return paramter
  145. }()
  146. lazy var advanceWordParameter: KMBatchConvertParameter = {
  147. let paramter = KMBatchConvertParameter(file: self)
  148. paramter.convertType = .WordAdvance
  149. return paramter
  150. }()
  151. lazy var excelParameter: KMBatchConvertParameter = {
  152. let paramter = KMBatchConvertParameter(file: self)
  153. paramter.convertType = .Excel
  154. return paramter
  155. }()
  156. lazy var PPTParameter: KMBatchConvertParameter = {
  157. let paramter = KMBatchConvertParameter(file: self)
  158. paramter.convertType = .PowerPoint
  159. return paramter
  160. }()
  161. lazy var RTFParameter: KMBatchConvertParameter = {
  162. let paramter = KMBatchConvertParameter(file: self)
  163. paramter.convertType = .RTF
  164. return paramter
  165. }()
  166. lazy var CSVParameter: KMBatchConvertParameter = {
  167. let paramter = KMBatchConvertParameter(file: self)
  168. paramter.convertType = .CSV
  169. return paramter
  170. }()
  171. lazy var HTMLParameter: KMBatchConvertParameter = {
  172. let paramter = KMBatchConvertParameter(file: self)
  173. paramter.convertType = .HTML
  174. return paramter
  175. }()
  176. lazy var TXTParameter: KMBatchConvertParameter = {
  177. let paramter = KMBatchConvertParameter(file: self)
  178. paramter.convertType = .Text
  179. return paramter
  180. }()
  181. lazy var JPEGParameter: KMBatchConvertParameter = {
  182. let paramter = KMBatchConvertParameter(file: self)
  183. paramter.convertType = .JPEG
  184. return paramter
  185. }()
  186. lazy var JPGParameter: KMBatchConvertParameter = {
  187. let paramter = KMBatchConvertParameter(file: self)
  188. paramter.convertType = .JPG
  189. return paramter
  190. }()
  191. lazy var PNGParameter: KMBatchConvertParameter = {
  192. let paramter = KMBatchConvertParameter(file: self)
  193. paramter.convertType = .PNG
  194. return paramter
  195. }()
  196. lazy var GIFParameter: KMBatchConvertParameter = {
  197. let paramter = KMBatchConvertParameter(file: self)
  198. paramter.convertType = .GIF
  199. return paramter
  200. }()
  201. lazy var TIFFParameter: KMBatchConvertParameter = {
  202. let paramter = KMBatchConvertParameter(file: self)
  203. paramter.convertType = .TIFF
  204. return paramter
  205. }()
  206. lazy var TGAParameter: KMBatchConvertParameter = {
  207. let paramter = KMBatchConvertParameter(file: self)
  208. paramter.convertType = .TGA
  209. return paramter
  210. }()
  211. lazy var BMPParameter: KMBatchConvertParameter = {
  212. let paramter = KMBatchConvertParameter(file: self)
  213. paramter.convertType = .BMP
  214. return paramter
  215. }()
  216. lazy var JPEG2000Parameter: KMBatchConvertParameter = {
  217. let paramter = KMBatchConvertParameter(file: self)
  218. paramter.convertType = .JPEG2000
  219. return paramter
  220. }()
  221. lazy var JSONParameter: KMBatchConvertParameter = {
  222. let paramter = KMBatchConvertParameter(file: self)
  223. paramter.convertType = .Json
  224. return paramter
  225. }()
  226. init(filePath: String) {
  227. super.init()
  228. self.filePath = filePath
  229. let UTTypeString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, filePath.customPathExtension as CFString, nil)?.takeRetainedValue() as String?
  230. let TypeCFString = UTTypeString as CFString?
  231. if UTTypeConformsTo(TypeCFString!, kUTTypePDF) {
  232. self.fileType = .PDF
  233. } else if UTTypeConformsTo(TypeCFString!, kUTTypeImage) {
  234. self.fileType = .Image
  235. } else {
  236. self.fileType = .Unknow
  237. }
  238. }
  239. init(filePath: String, type: KMBatchOperationType) {
  240. super.init()
  241. self.filePath = filePath
  242. self.currentOperateType = type
  243. let UTTypeString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, filePath.customPathExtension as CFString, nil)?.takeRetainedValue() as String?
  244. let TypeCFString = UTTypeString as CFString?
  245. if UTTypeConformsTo(TypeCFString!, kUTTypePDF) {
  246. self.fileType = .PDF
  247. } else if UTTypeConformsTo(TypeCFString!, kUTTypeImage) {
  248. self.fileType = .Image
  249. } else {
  250. self.fileType = .Unknow
  251. }
  252. }
  253. override func isEqual(_ other: Any?) -> Bool {
  254. if let other = other as? Self {
  255. if self.filePath == other.filePath {
  256. return true
  257. }
  258. return false
  259. }
  260. return false
  261. }
  262. func pagesArrayIntersect(with anArray: [Any]) -> [Any] {
  263. let document = CPDFDocument(url: URL(fileURLWithPath: self.filePath))
  264. document?.unlock(withPassword: self.password)
  265. var pageNumbersArr = [Any]()
  266. let count: Int = Int(document?.pageCount ?? 0)
  267. if count > 0 {
  268. for j in 0..<count {
  269. pageNumbersArr.append(j+1)
  270. }
  271. }
  272. let set1 = NSMutableSet(array: anArray)
  273. let set2 = NSMutableSet(array: pageNumbersArr)
  274. set1.intersect(set2 as! Set<AnyHashable>)
  275. let setArray = set1.allObjects as [Any]
  276. let sortedArray = setArray.sorted { (obj1, obj2) -> Bool in
  277. let number1 = obj1 as? NSNumber ?? NSNumber()
  278. let number2 = obj2 as? NSNumber ?? NSNumber()
  279. return number1.compare(number2) == .orderedAscending
  280. }
  281. return sortedArray
  282. }
  283. var currentConvertParameter: KMBatchConvertParameter? {
  284. if self.convertType == .WordStandard {
  285. return self.standardWordParameter
  286. } else if self.convertType == .WordAdvance {
  287. return self.advanceWordParameter
  288. } else if self.convertType == .Excel {
  289. return self.excelParameter
  290. } else if self.convertType == .PowerPoint {
  291. return self.PPTParameter
  292. } else if self.convertType == .RTF {
  293. return self.RTFParameter
  294. } else if self.convertType == .CSV {
  295. return self.CSVParameter
  296. } else if self.convertType == .HTML {
  297. return self.HTMLParameter
  298. } else if self.convertType == .Text {
  299. return self.TXTParameter
  300. } else if self.convertType == .JPEG {
  301. return self.JPEGParameter
  302. } else if self.convertType == .JPG {
  303. return self.JPGParameter
  304. } else if self.convertType == .PNG {
  305. return self.PNGParameter
  306. } else if self.convertType == .GIF {
  307. return self.GIFParameter
  308. } else if self.convertType == .TIFF {
  309. return self.TIFFParameter
  310. } else if self.convertType == .TGA {
  311. return self.TGAParameter
  312. } else if self.convertType == .BMP {
  313. return self.BMPParameter
  314. } else if self.convertType == .JPEG2000 {
  315. return self.JPEG2000Parameter
  316. } else if self.convertType == .Json {
  317. return self.JSONParameter
  318. }
  319. return nil
  320. }
  321. func fileSizeString(fSize: Float) -> String {
  322. let fileSize = fSize / 1024
  323. let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024.0 : fileSize/1048576.0) : fileSize
  324. let unit: String = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
  325. return String(format: "%0.1f ", size) + unit
  326. }
  327. }