KMHeaderFooterManager.swift 15 KB

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