123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- //
- // KMBatchOperateFile.swift
- // PDF Reader Pro
- //
- // Created by kdanmobile on 2023/10/25.
- //
- import Cocoa
- @objcMembers class KMBatchOperateFile: NSObject{
- /**基本信息*/
- var password: String = ""
- var filePath: String = ""
- var outputFilePath: String = ""
- lazy var sizeString: String = {
- guard let dict = try? FileManager.default.attributesOfItem(atPath: self.filePath) as NSDictionary else {
- return "0"
- }
- let _sizeString = self.fileSizeString(fSize: Float(dict.fileSize()))
- return _sizeString
- }()
- var fileType: KMBatchOperateFileType?
-
- // 是否使用OCR
- var isAllowOCR = false
- var ocrLanguage: COCRLanguage?
-
- /*当前正在进行的任务状态*/
- var error: NSError? {
- return self.currentOperateInfo?.error
- }
- var status: KMBatchOperateStatus?{
- return self.currentOperateInfo?.status
- }
- var dpi: Int? {
- if self.currentOperateType == .Convert{
- return self.currentConvertParameter?.dpi
- }
- return 0
- }
- var progress: Float?{
- return self.currentOperateInfo?.progress
- }
- var currentOperateType: KMBatchOperationType?
- var convertType: KMConvertWithPDFType = .WordAdvance
- /*当前的操作详细信息**/
- lazy var createPDFInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- var convertInfo: KMBatchConvertParameter?{
- return self.currentConvertParameter
- }
- lazy var mergeInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- lazy var compressInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- lazy var ocrInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- lazy var addPasswordInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- lazy var removePasswordInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- lazy var addWatermarkInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- lazy var removeWatermarkInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- lazy var addBackgroundInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- lazy var removeBackgroundInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- lazy var addHeaderFooterInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- lazy var removeHeaderFooterInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- lazy var addBatesInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- lazy var removeBatesInfo: KMBatchBaseParameter = {
- let info = KMBatchBaseParameter(file: self)
- return info
- }()
- var currentOperateInfo: KMBatchBaseParameter?{
- switch self.currentOperateType {
- case .CreatePDF:
- return self.createPDFInfo
- case .Convert:
- return self.convertInfo
- case .Merge:
- return self.mergeInfo
- case .Compress:
- return self.compressInfo
- case .OCR:
- return self.ocrInfo
- case .AddPassword:
- return self.addPasswordInfo
- case .RemovePassword:
- return self.removePasswordInfo
- case .AddWatermark:
- return self.addWatermarkInfo
- case .RemoveWatermark:
- return self.removeWatermarkInfo
- case .AddBackground:
- return self.addBackgroundInfo
- case .RemoveBackground:
- return self.removeBackgroundInfo
- case .AddHeaderFooter:
- return self.addHeaderFooterInfo
- case .RemoveHeaderFooter:
- return self.removeHeaderFooterInfo
- case .AddBates:
- return self.addBatesInfo
- case .RemoveBates:
- return self.removeBatesInfo
- case .none:
- return nil
- case .some(.NotDetermined):
- return nil
- }
- }
- /*转换详细信息**/
- lazy var standardWordParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .WordStandard
- return paramter
- }()
- lazy var advanceWordParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .WordAdvance
- return paramter
- }()
- lazy var excelParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .Excel
- return paramter
- }()
- lazy var PPTParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .PowerPoint
- return paramter
- }()
- lazy var RTFParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .RTF
- return paramter
- }()
- lazy var CSVParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .CSV
- return paramter
- }()
- lazy var HTMLParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .HTML
- return paramter
- }()
- lazy var TXTParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .Text
- return paramter
- }()
- lazy var JPEGParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .JPEG
- return paramter
- }()
- lazy var JPGParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .JPG
- return paramter
- }()
- lazy var PNGParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .PNG
- return paramter
- }()
- lazy var GIFParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .GIF
- return paramter
- }()
- lazy var TIFFParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .TIFF
- return paramter
- }()
- lazy var TGAParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .TGA
- return paramter
- }()
- lazy var BMPParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .BMP
- return paramter
- }()
- lazy var JPEG2000Parameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .JPEG2000
- return paramter
- }()
- lazy var JSONParameter: KMBatchConvertParameter = {
- let paramter = KMBatchConvertParameter(file: self)
- paramter.convertType = .Json
- return paramter
- }()
-
- init(filePath: String) {
- super.init()
- self.filePath = filePath
-
- let UTTypeString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, filePath.customPathExtension as CFString, nil)?.takeRetainedValue() as String?
- let TypeCFString = UTTypeString as CFString?
- if UTTypeConformsTo(TypeCFString!, kUTTypePDF) {
- self.fileType = .PDF
- } else if UTTypeConformsTo(TypeCFString!, kUTTypeImage) {
- self.fileType = .Image
- } else {
- self.fileType = .Unknow
- }
- }
- init(filePath: String, type: KMBatchOperationType) {
- super.init()
- self.filePath = filePath
- self.currentOperateType = type
- let UTTypeString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, filePath.customPathExtension as CFString, nil)?.takeRetainedValue() as String?
- let TypeCFString = UTTypeString as CFString?
- if UTTypeConformsTo(TypeCFString!, kUTTypePDF) {
- self.fileType = .PDF
- } else if UTTypeConformsTo(TypeCFString!, kUTTypeImage) {
- self.fileType = .Image
- } else {
- self.fileType = .Unknow
- }
- }
- override func isEqual(_ other: Any?) -> Bool {
- if let other = other as? Self {
- if self.filePath == other.filePath {
- return true
- }
- return false
- }
- return false
- }
-
- func pagesArrayIntersect(with anArray: [Any]) -> [Any] {
- let document = CPDFDocument(url: URL(fileURLWithPath: self.filePath))
- document?.unlock(withPassword: self.password)
- var pageNumbersArr = [Any]()
- let count: Int = Int(document?.pageCount ?? 0)
- if count > 0 {
- for j in 0..<count {
- pageNumbersArr.append(j+1)
- }
- }
-
- let set1 = NSMutableSet(array: anArray)
- let set2 = NSMutableSet(array: pageNumbersArr)
- set1.intersect(set2 as! Set<AnyHashable>)
- let setArray = set1.allObjects as [Any]
- let sortedArray = setArray.sorted { (obj1, obj2) -> Bool in
- let number1 = obj1 as? NSNumber ?? NSNumber()
- let number2 = obj2 as? NSNumber ?? NSNumber()
- return number1.compare(number2) == .orderedAscending
- }
-
- return sortedArray
- }
-
- var currentConvertParameter: KMBatchConvertParameter? {
- if self.convertType == .WordStandard {
- return self.standardWordParameter
- } else if self.convertType == .WordAdvance {
- return self.advanceWordParameter
- } else if self.convertType == .Excel {
- return self.excelParameter
- } else if self.convertType == .PowerPoint {
- return self.PPTParameter
- } else if self.convertType == .RTF {
- return self.RTFParameter
- } else if self.convertType == .CSV {
- return self.CSVParameter
- } else if self.convertType == .HTML {
- return self.HTMLParameter
- } else if self.convertType == .Text {
- return self.TXTParameter
- } else if self.convertType == .JPEG {
- return self.JPEGParameter
- } else if self.convertType == .JPG {
- return self.JPGParameter
- } else if self.convertType == .PNG {
- return self.PNGParameter
- } else if self.convertType == .GIF {
- return self.GIFParameter
- } else if self.convertType == .TIFF {
- return self.TIFFParameter
- } else if self.convertType == .TGA {
- return self.TGAParameter
- } else if self.convertType == .BMP {
- return self.BMPParameter
- } else if self.convertType == .JPEG2000 {
- return self.JPEG2000Parameter
- } else if self.convertType == .Json {
- return self.JSONParameter
- }
- return nil
- }
- func fileSizeString(fSize: Float) -> String {
- let fileSize = fSize / 1024
- let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024.0 : fileSize/1048576.0) : fileSize
- let unit: String = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
- return String(format: "%0.1f ", size) + unit
- }
- }
|