KMPageEditTools.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. var fileName = document.documentURL.deletingPathExtension().lastPathComponent
  31. fileName.append(" pages ")
  32. fileName.append(KMPageRangeTools.newParseSelectedIndexs(selectedIndex: pageIndexs.sorted()))
  33. fileName.append(".pdf")
  34. NSPanel.savePanel(window, true) { panel in
  35. panel.nameFieldStringValue = fileName
  36. } completion: { response, url, isOpen in
  37. if (response != .OK) {
  38. callback(false, nil, "cancel")
  39. return
  40. }
  41. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  42. let saveFilePath = url!.path
  43. DispatchQueue.global().async {
  44. var pdf = CPDFDocument.init()
  45. let success = (pdf!.extractAsOneDocument(withPages: extractPages, savePath: saveFilePath)) as Bool
  46. DispatchQueue.main.async {
  47. if (success == false) {
  48. callback(false, nil, "failure")
  49. return
  50. }
  51. if (isOpen == false) {
  52. let url = URL(fileURLWithPath: saveFilePath)
  53. NSWorkspace.shared.activateFileViewerSelecting([url])
  54. } else {
  55. if !saveFilePath.isPDFValid() {
  56. let alert = NSAlert()
  57. alert.alertStyle = .critical
  58. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  59. alert.runModal()
  60. return
  61. }
  62. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: saveFilePath), display: true) { document, result, error in
  63. }
  64. }
  65. callback(true, [URL(fileURLWithPath: saveFilePath)], "")
  66. }
  67. }
  68. }
  69. }
  70. return
  71. }
  72. let panel = NSOpenPanel()
  73. panel.canChooseFiles = false
  74. panel.canChooseDirectories = true
  75. panel.canCreateDirectories = true
  76. panel.allowsMultipleSelection = false
  77. panel.beginSheetModal(for: window) { response in
  78. if response != .OK {
  79. callback(false, nil, "cancel")
  80. return
  81. }
  82. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  83. let outputURL = panel.url
  84. DispatchQueue.global().async {
  85. let folderName = String((document.documentURL!.lastPathComponent.split(separator: ".")[0])) + "_extract"
  86. var filePath = URL(fileURLWithPath: outputURL!.path).appendingPathComponent(folderName).path
  87. var i = 1
  88. let testFilePath = filePath
  89. while FileManager.default.fileExists(atPath: filePath) {
  90. filePath = testFilePath + "\(i)"
  91. i += 1
  92. }
  93. try? FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false, attributes: nil)
  94. let successArray = document.extractPerPageDocument(withPages: extractPages, folerPath: filePath)
  95. DispatchQueue.main.async {
  96. if successArray!.count == 0 {
  97. callback(false, nil, "failure")
  98. return
  99. }
  100. NSWorkspace.shared.activateFileViewerSelecting(successArray!)
  101. callback(true, successArray, "")
  102. }
  103. }
  104. }
  105. }
  106. }
  107. class func split(_ document: CPDFDocument, _ avgNumberPage: Int, _ path: String, _ filename: String, callback: @escaping (_ result: Bool, _ outputDocuments: Array<CPDFDocument>?, _ error: String) -> ()) {
  108. if (avgNumberPage == 0) {
  109. callback(false, nil, "avgNumberPage = 0.")
  110. return
  111. }
  112. var fileCount: Int = (Int(document.pageCount) / avgNumberPage) + 1
  113. var files: Array<IndexSet> = []
  114. for i in 0 ..< fileCount {
  115. var indexs: IndexSet = []
  116. for j in 0 ..< avgNumberPage {
  117. if (i * avgNumberPage + j >= document.pageCount) {
  118. break
  119. }
  120. indexs.insert(i * avgNumberPage + j)
  121. }
  122. files.append(indexs)
  123. }
  124. var array: Array<CPDFDocument> = []
  125. for indexs in files {
  126. let document_new: CPDFDocument = CPDFDocument()
  127. document_new.importPages(indexs, from: document, at: 0)
  128. array.append(document_new)
  129. // document_new.write(to: <#T##URL!#>)
  130. }
  131. callback(true, array, "")
  132. }
  133. class func split(_ document: CPDFDocument, _ model: KMPageEditSplitSettingModel, _ path: String, _ filename: String, callback: @escaping (_ result: Bool, _ outputFilepaths: Array<String>?, _ error: String) -> ()) {
  134. let file_indexs = model.getSplitIndexSets
  135. if (file_indexs == nil || file_indexs!.count <= 0) {
  136. callback(false, nil, "")
  137. return
  138. }
  139. let filePath = "\(path)/\(document.documentURL.deletingPathExtension().lastPathComponent)"
  140. if (!FileManager.default.fileExists(atPath: filePath)) {
  141. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: true)
  142. }
  143. var filepaths: [String] = []
  144. for i in 0 ..< file_indexs!.count {
  145. let indexs = file_indexs![i]
  146. let filepath = String(format: "%@/%@ %d.pdf", filePath, filename, i+1)
  147. filepaths.append(filepath)
  148. let newDocument = CPDFDocument()
  149. newDocument?.importPages(indexs, from: document, at: 0)
  150. newDocument?.write(to: URL(fileURLWithPath: filepath))
  151. }
  152. callback(true, filepaths, "")
  153. }
  154. class func reverse(_ document: CPDFDocument, _ selectedIndexs: IndexSet, callback: @escaping (_ result: Bool, _ error: String) -> ()) {
  155. if (document.pageCount <= 0) {
  156. callback(false, "")
  157. return
  158. }
  159. if (selectedIndexs.count <= 1) {
  160. callback(false, "")
  161. return
  162. }
  163. var pages: Array<CPDFPage> = []
  164. for i in 0 ..< document.pageCount {
  165. if (selectedIndexs.contains(IndexSet.Element(i))) {
  166. pages.append(document.page(at: i))
  167. }
  168. }
  169. var first: Int = 0
  170. var last: Int = pages.count-1
  171. while (last > first) {
  172. let beforePage: CPDFPage = pages[first]
  173. let afterPage: CPDFPage = pages[last]
  174. let beforeIndex = document.index(for: beforePage)
  175. let afterIndex = document.index(for: afterPage)
  176. document.removePage(at: UInt(afterIndex))
  177. document.removePage(at: UInt(beforeIndex))
  178. document.insertPageObject(afterPage, at: UInt(beforeIndex))
  179. document.insertPageObject(beforePage, at: UInt(afterIndex))
  180. first += 1
  181. last -= 1
  182. }
  183. callback(true, "")
  184. }
  185. class func getPageSize() -> Array<String> {
  186. return KMCropTools.getPageSize()
  187. }
  188. class func getPageSizeValue(_ pageSize: String) -> NSSize {
  189. return KMCropTools.getPageSizeValue(pageSize)
  190. }
  191. class func getAllPageSizeUnit() -> Array<String> {
  192. return [KMPageSizeUint.mm.rawValue, KMPageSizeUint.cm.rawValue,KMPageSizeUint.km_in.rawValue]
  193. }
  194. class func convertSize(with fromUnit: KMPageSizeUint, to toUnit: KMPageSizeUint, value: CGFloat) -> String {
  195. var result: CGFloat = value
  196. if (fromUnit == toUnit) {
  197. } else if (toUnit == .cm) {
  198. if (fromUnit == .mm) {
  199. result = value / 10.0
  200. } else if (fromUnit == .km_in) {
  201. result = value * 25.4
  202. }
  203. } else if (toUnit == .mm) {
  204. if (fromUnit == .cm) {
  205. result = value * 10
  206. } else if (fromUnit == .km_in) {
  207. result = value * 10 * 25.4
  208. }
  209. } else if (toUnit == .km_in) {
  210. if (fromUnit == .cm) {
  211. result = value / 25.4
  212. } else if (fromUnit == .mm) {
  213. result = value/10.0/25.4
  214. }
  215. }
  216. if (fmodf(Float(result), 1) == 0) { /// 如果有一位小数点
  217. return String(format: "%.0f", result)
  218. } else if (fmodf(Float(result*10), 1) == 0) { //如果有两位小数点
  219. return String(format: "%.1f", result)
  220. } else {
  221. return String(format: "%.2f", result)
  222. }
  223. }
  224. class func sizeUnitToBoundSize(unit: KMPageSizeUint, pageSize: NSSize) -> NSSize {
  225. let fromUnit = unit
  226. let toUnit: KMPageSizeUint = .mm
  227. var result: NSSize = pageSize
  228. if (fromUnit == .cm) {
  229. result = NSSize(width: pageSize.width * 10, height: pageSize.height * 10)
  230. } else if (fromUnit == .km_in) {
  231. result = NSSize(width: pageSize.width * 10 * 25.4,height: pageSize.height * 10 * 25.4)
  232. }
  233. let width = result.width
  234. let height = result.height
  235. return NSSize(width: width * 595 / 210, height: height * 842 / 297)
  236. }
  237. class func boundSizeToUnitSize(size boundSize: NSSize, to toUnit: KMPageSizeUint) -> NSSize {
  238. let _fromUnit = toUnit
  239. let _toUnit: KMPageSizeUint = .mm
  240. var result: NSSize = boundSize
  241. if (_fromUnit == .cm) {
  242. result = NSSize(width: boundSize.width * 10, height: boundSize.height * 10)
  243. } else if (_fromUnit == .km_in) {
  244. result = NSSize(width: boundSize.width * 10 * 25.4,height: boundSize.height * 10 * 25.4)
  245. }
  246. let width = result.width
  247. let height = result.height
  248. return NSSize(width: width * 210 / 595, height: height * 297 / 842)
  249. }
  250. }