KMHeaderFooterManager.swift 15 KB

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