KMPageEditTools.swift 11 KB

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