KMPageEditTools.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // KMPageEditTools.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/1/11.
  6. //
  7. import Cocoa
  8. enum KMPageSizeUint: String {
  9. case mm = "mm"
  10. case cm = "cm"
  11. case km_in = "in"
  12. }
  13. class KMPageEditTools: NSObject {
  14. class func insert(_ document: CPDFDocument, _ index: Int, _ pages: Array<CPDFPage>) -> Bool {
  15. // for i in 0 ..< pages.count {
  16. // document.insertPageObject(pages[i], at: UInt(index+i))
  17. // }
  18. document.insertPageObject(pages.first, at: 0)
  19. return true
  20. }
  21. class func showInsertMenu(callback: (_ index: Int) -> ()) {
  22. }
  23. class func extract(_ document: CPDFDocument, _ pageIndexs: IndexSet,_ oneDocument: Bool,_ window: NSWindow, callback: @escaping (_ result: Bool, _ urls: Array<URL>?, _ error: String) -> ()) {
  24. /// 提取的页面
  25. var extractPages: Array<CPDFPage> = []
  26. for i in pageIndexs {
  27. extractPages.append(document.page(at: UInt(i)))
  28. }
  29. if (oneDocument) { /// 提取为一个文档
  30. let fileName = document.getFileNameAccordingSelctPages(extractPages)
  31. NSPanel.savePanel(window, true) { panel in
  32. panel.nameFieldStringValue = fileName
  33. } completion: { response, url, isOpen in
  34. if (response != .OK) {
  35. callback(false, nil, "cancel")
  36. return
  37. }
  38. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  39. let saveFilePath = url!.path
  40. DispatchQueue.global().async {
  41. var pdf = CPDFDocument.init()
  42. let success = (pdf!.extractAsOneDocument(withPages: extractPages, savePath: saveFilePath)) as Bool
  43. DispatchQueue.main.async {
  44. if (success == false) {
  45. callback(false, nil, "failure")
  46. return
  47. }
  48. if (isOpen == false) {
  49. let url = URL(fileURLWithPath: saveFilePath)
  50. NSWorkspace.shared.activateFileViewerSelecting([url])
  51. } else {
  52. if !saveFilePath.isPDFValid() {
  53. let alert = NSAlert()
  54. alert.alertStyle = .critical
  55. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  56. alert.runModal()
  57. return
  58. }
  59. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: saveFilePath), display: true) { document, result, error in
  60. }
  61. }
  62. callback(true, [URL(fileURLWithPath: saveFilePath)], "")
  63. }
  64. }
  65. }
  66. }
  67. return
  68. }
  69. NSPanel.savePanel(window, true) { panel in
  70. } completion: { result , url, isOpen in
  71. if result != .OK {
  72. callback(false, nil, "cancel")
  73. return
  74. }
  75. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  76. let outputURL = url
  77. DispatchQueue.global().async {
  78. let folderName = String((document.documentURL!.lastPathComponent.split(separator: ".")[0])) + "_extract"
  79. var filePath = URL(fileURLWithPath: outputURL!.path).appendingPathComponent(folderName).path
  80. var i = 1
  81. let testFilePath = filePath
  82. while FileManager.default.fileExists(atPath: filePath) {
  83. filePath = testFilePath + "\(i)"
  84. i += 1
  85. }
  86. try? FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false, attributes: nil)
  87. let successArray = document.extractPerPageDocument(withPages: extractPages, folerPath: filePath)
  88. DispatchQueue.main.async {
  89. if successArray!.count == 0 {
  90. callback(false, nil, "failure")
  91. return
  92. }
  93. if (!isOpen) {
  94. NSWorkspace.shared.activateFileViewerSelecting(successArray!)
  95. }
  96. callback(true, successArray, "")
  97. }
  98. }
  99. }
  100. }
  101. }
  102. class func split(_ document: CPDFDocument, _ avgNumberPage: Int, _ path: String, _ filename: String, callback: @escaping (_ result: Bool, _ outputDocuments: Array<CPDFDocument>?, _ error: String) -> ()) {
  103. if (avgNumberPage == 0) {
  104. callback(false, nil, "avgNumberPage = 0.")
  105. return
  106. }
  107. var fileCount: Int = (Int(document.pageCount) / avgNumberPage) + 1
  108. var files: Array<IndexSet> = []
  109. for i in 0 ..< fileCount {
  110. var indexs: IndexSet = []
  111. for j in 0 ..< avgNumberPage {
  112. if (i * avgNumberPage + j >= document.pageCount) {
  113. break
  114. }
  115. indexs.insert(i * avgNumberPage + j)
  116. }
  117. files.append(indexs)
  118. }
  119. var array: Array<CPDFDocument> = []
  120. for indexs in files {
  121. let document_new: CPDFDocument = CPDFDocument()
  122. document_new.importPages(indexs, from: document, at: 0)
  123. array.append(document_new)
  124. // document_new.write(to: <#T##URL!#>)
  125. }
  126. callback(true, array, "")
  127. }
  128. class func split(_ document: CPDFDocument, _ model: KMPageEditSplitSettingModel, _ path: String, _ filename: String, callback: @escaping (_ result: Bool, _ outputFilepaths: Array<String>?, _ error: String) -> ()) {
  129. let file_indexs = model.getSplitIndexSets
  130. if (file_indexs == nil || file_indexs!.count <= 0) {
  131. callback(false, nil, "")
  132. return
  133. }
  134. // var filePath = "\(path)/\(document.documentURL.deletingPathExtension().lastPathComponent)"
  135. // if (!FileManager.default.fileExists(atPath: filePath)) {
  136. // try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: true)
  137. // }
  138. let filePath = path
  139. var filepaths: [String] = []
  140. for i in 0 ..< file_indexs!.count {
  141. let indexs = file_indexs![i]
  142. let filepath = String(format: "%@/%@ %d.pdf", filePath, filename, i+1)
  143. filepaths.append(filepath)
  144. let newDocument = CPDFDocument()
  145. newDocument?.importPages(indexs, from: document, at: 0)
  146. newDocument?.write(to: URL(fileURLWithPath: filepath))
  147. }
  148. callback(true, filepaths, "")
  149. }
  150. class func reverse(_ document: CPDFDocument, _ selectedIndexs: IndexSet, callback: @escaping (_ result: Bool, _ error: String) -> ()) {
  151. if (document.pageCount <= 0) {
  152. callback(false, "")
  153. return
  154. }
  155. if (selectedIndexs.count <= 1) {
  156. callback(false, "")
  157. return
  158. }
  159. var pages: Array<CPDFPage> = []
  160. for i in 0 ..< document.pageCount {
  161. if (selectedIndexs.contains(IndexSet.Element(i))) {
  162. pages.append(document.page(at: i))
  163. }
  164. }
  165. var first: Int = 0
  166. var last: Int = pages.count-1
  167. while (last > first) {
  168. let beforePage: CPDFPage = pages[first]
  169. let afterPage: CPDFPage = pages[last]
  170. let beforeIndex = document.index(for: beforePage)
  171. let afterIndex = document.index(for: afterPage)
  172. document.removePage(at: UInt(afterIndex))
  173. document.removePage(at: UInt(beforeIndex))
  174. document.insertPageObject(afterPage, at: UInt(beforeIndex))
  175. document.insertPageObject(beforePage, at: UInt(afterIndex))
  176. first += 1
  177. last -= 1
  178. }
  179. callback(true, "")
  180. }
  181. class func getPageSize() -> Array<String> {
  182. return KMCropTools.getPageSize()
  183. }
  184. class func getPageSizeValue(_ pageSize: String) -> NSSize {
  185. return KMCropTools.getPageSizeValue(pageSize)
  186. }
  187. class func getAllPageSizeUnit() -> Array<String> {
  188. return [KMPageSizeUint.mm.rawValue, KMPageSizeUint.cm.rawValue,KMPageSizeUint.km_in.rawValue]
  189. }
  190. class func convertSize(with fromUnit: KMPageSizeUint, to toUnit: KMPageSizeUint, value: CGFloat) -> String {
  191. var result: CGFloat = value
  192. if (fromUnit == toUnit) {
  193. } else if (toUnit == .cm) {
  194. if (fromUnit == .mm) {
  195. result = value / 10.0
  196. } else if (fromUnit == .km_in) {
  197. result = value * 25.4
  198. }
  199. } else if (toUnit == .mm) {
  200. if (fromUnit == .cm) {
  201. result = value * 10
  202. } else if (fromUnit == .km_in) {
  203. result = value * 10 * 25.4
  204. }
  205. } else if (toUnit == .km_in) {
  206. if (fromUnit == .cm) {
  207. result = value / 25.4
  208. } else if (fromUnit == .mm) {
  209. result = value/10.0/25.4
  210. }
  211. }
  212. if (fmodf(Float(result), 1) == 0) { /// 如果有一位小数点
  213. return String(format: "%.0f", result)
  214. } else if (fmodf(Float(result*10), 1) == 0) { //如果有两位小数点
  215. return String(format: "%.1f", result)
  216. } else {
  217. return String(format: "%.2f", result)
  218. }
  219. }
  220. }