KMPageEditTools.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // KMPageEditTools.swift
  3. // PDF Reader Pro
  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) ?? false
  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. NSDocumentController.shared.km_safe_openDocument(withContentsOf: URL(fileURLWithPath: saveFilePath), display: true) { _, _, _ in
  56. }
  57. }
  58. callback(true, [URL(fileURLWithPath: saveFilePath)], "")
  59. }
  60. }
  61. }
  62. }
  63. return
  64. }
  65. let panel = NSOpenPanel()
  66. panel.canChooseFiles = false
  67. panel.canChooseDirectories = true
  68. panel.canCreateDirectories = true
  69. panel.allowsMultipleSelection = false
  70. panel.beginSheetModal(for: window) { response in
  71. if response != .OK {
  72. callback(false, nil, "cancel")
  73. return
  74. }
  75. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  76. let outputURL = panel.url
  77. DispatchQueue.global().async {
  78. let folderName = String(((document.documentURL?.lastPathComponent.split(separator: ".").first) ?? "")) + "_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. NSWorkspace.shared.activateFileViewerSelecting(successArray)
  94. callback(true, successArray, "")
  95. }
  96. }
  97. }
  98. }
  99. }
  100. class func split(_ document: CPDFDocument, _ avgNumberPage: Int, _ path: String, _ filename: String, callback: @escaping (_ result: Bool, _ outputDocuments: Array<CPDFDocument>?, _ error: String) -> ()) {
  101. if (avgNumberPage == 0) {
  102. callback(false, nil, "avgNumberPage = 0.")
  103. return
  104. }
  105. var fileCount: Int = (Int(document.pageCount) / avgNumberPage) + 1
  106. var files: Array<IndexSet> = []
  107. for i in 0 ..< fileCount {
  108. var indexs: IndexSet = []
  109. for j in 0 ..< avgNumberPage {
  110. if (i * avgNumberPage + j >= document.pageCount) {
  111. break
  112. }
  113. indexs.insert(i * avgNumberPage + j)
  114. }
  115. files.append(indexs)
  116. }
  117. var array: Array<CPDFDocument> = []
  118. for indexs in files {
  119. let document_new: CPDFDocument = CPDFDocument()
  120. document_new.importPages(indexs, from: document, at: 0)
  121. array.append(document_new)
  122. // document_new.write(to: <#T##URL!#>)
  123. }
  124. callback(true, array, "")
  125. }
  126. class func split(_ document: CPDFDocument, _ model: KMPageEditSplitSettingModel, _ path: String, _ filename: String, callback: @escaping (_ result: Bool, _ outputFilepaths: Array<String>?, _ error: String) -> ()) {
  127. let file_indexs = model.getSplitIndexSets ?? []
  128. if (file_indexs.count <= 0) {
  129. callback(false, nil, "")
  130. return
  131. }
  132. let filePath = "\(path)/\(document.documentURL.deletingPathExtension().lastPathComponent)"
  133. if (!FileManager.default.fileExists(atPath: filePath)) {
  134. try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: true)
  135. }
  136. var filepaths: [String] = []
  137. for i in 0 ..< file_indexs.count {
  138. let indexs = file_indexs[i]
  139. let filepath = String(format: "%@/%@ %d.pdf", filePath, filename, i+1)
  140. filepaths.append(filepath)
  141. let newDocument = CPDFDocument()
  142. newDocument?.importPages(indexs, from: document, at: 0)
  143. newDocument?.write(to: URL(fileURLWithPath: filepath))
  144. }
  145. callback(true, filepaths, "")
  146. }
  147. class func reverse(_ document: CPDFDocument, _ selectedIndexs: IndexSet, callback: @escaping (_ result: Bool, _ error: String) -> ()) {
  148. if (document.pageCount <= 0) {
  149. callback(false, "")
  150. return
  151. }
  152. if (selectedIndexs.count <= 1) {
  153. callback(false, "")
  154. return
  155. }
  156. var pages: Array<CPDFPage> = []
  157. for i in 0 ..< document.pageCount {
  158. if (selectedIndexs.contains(IndexSet.Element(i))) {
  159. pages.append(document.page(at: i))
  160. }
  161. }
  162. var first: Int = 0
  163. var last: Int = pages.count-1
  164. while (last > first) {
  165. let beforePage: CPDFPage = pages[first]
  166. let afterPage: CPDFPage = pages[last]
  167. let beforeIndex = document.index(for: beforePage)
  168. let afterIndex = document.index(for: afterPage)
  169. document.removePage(at: UInt(afterIndex))
  170. document.removePage(at: UInt(beforeIndex))
  171. document.insertPageObject(afterPage, at: UInt(beforeIndex))
  172. document.insertPageObject(beforePage, at: UInt(afterIndex))
  173. first += 1
  174. last -= 1
  175. }
  176. callback(true, "")
  177. }
  178. class func getPageSize() -> Array<String> {
  179. return KMCropTools.getPageSize()
  180. }
  181. class func getPageSizeValue(_ pageSize: String) -> NSSize {
  182. return KMCropTools.getPageSizeValue(pageSize)
  183. }
  184. class func getAllPageSizeUnit() -> Array<String> {
  185. return [KMPageSizeUint.mm.rawValue, KMPageSizeUint.cm.rawValue,KMPageSizeUint.km_in.rawValue]
  186. }
  187. class func convertSize(with fromUnit: KMPageSizeUint, to toUnit: KMPageSizeUint, value: CGFloat) -> String {
  188. var result: CGFloat = value
  189. if (fromUnit == toUnit) {
  190. } else if (toUnit == .cm) {
  191. if (fromUnit == .mm) {
  192. result = value / 10.0
  193. } else if (fromUnit == .km_in) {
  194. result = value * 25.4
  195. }
  196. } else if (toUnit == .mm) {
  197. if (fromUnit == .cm) {
  198. result = value * 10
  199. } else if (fromUnit == .km_in) {
  200. result = value * 10 * 25.4
  201. }
  202. } else if (toUnit == .km_in) {
  203. if (fromUnit == .cm) {
  204. result = value / 25.4
  205. } else if (fromUnit == .mm) {
  206. result = value/10.0/25.4
  207. }
  208. }
  209. if (fmodf(Float(result), 1) == 0) { /// 如果有一位小数点
  210. return String(format: "%.0f", result)
  211. } else if (fmodf(Float(result*10), 1) == 0) { //如果有两位小数点
  212. return String(format: "%.1f", result)
  213. } else {
  214. return String(format: "%.2f", result)
  215. }
  216. }
  217. class func sizeUnitToBoundSize(unit: KMPageSizeUint, pageSize: NSSize) -> NSSize {
  218. let fromUnit = unit
  219. let toUnit: KMPageSizeUint = .mm
  220. var result: NSSize = pageSize
  221. if (fromUnit == .cm) {
  222. result = NSSize(width: pageSize.width * 10, height: pageSize.height * 10)
  223. } else if (fromUnit == .km_in) {
  224. result = NSSize(width: pageSize.width * 10 * 25.4,height: pageSize.height * 10 * 25.4)
  225. }
  226. let width = result.width
  227. let height = result.height
  228. return NSSize(width: width * 595 / 210, height: height * 842 / 297)
  229. }
  230. class func boundSizeToUnitSize(size boundSize: NSSize, to toUnit: KMPageSizeUint) -> NSSize {
  231. let _fromUnit = toUnit
  232. let _toUnit: KMPageSizeUint = .mm
  233. var result: NSSize = boundSize
  234. if (_fromUnit == .cm) {
  235. result = NSSize(width: boundSize.width * 10, height: boundSize.height * 10)
  236. } else if (_fromUnit == .km_in) {
  237. result = NSSize(width: boundSize.width * 10 * 25.4,height: boundSize.height * 10 * 25.4)
  238. }
  239. let width = result.width
  240. let height = result.height
  241. return NSSize(width: width * 210 / 595, height: height * 297 / 842)
  242. }
  243. }