KMBatchOperateFile.swift 12 KB

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