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. let panel = NSOpenPanel()
  70. panel.canChooseFiles = false
  71. panel.canChooseDirectories = true
  72. panel.canCreateDirectories = true
  73. panel.allowsMultipleSelection = false
  74. let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
  75. button.state = .off
  76. panel.accessoryView = button
  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. if (button.state == .off) {
  101. NSWorkspace.shared.activateFileViewerSelecting(successArray!)
  102. }
  103. callback(true, successArray, "")
  104. }
  105. }
  106. }
  107. }
  108. }
  109. class func split(_ document: CPDFDocument, _ avgNumberPage: Int, _ path: String, _ filename: String, callback: @escaping (_ result: Bool, _ outputDocuments: Array<CPDFDocument>?, _ error: String) -> ()) {
  110. if (avgNumberPage == 0) {
  111. callback(false, nil, "avgNumberPage = 0.")
  112. return
  113. }
  114. var fileCount: Int = (Int(document.pageCount) / avgNumberPage) + 1
  115. var files: Array<IndexSet> = []
  116. for i in 0 ..< fileCount {
  117. var indexs: IndexSet = []
  118. for j in 0 ..< avgNumberPage {
  119. if (i * avgNumberPage + j >= document.pageCount) {
  120. break
  121. }
  122. indexs.insert(i * avgNumberPage + j)
  123. }
  124. files.append(indexs)
  125. }
  126. var array: Array<CPDFDocument> = []
  127. for indexs in files {
  128. let document_new: CPDFDocument = CPDFDocument()
  129. document_new.importPages(indexs, from: document, at: 0)
  130. array.append(document_new)
  131. // document_new.write(to: <#T##URL!#>)
  132. }
  133. callback(true, array, "")
  134. }
  135. class func split(_ document: CPDFDocument, _ model: KMPageEditSplitSettingModel, _ path: String, _ filename: String, callback: @escaping (_ result: Bool, _ outputFilepaths: Array<String>?, _ error: String) -> ()) {
  136. let file_indexs = model.getSplitIndexSets
  137. if (file_indexs == nil || file_indexs!.count <= 0) {
  138. callback(false, nil, "")
  139. return
  140. }
  141. var filepaths: [String] = []
  142. for i in 0 ..< file_indexs!.count {
  143. let indexs = file_indexs![i]
  144. let filepath = String(format: "%@/%@ %d.pdf", path, filename, i+1)
  145. filepaths.append(filepath)
  146. let newDocument = CPDFDocument()
  147. newDocument?.importPages(indexs, from: document, at: 0)
  148. newDocument?.write(to: URL(fileURLWithPath: filepath))
  149. }
  150. callback(true, filepaths, "")
  151. }
  152. class func reverse(_ document: CPDFDocument, _ selectedIndexs: IndexSet, callback: @escaping (_ result: Bool, _ error: String) -> ()) {
  153. if (document.pageCount <= 0) {
  154. callback(false, "")
  155. return
  156. }
  157. if (selectedIndexs.count <= 1) {
  158. callback(false, "")
  159. return
  160. }
  161. var pages: Array<CPDFPage> = []
  162. for i in 0 ..< document.pageCount-1 {
  163. if (selectedIndexs.contains(IndexSet.Element(i))) {
  164. pages.append(document.page(at: i))
  165. }
  166. }
  167. var first: Int = 0
  168. var last: Int = pages.count-1
  169. while (last > first) {
  170. let beforePage: CPDFPage = pages[first]
  171. let afterPage: CPDFPage = pages[last]
  172. let beforeIndex = document.index(for: beforePage)
  173. let afterIndex = document.index(for: afterPage)
  174. document.removePage(at: UInt(beforeIndex))
  175. document.removePage(at: UInt(afterIndex))
  176. document.insertPageObject(afterPage, at: UInt(beforeIndex))
  177. document.insertPageObject(beforePage, at: UInt(afterIndex))
  178. first += 1
  179. last -= 1
  180. }
  181. callback(true, "")
  182. }
  183. class func getPageSize() -> Array<String> {
  184. return KMCropTools.getPageSize()
  185. }
  186. class func getPageSizeValue(_ pageSize: String) -> NSSize {
  187. return KMCropTools.getPageSizeValue(pageSize)
  188. }
  189. class func getAllPageSizeUnit() -> Array<String> {
  190. return [KMPageSizeUint.mm.rawValue, KMPageSizeUint.cm.rawValue,KMPageSizeUint.km_in.rawValue]
  191. }
  192. class func convertSize(with fromUnit: KMPageSizeUint, to toUnit: KMPageSizeUint, value: CGFloat) -> String {
  193. var result: CGFloat = value
  194. if (fromUnit == toUnit) {
  195. } else if (toUnit == .cm) {
  196. if (fromUnit == .mm) {
  197. result = value / 10.0
  198. } else if (fromUnit == .km_in) {
  199. result = value * 25.4
  200. }
  201. } else if (toUnit == .mm) {
  202. if (fromUnit == .cm) {
  203. result = value * 10
  204. } else if (fromUnit == .km_in) {
  205. result = value * 10 * 25.4
  206. }
  207. } else if (toUnit == .km_in) {
  208. if (fromUnit == .cm) {
  209. result = value / 25.4
  210. } else if (fromUnit == .mm) {
  211. result = value/10.0/25.4
  212. }
  213. }
  214. if (fmodf(Float(result), 1) == 0) { /// 如果有一位小数点
  215. return String(format: "%.0f", result)
  216. } else if (fmodf(Float(result*10), 1) == 0) { //如果有两位小数点
  217. return String(format: "%.1f", result)
  218. } else {
  219. return String(format: "%.2f", result)
  220. }
  221. }
  222. }