DSignatureApperanceManager.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // DSignatureApperanceManager.swift
  3. // Cisdem PDFMaster
  4. //
  5. // Created by Niehaoyu on 2023/10/8.
  6. //
  7. import Cocoa
  8. class DSignatureAppearanceData: NSObject {
  9. var appearanceName = NSString()
  10. var signatureConfig = CPDFSignatureConfig()
  11. var tag = NSString()
  12. var drawType: CDSignatureCustomType = .none
  13. var isInputDSignatureText: Bool = false
  14. override init() {
  15. super.init()
  16. appearanceName = ""
  17. }
  18. }
  19. class DSignatureApperanceManager: NSObject {
  20. @objc public static let manager = DSignatureApperanceManager.init()
  21. var signatures: NSMutableArray!
  22. override init() {
  23. super.init()
  24. self.signatures = NSMutableArray.init()
  25. }
  26. @objc func tagString() -> NSString {
  27. let dateFormatter = DateFormatter.init()
  28. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  29. let dateString = dateFormatter.string(from: NSDate() as Date)
  30. return NSString(format: "%@ %d", dateString, arc4random()%10000)
  31. }
  32. func getFileSize(atPath filePath : String) -> CGFloat {
  33. guard let dict = try? FileManager.default.attributesOfItem(atPath: filePath) as NSDictionary else {
  34. return 0
  35. }
  36. return CGFloat(dict.fileSize())
  37. }
  38. func sortContens(_ contens: NSArray) -> NSArray {
  39. let tContens = NSMutableArray()
  40. var nameItem: CPDFSignatureConfigItem!
  41. var dnItem: CPDFSignatureConfigItem!
  42. var reaItem: CPDFSignatureConfigItem!
  43. var locaItem: CPDFSignatureConfigItem!
  44. var dateItem: CPDFSignatureConfigItem!
  45. var verItem: CPDFSignatureConfigItem!
  46. for item in contens {
  47. if (item as! CPDFSignatureConfigItem).key == NAME_KEY {
  48. nameItem = item as? CPDFSignatureConfigItem
  49. } else if (item as! CPDFSignatureConfigItem).key == DN_KEY {
  50. dnItem = item as? CPDFSignatureConfigItem
  51. } else if (item as! CPDFSignatureConfigItem).key == REASON_KEY {
  52. reaItem = item as? CPDFSignatureConfigItem
  53. } else if (item as! CPDFSignatureConfigItem).key == LOCATION_KEY {
  54. locaItem = item as? CPDFSignatureConfigItem
  55. } else if (item as! CPDFSignatureConfigItem).key == DATE_KEY {
  56. dateItem = item as? CPDFSignatureConfigItem
  57. } else if (item as! CPDFSignatureConfigItem).key == VERSION_KEY {
  58. verItem = item as? CPDFSignatureConfigItem
  59. }
  60. }
  61. if nameItem != nil {
  62. tContens.add(nameItem as Any)
  63. }
  64. if dnItem != nil {
  65. tContens.add(dnItem as Any)
  66. }
  67. if reaItem != nil {
  68. tContens.add(reaItem as Any)
  69. }
  70. if locaItem != nil {
  71. tContens.add(locaItem as Any)
  72. }
  73. if dateItem != nil {
  74. tContens.add(dateItem as Any)
  75. }
  76. if verItem != nil {
  77. tContens.add(verItem as Any)
  78. }
  79. return tContens
  80. }
  81. //MARK: Load
  82. public func loadDSignatureAppearanceDatas() -> NSMutableArray{
  83. self.signatures.removeAllObjects()
  84. if FileManager.default.fileExists(atPath: kDSignaturePlistPath) {
  85. let dictionary = NSDictionary(contentsOfFile: kDSignaturePlistPath)!
  86. for key in dictionary.allKeys {
  87. let dic: NSDictionary = dictionary[key] as! NSDictionary
  88. let data: DSignatureAppearanceData = DSignatureAppearanceData.init()
  89. data.tag = key as! NSString
  90. data.appearanceName = dic[kDSignatureOfAppearanceKey] as! NSString
  91. if (dic.value(forKey: kDSignatureOfDrawTypeKey) != nil) {
  92. data.drawType = CDSignatureCustomType(rawValue: dic[kDSignatureOfDrawTypeKey] as! NSInteger)!
  93. } else {
  94. data.drawType = .none
  95. }
  96. data.signatureConfig = self.switchDictionaryToWatermark(dic)
  97. data.isInputDSignatureText = (dic[kDSignatureOfIsTextKey] != nil)
  98. self.signatures.add(data)
  99. }
  100. }
  101. if self.signatures.count < 1 {
  102. let data = self.standardDSignatureAppearanceData()
  103. data.tag = self.tagString()
  104. data.appearanceName = NSLocalizedString("Standard Text", comment: "") as NSString
  105. _ = self.addDSignatureAppearanceData(watermark: data)
  106. }
  107. return self.signatures
  108. }
  109. func getLastDSignatureAppearanceData() -> DSignatureAppearanceData {
  110. let data = DSignatureAppearanceData.init()
  111. var info: [String : Any] = [:]
  112. if (UserDefaults.standard.value(forKey: kLastAddDSignatureDataKey) == nil) {
  113. } else {
  114. info = (UserDefaults.standard.value(forKey: kLastAddDSignatureDataKey) as! [String : Any])
  115. }
  116. var watermarkDic = NSMutableDictionary(dictionary: info)
  117. if watermarkDic.allKeys.count > 0 {
  118. data.tag = watermarkDic.allKeys.first as! NSString
  119. var appearanceDic: [String : Any] = [:]
  120. if (watermarkDic.object(forKey: data.tag as Any) == nil) {
  121. } else {
  122. appearanceDic = watermarkDic.object(forKey: data.tag as Any) as! [String : Any]
  123. data.appearanceName = appearanceDic[kDSignatureOfAppearanceKey] as! NSString
  124. let typeA: CDSignatureCustomType = appearanceDic[kDSignatureOfDrawTypeKey] as? CDSignatureCustomType ?? .none
  125. data.drawType = typeA
  126. data.signatureConfig = self.switchDictionaryToWatermark(appearanceDic as NSDictionary)
  127. data.isInputDSignatureText = (appearanceDic[kDSignatureOfIsTextKey] != nil)
  128. }
  129. }
  130. return data
  131. }
  132. //MARK: Get
  133. func standardDSignatureAppearanceData() -> DSignatureAppearanceData {
  134. let data = DSignatureAppearanceData.init()
  135. data.drawType = .text
  136. let config = CPDFSignatureConfig.init()
  137. config.isDrawLogo = true
  138. config.isContentAlginLeft = false
  139. config.isDrawKey = true
  140. #if VERSION_FREE
  141. #if VERSION_DMG
  142. config.logo = NSImage(named: "PDFReaderProLogo")
  143. #else
  144. config.logo = NSImage(named: "PDFReaderProLogo")
  145. #endif
  146. #else
  147. config.logo = NSImage(named: "PDFReaderProEditionLogo")
  148. #endif
  149. let nameItem = CPDFSignatureConfigItem.init()
  150. nameItem.key = NAME_KEY
  151. nameItem.value = NSLocalizedString("<your common name here>", comment: "")
  152. let dateItem = CPDFSignatureConfigItem.init()
  153. dateItem.key = DATE_KEY
  154. let dateFormatter = DateFormatter.init()
  155. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  156. dateItem.value = dateFormatter.string(from: NSDate() as Date)
  157. let contents = NSMutableArray()
  158. contents.add(nameItem)
  159. contents.add(dateItem)
  160. config.contents = self.sortContens(contents) as? [CPDFSignatureConfigItem]
  161. data.signatureConfig = config
  162. data.isInputDSignatureText = false
  163. return data
  164. }
  165. //MARK: Switch
  166. func switchDictionaryToWatermark(_ dic: NSDictionary) -> CPDFSignatureConfig {
  167. let config = CPDFSignatureConfig.init()
  168. var contents = Array<Any>.init()
  169. for key in dic.allKeys {
  170. if key as! String == ISDRAWLOGO_KEY {
  171. if let isDrawLogo = dic[ISDRAWLOGO_KEY] as? Bool {
  172. config.isDrawLogo = isDrawLogo
  173. }
  174. } else if key as! String == ISDRAW_KEY {
  175. config.isDrawKey = (dic[ISDRAW_KEY] != nil)
  176. } else if key as! String == ISCONTENTALGINLEGF_KEY {
  177. if let isContentAlignLeft = dic[ISCONTENTALGINLEGF_KEY] as? Bool {
  178. config.isContentAlginLeft = isContentAlignLeft
  179. }
  180. } else if key as! String == kDSignatureOfTextKey {
  181. config.text = dic[kDSignatureOfTextKey] as? String
  182. } else if key as! String == kDSignatureOfImagePathKey {
  183. guard let keyValue = dic[kDSignatureOfImagePathKey] as? String else {
  184. return config
  185. }
  186. let filePath = kDSignatureFolderPath + "/" + keyValue
  187. if FileManager.default.fileExists(atPath: filePath) {
  188. config.image = NSImage.init(contentsOfFile: filePath)
  189. }
  190. } else {
  191. let item = CPDFSignatureConfigItem.init()
  192. item.key = NSLocalizedString(key as! String, comment: "")
  193. item.value = dic[key] as? String
  194. contents.append(item)
  195. }
  196. }
  197. // if config.isDrawLogo {
  198. #if VERSION_FREE
  199. #if VERSION_DMG
  200. config.logo = NSImage(named: "PDFReaderProLogo")
  201. #else
  202. config.logo = NSImage(named: "PDFReaderProLogo")
  203. #endif
  204. #else
  205. config.logo = NSImage(named: "PDFReaderProEditionLogo")
  206. #endif
  207. // }
  208. config.contents = self.sortContens(contents as NSArray) as? [CPDFSignatureConfigItem]
  209. return config
  210. }
  211. func switchDSignatureToDictionary(signatureConfig: CPDFSignatureConfig) -> NSDictionary {
  212. let configDictionary = NSMutableDictionary.init()
  213. if signatureConfig.isDrawLogo {
  214. configDictionary.setObject((1), forKey: ISDRAWLOGO_KEY as NSCopying)
  215. } else {
  216. configDictionary.setObject((0), forKey: ISDRAWLOGO_KEY as NSCopying)
  217. }
  218. if signatureConfig.isDrawKey {
  219. configDictionary.setObject((1), forKey: ISDRAW_KEY as NSCopying)
  220. } else {
  221. configDictionary.setObject((0), forKey: ISDRAW_KEY as NSCopying)
  222. }
  223. if signatureConfig.isContentAlginLeft {
  224. configDictionary.setObject((1), forKey: ISCONTENTALGINLEGF_KEY as NSCopying)
  225. } else {
  226. configDictionary.setObject((0), forKey: ISCONTENTALGINLEGF_KEY as NSCopying)
  227. }
  228. if signatureConfig.text != nil {
  229. configDictionary.setObject(signatureConfig.text as String, forKey: kDSignatureOfTextKey as NSCopying)
  230. }
  231. if signatureConfig.image != nil {
  232. let image = signatureConfig.image
  233. let data = image?.tiffRepresentation
  234. let imageRep = NSBitmapImageRep(data: data!)
  235. imageRep?.size = image!.size
  236. var imageData: NSData?
  237. imageData = imageRep?.representation(using: .png, properties: [:]) as NSData?
  238. let tag = self.tagString()
  239. let rPath = kDSignatureFolderPath.appending("/\(tag).png")
  240. imageData?.write(toFile: rPath, atomically: true)
  241. var pathComponent: NSString?
  242. pathComponent = NSString(format: "%@/%@.png", kDSignatureFolderPath, tag)
  243. configDictionary.setObject(pathComponent?.lastPathComponent as Any, forKey: kDSignatureOfImagePathKey as NSCopying)
  244. }
  245. for item in signatureConfig.contents {
  246. configDictionary.setValue(item.value, forKey: item.key)
  247. }
  248. return configDictionary as NSDictionary
  249. }
  250. //MARK: Add
  251. func addDSignatureAppearanceData(watermark: DSignatureAppearanceData) -> Bool {
  252. if FileManager.default.fileExists(atPath: kDSignatureFolderPath) == false {
  253. if FileManager.default.fileExists(atPath: ApplicationSupportDirectory) == false {
  254. try?FileManager.default.createDirectory(atPath: ApplicationSupportDirectory, withIntermediateDirectories: false)
  255. }
  256. try?FileManager.default.createDirectory(atPath: kDSignatureFolderPath, withIntermediateDirectories: false)
  257. if FileManager.default.fileExists(atPath: kDSignatureFolderPath) == false {
  258. return false
  259. }
  260. }
  261. if FileManager.default.fileExists(atPath: kDSignaturePlistPath) == false {
  262. if FileManager.default.createFile(atPath: kDSignaturePlistPath, contents: nil, attributes: nil) == false {
  263. return false
  264. }
  265. }
  266. var dictionary = NSDictionary.init()
  267. if self.getFileSize(atPath: kDSignaturePlistPath) > 0 {
  268. dictionary = NSDictionary(contentsOfFile: kDSignaturePlistPath)!
  269. }
  270. let newDictionary = NSMutableDictionary(dictionary: dictionary)
  271. let tag = watermark.tag
  272. let appearanceDic = self.switchDSignatureToDictionary(signatureConfig: watermark.signatureConfig)
  273. appearanceDic.setValue(watermark.appearanceName, forKey: kDSignatureOfAppearanceKey)
  274. appearanceDic.setValue((watermark.drawType.rawValue), forKey: kDSignatureOfDrawTypeKey)
  275. if watermark.isInputDSignatureText {
  276. appearanceDic.setValue((1), forKey: kDSignatureOfIsTextKey)
  277. } else {
  278. appearanceDic.setValue((0), forKey: kDSignatureOfIsTextKey)
  279. }
  280. newDictionary.setObject(appearanceDic, forKey: tag)
  281. if newDictionary.write(toFile: kDSignaturePlistPath, atomically: true) {
  282. if self.signatures.count < 1 {
  283. self.signatures.add(watermark)
  284. } else {
  285. self.signatures.insert(watermark, at: 0)
  286. }
  287. return true
  288. }
  289. return false
  290. }
  291. //MARK: Remove
  292. func removeDSignatureAppearanceData(watermark: DSignatureAppearanceData) -> Bool {
  293. let key = watermark.tag
  294. if key.length == 0 {
  295. return false
  296. }
  297. if FileManager.default.fileExists(atPath: kDSignaturePlistPath) == false {
  298. return false
  299. }
  300. let dictionary = NSDictionary(contentsOfFile: kDSignaturePlistPath)!
  301. let newDictionary = NSMutableDictionary(dictionary: dictionary)
  302. newDictionary.removeObject(forKey: key as Any)
  303. if newDictionary.write(toFile: kDSignaturePlistPath, atomically: true) {
  304. self.signatures.remove(watermark)
  305. return true
  306. }
  307. return false
  308. }
  309. //MARK: Save
  310. func saveDSignatureAppearanceData(watermark: DSignatureAppearanceData) ->() {
  311. let watermarkDictionary = NSMutableDictionary()
  312. let appearanceDic = self.switchDSignatureToDictionary(signatureConfig: watermark.signatureConfig)
  313. appearanceDic.setValue(watermark.appearanceName, forKey: kDSignatureOfAppearanceKey)
  314. appearanceDic.setValue((watermark.drawType.rawValue), forKey: kDSignatureOfDrawTypeKey)
  315. if watermark.isInputDSignatureText {
  316. appearanceDic.setValue((1), forKey: kDSignatureOfIsTextKey)
  317. } else {
  318. appearanceDic.setValue((0), forKey: kDSignatureOfIsTextKey)
  319. }
  320. watermarkDictionary.setObject(appearanceDic, forKey: watermark.tag)
  321. UserDefaults.standard.setValue(watermarkDictionary, forKey: kLastAddDSignatureDataKey)
  322. UserDefaults.standard.setValue((1), forKey: "kHaveSaveWatermarkData")
  323. UserDefaults.standard.synchronize()
  324. }
  325. }