KMHeaderFooterManager.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 removeAllHeaderFooters() -> Bool {
  94. if (!FileManager.default.fileExists(atPath: kHeaderFooterPlistPath!)) {
  95. return false
  96. }
  97. let dictionary = NSDictionary(contentsOfFile: kHeaderFooterPlistPath!)
  98. var newDictionary: NSMutableDictionary!
  99. if (dictionary != nil) {
  100. newDictionary = NSMutableDictionary(dictionary: dictionary!)
  101. } else {
  102. newDictionary = NSMutableDictionary()
  103. }
  104. newDictionary.removeAllObjects()
  105. let result = newDictionary.write(toFile: kHeaderFooterPlistPath!, atomically: true)
  106. if (result) {
  107. self.headFooterObjects.removeAll()
  108. }
  109. return result
  110. }
  111. func updateHeaderFooter(theModel: KMHeaderFooterModel) -> Bool {
  112. var flagModel: KMHeaderFooterModel!
  113. for model in self.headFooterObjects {
  114. if (model.tag == theModel.tag) {
  115. flagModel = model
  116. break
  117. }
  118. }
  119. if (flagModel == nil) {
  120. return false
  121. }
  122. let dict = NSDictionary(contentsOfFile: kHeaderFooterPlistPath!)
  123. var newDict:NSMutableDictionary!
  124. if (dict != nil) {
  125. newDict = NSMutableDictionary(dictionary: dict!)
  126. } else {
  127. newDict = NSMutableDictionary()
  128. }
  129. let watermarkDict = self.parseModel(model: theModel)
  130. newDict.setObject(watermarkDict, forKey: flagModel.tag as NSCopying)
  131. let result = newDict.write(toFile: kHeaderFooterPlistPath!, atomically: true)
  132. if (result) {
  133. if let index = self.headFooterObjects.firstIndex(of: flagModel) {
  134. self.headFooterObjects[index] = theModel
  135. }
  136. }
  137. return result
  138. }
  139. func updateModel(_ model: KMHeaderFooterModel, with dict: NSDictionary) {
  140. model.fontName = dict.object(forKey: "fontName") as! String
  141. model.fontsize = dict.object(forKey: "fontsize") as! CGFloat
  142. if let value = dict.object(forKey: "color") {
  143. model.color = NSColor.km_init(hex: value as! String)
  144. }
  145. model.leftMargin = dict.object(forKey: "leftMargin") as! Int
  146. model.rightMargin = dict.object(forKey: "rightMargin") as! Int
  147. model.bottomMargin = dict.object(forKey: "bottomMargin") as! Int
  148. model.topMargin = dict.object(forKey: "topMargin") as! Int
  149. model.topLeftString = dict.object(forKey: "topLeftString") as! String
  150. model.topCenterString = dict.object(forKey: "topCenterString") as! String
  151. model.topRightString = dict.object(forKey: "topRightString") as! String
  152. model.bottomLeftString = dict.object(forKey: "bottomLeftString") as! String
  153. model.bottomCenterString = dict.object(forKey: "bottomCenterString") as! String
  154. model.bottomRightString = dict.object(forKey: "bottomRightString") as! String
  155. model.dateFormatString = dict.object(forKey: "dateFormatString") as! String
  156. model.pageFormatString = dict.object(forKey: "pageFormatString") as! String
  157. model.startString = dict.object(forKey: "startString") as! String
  158. model.name = dict.object(forKey: "name") as! String
  159. if let value = dict.object(forKey: "tag") {
  160. model.tag = value as! String
  161. }
  162. }
  163. func updateCPDFHeaderFooter(_ headerFooter: CPDFHeaderFooter, withModel model: KMHeaderFooterModel, _ totalPDFCount: Int) {
  164. let fontSize = model.fontsize
  165. let fontName: String = model.fontName
  166. let font = NSFont.boldSystemFont(ofSize:fontSize)
  167. let style = NSMutableParagraphStyle()
  168. style.alignment = .center
  169. style.lineBreakMode = .byCharWrapping
  170. 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
  171. headerFooter.margin = NSEdgeInsetsMake(max(CGFloat(model.topMargin)-size.height, 0), CGFloat(model.leftMargin), max(CGFloat(model.bottomMargin)-size.height, 0), CGFloat(model.rightMargin))
  172. let strings = KMHeaderFooterModel.parseModel(model: model, pageCount: totalPDFCount)
  173. var count: Int = 0
  174. let color = model.color
  175. for text in strings {
  176. headerFooter.setText(text, at: UInt(count))
  177. headerFooter.setTextColor(color, at: UInt(count))
  178. headerFooter.setFontSize(fontSize, at: UInt(count))
  179. headerFooter.setFontName(fontName, at: UInt(count))
  180. count += 1
  181. }
  182. }
  183. //MARK: - Parse
  184. func parseModel(model: KMHeaderFooterModel) -> Dictionary<String, Any> {
  185. var dict: [String : Any] = [:]
  186. dict["fontName"] = model.fontName
  187. dict["fontsize"] = model.fontsize
  188. dict["color"] = model.color.toHex()
  189. dict["leftMargin"] = model.leftMargin
  190. dict["rightMargin"] = model.rightMargin
  191. dict["bottomMargin"] = model.bottomMargin
  192. dict["topMargin"] = model.topMargin
  193. dict["topLeftString"] = model.topLeftString
  194. dict["topCenterString"] = model.topCenterString
  195. dict["topRightString"] = model.topRightString
  196. dict["bottomLeftString"] = model.bottomLeftString
  197. dict["bottomCenterString"] = model.bottomCenterString
  198. dict["bottomRightString"] = model.bottomRightString
  199. dict["dateFormatString"] = model.dateFormatString
  200. dict["pageFormatString"] = model.pageFormatString
  201. dict["startString"] = model.startString
  202. dict["name"] = model.name
  203. dict["tag"] = model.tag
  204. return dict
  205. }
  206. private func parseDictionary(dict: NSDictionary) -> KMHeaderFooterModel {
  207. let model = KMHeaderFooterModel()
  208. model.fontName = dict.object(forKey: "fontName") as! String
  209. model.fontsize = dict.object(forKey: "fontsize") as! CGFloat
  210. if let value = dict.object(forKey: "color") {
  211. model.color = NSColor.km_init(hex: value as! String)
  212. }
  213. model.leftMargin = dict.object(forKey: "leftMargin") as! Int
  214. model.rightMargin = dict.object(forKey: "rightMargin") as! Int
  215. model.bottomMargin = dict.object(forKey: "bottomMargin") as! Int
  216. model.topMargin = dict.object(forKey: "topMargin") as! Int
  217. model.topLeftString = dict.object(forKey: "topLeftString") as! String
  218. model.topCenterString = dict.object(forKey: "topCenterString") as! String
  219. model.topRightString = dict.object(forKey: "topRightString") as! String
  220. model.bottomLeftString = dict.object(forKey: "bottomLeftString") as! String
  221. model.bottomCenterString = dict.object(forKey: "bottomCenterString") as! String
  222. model.bottomRightString = dict.object(forKey: "bottomRightString") as! String
  223. model.dateFormatString = dict.object(forKey: "dateFormatString") as! String
  224. model.pageFormatString = dict.object(forKey: "pageFormatString") as! String
  225. model.startString = dict.object(forKey: "startString") as! String
  226. model.name = dict.object(forKey: "name") as! String
  227. model.tag = dict.object(forKey: "tag") as! String
  228. return model
  229. }
  230. //MARK: - Get
  231. func fetchHeaderFooterAvailableName() -> String {
  232. var availableIndex = 1
  233. for item in headFooterObjects {
  234. if item.name.hasPrefix("HeaderFooter-") {
  235. if let index = Int(item.name.dropFirst("HeaderFooter-".count)), index >= availableIndex {
  236. availableIndex = index + 1
  237. }
  238. }
  239. }
  240. return "HeaderFooter-\(availableIndex)"
  241. }
  242. //MARK: - Compare
  243. class func compareIsChangedModel(_ model: KMHeaderFooterModel, withDict dict: NSDictionary) -> Bool {
  244. if let value = dict["fontName"] {
  245. if model.fontName != (value as! String) {
  246. return true
  247. }
  248. }
  249. if let value = dict["fontsize"] {
  250. if model.fontsize != (value as! CGFloat) {
  251. return true
  252. }
  253. }
  254. if let value = dict["color"] {
  255. if model.color.toHex() != (value as! String) {
  256. return true
  257. }
  258. }
  259. if let value = dict["leftMargin"] {
  260. if model.leftMargin != (value as! Int) {
  261. return true
  262. }
  263. }
  264. if let value = dict["rightMargin"] {
  265. if model.rightMargin != (value as! Int) {
  266. return true
  267. }
  268. }
  269. if let value = dict["bottomMargin"] {
  270. if model.bottomMargin != (value as! Int) {
  271. return true
  272. }
  273. }
  274. if let value = dict["topMargin"] {
  275. if model.topMargin != (value as! Int) {
  276. return true
  277. }
  278. }
  279. if let value = dict["topLeftString"] {
  280. if model.topLeftString != (value as! String) {
  281. return true
  282. }
  283. }
  284. if let value = dict["topCenterString"] {
  285. if model.topCenterString != (value as! String) {
  286. return true
  287. }
  288. }
  289. if let value = dict["topRightString"] {
  290. if model.topRightString != (value as! String) {
  291. return true
  292. }
  293. }
  294. if let value = dict["bottomLeftString"] {
  295. if model.bottomLeftString != (value as! String) {
  296. return true
  297. }
  298. }
  299. if let value = dict["bottomCenterString"] {
  300. if model.bottomCenterString != (value as! String) {
  301. return true
  302. }
  303. }
  304. if let value = dict["bottomRightString"] {
  305. if model.bottomRightString != (value as! String) {
  306. return true
  307. }
  308. }
  309. if let value = dict["dateFormatString"] {
  310. if model.dateFormatString != (value as! String) {
  311. return true
  312. }
  313. }
  314. if let value = dict["pageFormatString"] {
  315. if model.pageFormatString != (value as! String) {
  316. return true
  317. }
  318. }
  319. if let value = dict["startString"] {
  320. if model.startString != (value as! String) {
  321. return true
  322. }
  323. }
  324. return false
  325. }
  326. }
  327. //MARK: - Class Method
  328. extension KMHeaderFooterManager {
  329. class func getdateFormatArray() -> [String] {
  330. return [
  331. "m/d",
  332. "m/d/yy",
  333. "m/d/yyyy",
  334. "mm/dd/yy",
  335. "mm/dd/yyyy",
  336. "d/m/yy",
  337. "d/m/yyyy",
  338. "dd/mm/yy",
  339. "dd/mm/yyyy",
  340. "mm/yy",
  341. "mm/yyyy",
  342. "m.d.yy",
  343. "m.d.yyyy",
  344. "mm.dd.yy",
  345. "mm.dd.yyyy",
  346. "mm.yy",
  347. "mm.yyyy",
  348. "d.m.yy",
  349. "d.m.yyyy",
  350. "dd.mm.yy",
  351. "dd.mm.yyyy",
  352. "yy-mm-dd",
  353. "yyyy-mm-dd"
  354. ]
  355. }
  356. class func getPageFormats() -> [String] {
  357. return ["1",
  358. "1 of n",
  359. "1/n",
  360. "Page 1",
  361. "Page 1 of n"]
  362. }
  363. class func getFontSize() -> [String] {
  364. return ["6","8","10","12","14",
  365. "16","18","20","22","24",
  366. "26","28","30","32","34",
  367. "36","40","48","64","80",
  368. "96","112"]
  369. }
  370. }