KMHeaderFooterManager.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import Foundation
  2. let kHeaderFooterFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("headerFooter")
  3. let kHeaderFooterPlistPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("headerFooter").stringByAppendingPathComponent("headerFooter2025.plist")
  4. let kHeaderFooterRemovedKey = "kHeaderFooterRemovedKey"
  5. private let kHeaderFooterInfoSaveKey = "kHeaderFooterInfoSaveKey"
  6. let kHeaderFooterManagerInit = "kHeaderFooterManagerInit"
  7. class KMHeaderFooterManager: NSObject {
  8. var headFooterObjects: [KMHeaderFooterObject] = []
  9. static let defaultManager = KMHeaderFooterManager()
  10. override init() {
  11. super.init()
  12. print("kHeaderFooterPlistPath = \(kHeaderFooterPlistPath ?? "")")
  13. if (FileManager.default.fileExists(atPath: kHeaderFooterPlistPath!)) {
  14. let dataDict = NSDictionary(contentsOfFile: kHeaderFooterPlistPath!)
  15. if (dataDict == nil) {
  16. return
  17. }
  18. for keyIndex in 0 ..< (dataDict?.allKeys.count)! {
  19. let key: String = dataDict?.allKeys[keyIndex] as! String
  20. let backgroundDict: NSDictionary = dataDict?.object(forKey: key) as! NSDictionary
  21. let model = parseDictionary(dict: backgroundDict)
  22. model.tag = key
  23. self.headFooterObjects.append(model)
  24. }
  25. }
  26. }
  27. //MARK: - 增删改查
  28. func addHeaderFooter(_ obj: KMHeaderFooterObject) -> Bool {
  29. if (!FileManager.default.fileExists(atPath: kHeaderFooterFolderPath!)) {
  30. let create: ()? = try?FileManager.default.createDirectory(atPath: kHeaderFooterFolderPath!, withIntermediateDirectories: false)
  31. if (create == nil) {
  32. return false
  33. }
  34. }
  35. if (!FileManager.default.fileExists(atPath: kHeaderFooterPlistPath!)) {
  36. let create = try?FileManager.default.createFile(atPath: kHeaderFooterPlistPath!, contents: nil)
  37. if (create == nil) {
  38. return false
  39. }
  40. }
  41. let dict = NSDictionary(contentsOfFile: kHeaderFooterPlistPath!)
  42. var newDict:NSMutableDictionary!
  43. if (dict != nil) {
  44. newDict = NSMutableDictionary(dictionary: dict!)
  45. } else {
  46. newDict = NSMutableDictionary()
  47. }
  48. let backgroundDict = self.parseModel(model: obj)
  49. if (backgroundDict.isEmpty) {
  50. let alert = NSAlert()
  51. alert.alertStyle = .critical
  52. // alert.messageText = NSLocalizedString("文件\(obj.imagePath?.lastPathComponent)已损坏", comment: "")
  53. alert.runModal()
  54. return false
  55. }
  56. let tag = obj.tag
  57. newDict.addEntries(from: [tag : backgroundDict])
  58. let result = newDict.write(toFile: kHeaderFooterPlistPath!, atomically: true)
  59. if (result) {
  60. if (self.headFooterObjects.count < 1) {
  61. self.headFooterObjects.append(obj)
  62. } else {
  63. self.headFooterObjects.insert(obj, at: 0)
  64. }
  65. }
  66. return result
  67. }
  68. func removeHeaderFooter(_ obj: KMHeaderFooterObject) -> Bool {
  69. if (obj.tag.isEmpty) {
  70. return false
  71. }
  72. if (!FileManager.default.fileExists(atPath: kHeaderFooterPlistPath!)) {
  73. return false
  74. }
  75. let key: String = obj.tag
  76. let dictionary = NSDictionary(contentsOfFile: kHeaderFooterPlistPath!)
  77. var newDictionary: NSMutableDictionary!
  78. if (dictionary != nil) {
  79. newDictionary = NSMutableDictionary(dictionary: dictionary!)
  80. } else {
  81. newDictionary = NSMutableDictionary()
  82. }
  83. newDictionary.removeObject(forKey: key)
  84. let result = newDictionary.write(toFile: kHeaderFooterPlistPath!, atomically: true)
  85. if (result) {
  86. if (self.headFooterObjects.contains(obj)) {
  87. self.headFooterObjects.removeObject(obj)
  88. }
  89. }
  90. return result
  91. }
  92. func updateHeaderFooter(theModel: KMHeaderFooterObject) -> Bool {
  93. var flagModel: KMHeaderFooterObject!
  94. for model in self.headFooterObjects {
  95. if (model.tag == theModel.tag) {
  96. flagModel = model
  97. break
  98. }
  99. }
  100. if (flagModel == nil) {
  101. return false
  102. }
  103. let dict = NSDictionary(contentsOfFile: kHeaderFooterPlistPath!)
  104. var newDict:NSMutableDictionary!
  105. if (dict != nil) {
  106. newDict = NSMutableDictionary(dictionary: dict!)
  107. } else {
  108. newDict = NSMutableDictionary()
  109. }
  110. let watermarkDict = self.parseModel(model: theModel)
  111. if (watermarkDict.isEmpty) {
  112. let alert = NSAlert()
  113. alert.alertStyle = .critical
  114. // alert.messageText = NSLocalizedString("文件\(watermark.imagePath?.lastPathComponent)已损坏", comment: "")
  115. alert.runModal()
  116. return false
  117. }
  118. newDict.setObject(watermarkDict, forKey: flagModel.tag as NSCopying)
  119. let result = newDict.write(toFile: kHeaderFooterPlistPath!, atomically: true)
  120. if (result) {
  121. if let index = self.headFooterObjects.firstIndex(of: flagModel) {
  122. self.headFooterObjects[index] = theModel
  123. }
  124. }
  125. return result
  126. }
  127. func updateModel(_ model: KMHeaderFooterObject, with dict: NSDictionary) {
  128. model.fontName = dict.object(forKey: "fontName") as! String
  129. model.fontsize = dict.object(forKey: "fontsize") as! CGFloat
  130. if let value = dict.object(forKey: "color") {
  131. model.color = NSColor.km_init(hex: value as! String)
  132. }
  133. model.leftMargin = dict.object(forKey: "leftMargin") as! Int
  134. model.rightMargin = dict.object(forKey: "rightMargin") as! Int
  135. model.bottomMargin = dict.object(forKey: "bottomMargin") as! Int
  136. model.topMargin = dict.object(forKey: "topMargin") as! Int
  137. model.topLeftString = dict.object(forKey: "topLeftString") as! String
  138. model.topCenterString = dict.object(forKey: "topCenterString") as! String
  139. model.topRightString = dict.object(forKey: "topRightString") as! String
  140. model.bottomLeftString = dict.object(forKey: "bottomLeftString") as! String
  141. model.bottomCenterString = dict.object(forKey: "bottomCenterString") as! String
  142. model.bottomRightString = dict.object(forKey: "bottomRightString") as! String
  143. model.dateFormatString = dict.object(forKey: "dateFormatString") as! String
  144. model.pageFormatString = dict.object(forKey: "pageFormatString") as! String
  145. model.startString = dict.object(forKey: "startString") as! String
  146. model.name = dict.object(forKey: "name") as! String
  147. if let value = dict.object(forKey: "tag") {
  148. model.tag = value as! String
  149. }
  150. }
  151. func updateCPDFHeaderFooter(_ headerFooter: CPDFHeaderFooter, withModel model: KMHeaderFooterObject, _ totalPDFCount: Int) {
  152. let fontSize = model.fontsize
  153. let fontName: String = model.fontName
  154. let font = NSFont.boldSystemFont(ofSize:fontSize)
  155. let style = NSMutableParagraphStyle()
  156. style.alignment = .center
  157. style.lineBreakMode = .byCharWrapping
  158. let size: NSSize = "text".boundingRect(with: NSSize(width: 1000, height: 1000), options: NSString.DrawingOptions(rawValue: 3), attributes: [NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : style]).size
  159. headerFooter.margin = NSEdgeInsetsMake(max(CGFloat(model.topMargin)-size.height, 0), CGFloat(model.leftMargin), max(CGFloat(model.bottomMargin)-size.height, 0), CGFloat(model.rightMargin))
  160. let strings = KMHeaderFooterObject.parseModel(model: model, pageCount: totalPDFCount)
  161. var count: Int = 0
  162. let color = model.color
  163. for text in strings {
  164. headerFooter.setText(text, at: UInt(count))
  165. headerFooter.setTextColor(color, at: UInt(count))
  166. headerFooter.setFontSize(fontSize, at: UInt(count))
  167. headerFooter.setFontName(fontName, at: UInt(count))
  168. count += 1
  169. }
  170. }
  171. //MARK: - Parse
  172. func parseModel(model: KMHeaderFooterObject) -> Dictionary<String, Any> {
  173. var dict: [String : Any] = [:]
  174. dict["fontName"] = model.fontName
  175. dict["fontsize"] = model.fontsize
  176. dict["color"] = model.color.toHex()
  177. dict["leftMargin"] = model.leftMargin
  178. dict["rightMargin"] = model.rightMargin
  179. dict["bottomMargin"] = model.bottomMargin
  180. dict["topMargin"] = model.topMargin
  181. dict["topLeftString"] = model.topLeftString
  182. dict["topCenterString"] = model.topCenterString
  183. dict["topRightString"] = model.topRightString
  184. dict["bottomLeftString"] = model.bottomLeftString
  185. dict["bottomCenterString"] = model.bottomCenterString
  186. dict["bottomRightString"] = model.bottomRightString
  187. dict["dateFormatString"] = model.dateFormatString
  188. dict["pageFormatString"] = model.pageFormatString
  189. dict["startString"] = model.startString
  190. dict["name"] = model.name
  191. dict["tag"] = model.tag
  192. return dict
  193. }
  194. private func parseDictionary(dict: NSDictionary) -> KMHeaderFooterObject {
  195. let model = KMHeaderFooterObject()
  196. model.fontName = dict.object(forKey: "fontName") as! String
  197. model.fontsize = dict.object(forKey: "fontsize") as! CGFloat
  198. if let value = dict.object(forKey: "color") {
  199. model.color = NSColor.km_init(hex: value as! String)
  200. }
  201. model.leftMargin = dict.object(forKey: "leftMargin") as! Int
  202. model.rightMargin = dict.object(forKey: "rightMargin") as! Int
  203. model.bottomMargin = dict.object(forKey: "bottomMargin") as! Int
  204. model.topMargin = dict.object(forKey: "topMargin") as! Int
  205. model.topLeftString = dict.object(forKey: "topLeftString") as! String
  206. model.topCenterString = dict.object(forKey: "topCenterString") as! String
  207. model.topRightString = dict.object(forKey: "topRightString") as! String
  208. model.bottomLeftString = dict.object(forKey: "bottomLeftString") as! String
  209. model.bottomCenterString = dict.object(forKey: "bottomCenterString") as! String
  210. model.bottomRightString = dict.object(forKey: "bottomRightString") as! String
  211. model.dateFormatString = dict.object(forKey: "dateFormatString") as! String
  212. model.pageFormatString = dict.object(forKey: "pageFormatString") as! String
  213. model.startString = dict.object(forKey: "startString") as! String
  214. model.name = dict.object(forKey: "name") as! String
  215. model.tag = dict.object(forKey: "tag") as! String
  216. return model
  217. }
  218. //MARK: - Get
  219. func fetchHeaderFooterAvailableName() -> String {
  220. var availableIndex = 0
  221. for item in headFooterObjects {
  222. if item.name.hasPrefix("HeaderFooter") {
  223. if let index = Int(item.name.dropFirst("HeaderFooter".count)), index >= availableIndex {
  224. availableIndex = index + 1
  225. }
  226. }
  227. }
  228. return "HeaderFooter\(availableIndex)"
  229. }
  230. //MARK: - Compare
  231. class func compareIsChangedModel(_ model: KMHeaderFooterObject, withDict dict: NSDictionary) -> Bool {
  232. if let value = dict["fontName"] {
  233. if model.fontName != (value as! String) {
  234. return true
  235. }
  236. }
  237. if let value = dict["fontsize"] {
  238. if model.fontsize != (value as! CGFloat) {
  239. return true
  240. }
  241. }
  242. if let value = dict["color"] {
  243. if model.color.toHex() != (value as! String) {
  244. return true
  245. }
  246. }
  247. if let value = dict["leftMargin"] {
  248. if model.leftMargin != (value as! Int) {
  249. return true
  250. }
  251. }
  252. if let value = dict["rightMargin"] {
  253. if model.rightMargin != (value as! Int) {
  254. return true
  255. }
  256. }
  257. if let value = dict["bottomMargin"] {
  258. if model.bottomMargin != (value as! Int) {
  259. return true
  260. }
  261. }
  262. if let value = dict["topMargin"] {
  263. if model.topMargin != (value as! Int) {
  264. return true
  265. }
  266. }
  267. if let value = dict["topLeftString"] {
  268. if model.topLeftString != (value as! String) {
  269. return true
  270. }
  271. }
  272. if let value = dict["topCenterString"] {
  273. if model.topCenterString != (value as! String) {
  274. return true
  275. }
  276. }
  277. if let value = dict["topRightString"] {
  278. if model.topRightString != (value as! String) {
  279. return true
  280. }
  281. }
  282. if let value = dict["bottomLeftString"] {
  283. if model.bottomLeftString != (value as! String) {
  284. return true
  285. }
  286. }
  287. if let value = dict["bottomCenterString"] {
  288. if model.bottomCenterString != (value as! String) {
  289. return true
  290. }
  291. }
  292. if let value = dict["bottomRightString"] {
  293. if model.bottomRightString != (value as! String) {
  294. return true
  295. }
  296. }
  297. if let value = dict["dateFormatString"] {
  298. if model.dateFormatString != (value as! String) {
  299. return true
  300. }
  301. }
  302. if let value = dict["pageFormatString"] {
  303. if model.pageFormatString != (value as! String) {
  304. return true
  305. }
  306. }
  307. if let value = dict["startString"] {
  308. if model.startString != (value as! String) {
  309. return true
  310. }
  311. }
  312. return false
  313. }
  314. }
  315. extension KMHeaderFooterManager {
  316. class func getdateFormatArray() -> [String] {
  317. return [
  318. "m/d",
  319. "m/d/yy",
  320. "m/d/yyyy",
  321. "mm/dd/yy",
  322. "mm/dd/yyyy",
  323. "d/m/yy",
  324. "d/m/yyyy",
  325. "dd/mm/yy",
  326. "dd/mm/yyyy",
  327. "mm/yy",
  328. "mm/yyyy",
  329. "m.d.yy",
  330. "m.d.yyyy",
  331. "mm.dd.yy",
  332. "mm.dd.yyyy",
  333. "mm.yy",
  334. "mm.yyyy",
  335. "d.m.yy",
  336. "d.m.yyyy",
  337. "dd.mm.yy",
  338. "dd.mm.yyyy",
  339. "yy-mm-dd",
  340. "yyyy-mm-dd"
  341. ]
  342. }
  343. class func getPageFormats() -> [String] {
  344. return ["1",
  345. "1 of n",
  346. "1/n",
  347. "Page 1",
  348. "Page 1 of n"]
  349. }
  350. class func getFontSize() -> [String] {
  351. return ["6","8","10","12","14",
  352. "16","18","20","22","24",
  353. "26","28","30","32","34",
  354. "36","40","48","64","80",
  355. "96","112"]
  356. }
  357. }