CStampFileManger.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // CStampFileManger.swift
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. let kPDFStampDataFolder = NSHomeDirectory().appending("/Library/PDFKitResources/Stamp")
  14. let kPDFStampTextList = NSHomeDirectory().appending("/Library/PDFKitResources/Stamp/stamp_text.plist")
  15. let kPDFStampImageList = NSHomeDirectory().appending("/Library/PDFKitResources/Stamp/stamp_image.plist")
  16. enum PDFStampCustomType: Int {
  17. case text
  18. case image
  19. }
  20. class CStampFileManager: NSObject {
  21. var stampTextList: [NSDictionary] = []
  22. var stampImageList: [NSDictionary] = []
  23. var deleteList: [NSDictionary] = []
  24. // MARK: - Init Method
  25. override init() {
  26. super.init()
  27. deleteList = []
  28. }
  29. func getDateTime() -> String {
  30. let timename = NSTimeZone.system
  31. let outputFormatter = DateFormatter()
  32. outputFormatter.timeZone = timename
  33. var tDate: String? = nil
  34. outputFormatter.dateFormat = "YYYYMMddHHmmss"
  35. tDate = outputFormatter.string(from: Date())
  36. return tDate ?? ""
  37. }
  38. // MARK: - File Manager
  39. func readStampDataFromFile() {
  40. readCustomStamp_TextStamp()
  41. readCustomStamp_ImageStamp()
  42. }
  43. func readCustomStamp_TextStamp() {
  44. let tManager = FileManager.default
  45. if !tManager.fileExists(atPath: kPDFStampTextList) {
  46. stampTextList = []
  47. } else {
  48. if !stampTextList.isEmpty {
  49. stampTextList = []
  50. }
  51. stampTextList = (NSMutableArray(contentsOfFile: kPDFStampTextList) as? [NSDictionary]) ?? []
  52. if stampTextList.isEmpty {
  53. stampTextList = []
  54. }
  55. }
  56. }
  57. func readCustomStamp_ImageStamp() {
  58. let tManager = FileManager.default
  59. if !tManager.fileExists(atPath: kPDFStampImageList) {
  60. stampImageList = []
  61. } else {
  62. if !stampImageList.isEmpty {
  63. stampImageList = []
  64. }
  65. stampImageList = NSMutableArray(contentsOfFile: kPDFStampImageList) as? [NSDictionary] ?? []
  66. if stampImageList.isEmpty {
  67. stampImageList = []
  68. }
  69. }
  70. }
  71. func getTextStampData() -> [Any] {
  72. return stampTextList
  73. }
  74. func getImageStampData() -> [Any] {
  75. return stampImageList
  76. }
  77. func saveStamp(with image: UIImage) -> String? {
  78. let tManager = FileManager.default
  79. guard let imageData = image.pngData() else {
  80. return nil
  81. }
  82. if imageData.isEmpty {
  83. return nil
  84. }
  85. let tName = getDateTime()
  86. let tPath = kPDFStampDataFolder.appending("/\(tName).png")
  87. if (imageData as NSData).write(toFile: tPath, atomically: false) {
  88. return tPath
  89. } else {
  90. var tPathDic = kPDFStampDataFolder
  91. var tIsDirectory = ObjCBool(false)
  92. while true {
  93. if tManager.fileExists(atPath: tPathDic, isDirectory: &tIsDirectory) {
  94. if tIsDirectory.boolValue {
  95. break
  96. }
  97. } else {
  98. try? tManager.createDirectory(atPath: tPathDic, withIntermediateDirectories: true, attributes: nil)
  99. }
  100. tPathDic = (tPathDic as NSString).deletingLastPathComponent
  101. }
  102. if (imageData as NSData).write(toFile: tPath, atomically: false) {
  103. return tPath
  104. }
  105. }
  106. return nil
  107. }
  108. func removeStampImage() {
  109. for tDict in deleteList {
  110. if let tPath = tDict["path"] as? String {
  111. let tFileManager = FileManager.default
  112. try? tFileManager.removeItem(atPath: tPath)
  113. }
  114. }
  115. }
  116. func saveStampDataToFile(stampType: PDFStampCustomType) -> Bool {
  117. let tManager = FileManager.default
  118. switch stampType {
  119. case .text:
  120. let array = NSMutableArray(array: stampTextList)
  121. if array.write(toFile: kPDFStampTextList, atomically: false) {
  122. return true
  123. } else {
  124. var tPathDic = kPDFStampTextList
  125. var tIsDirectory = ObjCBool(false)
  126. while true {
  127. tPathDic = (tPathDic as NSString).deletingLastPathComponent
  128. if tManager.fileExists(atPath: tPathDic, isDirectory: &tIsDirectory) {
  129. if tIsDirectory.boolValue {
  130. break
  131. }
  132. } else {
  133. try? tManager.createDirectory(atPath: tPathDic, withIntermediateDirectories: true, attributes: nil)
  134. }
  135. }
  136. let array = NSMutableArray(array: stampTextList)
  137. if array.write(toFile: kPDFStampTextList, atomically: false) {
  138. return true
  139. }
  140. }
  141. return false
  142. case .image:
  143. let array = NSMutableArray(array: stampImageList)
  144. if array.write(toFile: kPDFStampImageList, atomically: false) {
  145. return true
  146. } else {
  147. var tPathDic = kPDFStampImageList
  148. var tIsDirectory = ObjCBool(false)
  149. while true {
  150. tPathDic = (tPathDic as NSString).deletingLastPathComponent
  151. if tManager.fileExists(atPath: tPathDic, isDirectory: &tIsDirectory) {
  152. if tIsDirectory.boolValue {
  153. break
  154. }
  155. } else {
  156. try? tManager.createDirectory(atPath: tPathDic, withIntermediateDirectories: true, attributes: nil)
  157. }
  158. }
  159. let array = NSMutableArray(array: stampImageList)
  160. if array.write(toFile: kPDFStampImageList, atomically: false) {
  161. return true
  162. }
  163. }
  164. return false
  165. }
  166. }
  167. func insertStampItem(_ stampItem: NSDictionary, type stampType: PDFStampCustomType) -> Bool {
  168. switch stampType {
  169. case .text:
  170. if stampTextList.isEmpty {
  171. readCustomStamp_TextStamp()
  172. }
  173. if stampItem is [AnyHashable: Any] {
  174. let array = NSMutableArray(array: stampTextList)
  175. array.insert(stampItem, at: 0)
  176. stampTextList = array as? [NSDictionary] ?? []
  177. if saveStampDataToFile(stampType: .text) {
  178. return true
  179. } else {
  180. return false
  181. }
  182. } else {
  183. return false
  184. }
  185. case .image:
  186. if stampImageList.isEmpty {
  187. readCustomStamp_ImageStamp()
  188. }
  189. if let item = stampItem as? [AnyHashable: Any] {
  190. let array = NSMutableArray(array: stampImageList)
  191. array.insert(item, at: 0)
  192. stampImageList = array as? [NSDictionary] ?? []
  193. if saveStampDataToFile(stampType: .image) {
  194. return true
  195. } else {
  196. return false
  197. }
  198. } else {
  199. return false
  200. }
  201. }
  202. }
  203. func removeStampItem(index: Int, stampType: PDFStampCustomType) -> Bool {
  204. switch stampType {
  205. case .text:
  206. if stampTextList.isEmpty {
  207. readCustomStamp_TextStamp()
  208. }
  209. if index >= 0 && index <= stampTextList.count {
  210. stampTextList.remove(at: index)
  211. if saveStampDataToFile(stampType: .text) {
  212. return true
  213. } else {
  214. return false
  215. }
  216. } else {
  217. return false
  218. }
  219. case .image:
  220. if stampImageList.isEmpty {
  221. readCustomStamp_ImageStamp()
  222. }
  223. if index >= 0 && index < stampImageList.count {
  224. if deleteList.isEmpty {
  225. deleteList = []
  226. }
  227. if let tDict = stampImageList[index] as? [AnyHashable: Any] {
  228. let array = NSMutableArray(array: deleteList)
  229. array.add(tDict)
  230. deleteList = array as? [NSDictionary] ?? []
  231. }
  232. stampImageList.remove(at: index)
  233. if saveStampDataToFile(stampType: .image) {
  234. return true
  235. } else {
  236. return false
  237. }
  238. } else {
  239. return false
  240. }
  241. }
  242. }
  243. }