123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- //
- // CStampFileManger.swift
- // ComPDFKit_Tools
- //
- // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
- //
- // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- // This notice may not be removed from this file.
- //
- import UIKit
- let kPDFStampDataFolder = NSHomeDirectory().appending("/Library/PDFKitResources/Stamp")
- let kPDFStampTextList = NSHomeDirectory().appending("/Library/PDFKitResources/Stamp/stamp_text.plist")
- let kPDFStampImageList = NSHomeDirectory().appending("/Library/PDFKitResources/Stamp/stamp_image.plist")
- enum PDFStampCustomType: Int {
- case text
- case image
- }
- class CStampFileManager: NSObject {
- var stampTextList: [NSDictionary] = []
- var stampImageList: [NSDictionary] = []
- var deleteList: [NSDictionary] = []
-
- // MARK: - Init Method
-
- override init() {
- super.init()
- deleteList = []
- }
-
- func getDateTime() -> String {
- let timename = NSTimeZone.system
- let outputFormatter = DateFormatter()
- outputFormatter.timeZone = timename
- var tDate: String? = nil
-
- outputFormatter.dateFormat = "YYYYMMddHHmmss"
- tDate = outputFormatter.string(from: Date())
-
- return tDate ?? ""
-
- }
-
- // MARK: - File Manager
-
- func readStampDataFromFile() {
- readCustomStamp_TextStamp()
- readCustomStamp_ImageStamp()
- }
-
- func readCustomStamp_TextStamp() {
- let tManager = FileManager.default
- if !tManager.fileExists(atPath: kPDFStampTextList) {
- stampTextList = []
- } else {
- if !stampTextList.isEmpty {
- stampTextList = []
- }
- stampTextList = (NSMutableArray(contentsOfFile: kPDFStampTextList) as? [NSDictionary]) ?? []
- if stampTextList.isEmpty {
- stampTextList = []
- }
- }
-
- }
-
- func readCustomStamp_ImageStamp() {
- let tManager = FileManager.default
- if !tManager.fileExists(atPath: kPDFStampImageList) {
- stampImageList = []
- } else {
- if !stampImageList.isEmpty {
- stampImageList = []
- }
- stampImageList = NSMutableArray(contentsOfFile: kPDFStampImageList) as? [NSDictionary] ?? []
- if stampImageList.isEmpty {
- stampImageList = []
- }
- }
-
- }
-
-
- func getTextStampData() -> [Any] {
- return stampTextList
- }
-
- func getImageStampData() -> [Any] {
- return stampImageList
- }
-
- func saveStamp(with image: UIImage) -> String? {
- let tManager = FileManager.default
- guard let imageData = image.pngData() else {
- return nil
- }
- if imageData.isEmpty {
- return nil
- }
-
- let tName = getDateTime()
- let tPath = kPDFStampDataFolder.appending("/\(tName).png")
-
- if (imageData as NSData).write(toFile: tPath, atomically: false) {
- return tPath
- } else {
- var tPathDic = kPDFStampDataFolder
- var tIsDirectory = ObjCBool(false)
- while true {
- if tManager.fileExists(atPath: tPathDic, isDirectory: &tIsDirectory) {
- if tIsDirectory.boolValue {
- break
- }
- } else {
- try? tManager.createDirectory(atPath: tPathDic, withIntermediateDirectories: true, attributes: nil)
- }
- tPathDic = (tPathDic as NSString).deletingLastPathComponent
- }
-
- if (imageData as NSData).write(toFile: tPath, atomically: false) {
- return tPath
- }
- }
-
- return nil
-
- }
-
- func removeStampImage() {
- for tDict in deleteList {
- if let tPath = tDict["path"] as? String {
- let tFileManager = FileManager.default
- try? tFileManager.removeItem(atPath: tPath)
- }
- }
- }
-
-
- func saveStampDataToFile(stampType: PDFStampCustomType) -> Bool {
- let tManager = FileManager.default
- switch stampType {
- case .text:
- let array = NSMutableArray(array: stampTextList)
- if array.write(toFile: kPDFStampTextList, atomically: false) {
- return true
- } else {
- var tPathDic = kPDFStampTextList
- var tIsDirectory = ObjCBool(false)
- while true {
- tPathDic = (tPathDic as NSString).deletingLastPathComponent
- if tManager.fileExists(atPath: tPathDic, isDirectory: &tIsDirectory) {
- if tIsDirectory.boolValue {
- break
- }
- } else {
- try? tManager.createDirectory(atPath: tPathDic, withIntermediateDirectories: true, attributes: nil)
- }
-
- }
- let array = NSMutableArray(array: stampTextList)
- if array.write(toFile: kPDFStampTextList, atomically: false) {
- return true
- }
- }
- return false
-
- case .image:
- let array = NSMutableArray(array: stampImageList)
- if array.write(toFile: kPDFStampImageList, atomically: false) {
- return true
- } else {
- var tPathDic = kPDFStampImageList
- var tIsDirectory = ObjCBool(false)
- while true {
- tPathDic = (tPathDic as NSString).deletingLastPathComponent
- if tManager.fileExists(atPath: tPathDic, isDirectory: &tIsDirectory) {
- if tIsDirectory.boolValue {
- break
- }
- } else {
- try? tManager.createDirectory(atPath: tPathDic, withIntermediateDirectories: true, attributes: nil)
- }
- }
- let array = NSMutableArray(array: stampImageList)
- if array.write(toFile: kPDFStampImageList, atomically: false) {
- return true
- }
- }
- return false
-
- }
-
- }
-
- func insertStampItem(_ stampItem: NSDictionary, type stampType: PDFStampCustomType) -> Bool {
- switch stampType {
- case .text:
- if stampTextList.isEmpty {
- readCustomStamp_TextStamp()
- }
- if stampItem is [AnyHashable: Any] {
- let array = NSMutableArray(array: stampTextList)
- array.insert(stampItem, at: 0)
- stampTextList = array as? [NSDictionary] ?? []
- if saveStampDataToFile(stampType: .text) {
- return true
- } else {
- return false
- }
- } else {
- return false
- }
-
- case .image:
- if stampImageList.isEmpty {
- readCustomStamp_ImageStamp()
- }
-
- if let item = stampItem as? [AnyHashable: Any] {
- let array = NSMutableArray(array: stampImageList)
- array.insert(item, at: 0)
- stampImageList = array as? [NSDictionary] ?? []
- if saveStampDataToFile(stampType: .image) {
- return true
- } else {
- return false
- }
- } else {
- return false
- }
-
- }
-
- }
-
-
- func removeStampItem(index: Int, stampType: PDFStampCustomType) -> Bool {
- switch stampType {
- case .text:
- if stampTextList.isEmpty {
- readCustomStamp_TextStamp()
- }
- if index >= 0 && index <= stampTextList.count {
- stampTextList.remove(at: index)
- if saveStampDataToFile(stampType: .text) {
- return true
- } else {
- return false
- }
- } else {
- return false
- }
-
- case .image:
- if stampImageList.isEmpty {
- readCustomStamp_ImageStamp()
- }
- if index >= 0 && index < stampImageList.count {
- if deleteList.isEmpty {
- deleteList = []
- }
- if let tDict = stampImageList[index] as? [AnyHashable: Any] {
- let array = NSMutableArray(array: deleteList)
- array.add(tDict)
- deleteList = array as? [NSDictionary] ?? []
- }
-
- stampImageList.remove(at: index)
- if saveStampDataToFile(stampType: .image) {
- return true
- } else {
- return false
- }
- } else {
- return false
- }
-
- }
-
- }
-
- }
|