KMNThumbnailBaseViewController+Action.swift 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. //
  2. // KMMainViewController+Action.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/12/15.
  6. //
  7. import Foundation
  8. import KMComponentLibrary
  9. extension KMNThumbnailBaseViewController {
  10. private func insertImageFilePath(imagePath:String,pageDex:Int)->Bool{
  11. var isSuccessFul:Bool = false
  12. if (FileManager.default.fileExists(atPath: imagePath)) {
  13. if let image = NSImage(contentsOfFile: imagePath) {
  14. isSuccessFul = showDocument?.km_insertPage(image.size, withImage: imagePath, at: UInt(pageDex)) == true
  15. }
  16. }
  17. return isSuccessFul
  18. }
  19. private func undoDeleteIndexPaths(deleteIndexPath: Set<IndexPath>) {
  20. var changeIndex:IndexSet = []
  21. var deletePages:[CPDFPage] = []
  22. let pageIndexs = KMNTools.indexpathsToIndexs(indexpaths: deleteIndexPath)
  23. for i in pageIndexs {
  24. if let page = showDocument?.page(at: UInt(i)) {
  25. deletePages.append(page)
  26. changeIndex.insert(i)
  27. }
  28. }
  29. showDocument?.removePage(at: pageIndexs)
  30. refreshDatas()
  31. collectionView.reloadData()
  32. currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  33. self?.undoInsertPages(insertPages: deletePages, indexs: pageIndexs)
  34. }
  35. }
  36. private func undoInsertPages(insertPages:[CPDFPage],indexs: IndexSet) {
  37. var indexpaths = Set<IndexPath>()
  38. var count: Int = 0
  39. for index in indexs {
  40. guard let pageCount = showDocument?.pageCount, index <= pageCount, count < insertPages.count, index != -1 else {
  41. KMPrint("index invalid. index: \(index)")
  42. break
  43. }
  44. showDocument?.insertPageObject(insertPages[count], at: UInt(index))
  45. indexpaths.insert(IndexPath(item: index, section: 0))
  46. count += 1
  47. }
  48. refreshDatas()
  49. collectionView.reloadData()
  50. collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
  51. collectionView.selectionIndexPaths = indexpaths
  52. currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  53. self?.deletePages(indexpaths: indexpaths)
  54. }
  55. }
  56. private func undoReplacePages(of targetIndexpaths: Set<IndexPath>, with documents: [CPDFDocument],insertIndexSet:IndexSet,orgPages:[CPDFPage],orgIndexs: IndexSet) {
  57. showDocument?.removePage(at: insertIndexSet)
  58. var indexpaths = Set<IndexPath>()
  59. var count: Int = 0
  60. for index in orgIndexs {
  61. guard let pageCount = showDocument?.pageCount, index <= pageCount, count < orgPages.count, index != -1 else {
  62. KMPrint("index invalid. index: \(index)")
  63. break
  64. }
  65. showDocument?.insertPageObject(orgPages[count], at: UInt(index))
  66. indexpaths.insert(IndexPath(item: index, section: 0))
  67. count += 1
  68. }
  69. refreshDatas()
  70. collectionView.reloadData()
  71. collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
  72. collectionView.selectionIndexPaths = indexpaths
  73. currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  74. self?.replacePages(of: targetIndexpaths, with: documents)
  75. }
  76. }
  77. private func undoMovePages(movePages:[CPDFPage],destinationDex:Int,orgPages:[CPDFPage],orgPageDexs:[Int]) {
  78. for (i, page) in orgPages.enumerated() {
  79. let dragIndex = page.pageIndex()
  80. let index:UInt = UInt(orgPageDexs[i])
  81. showDocument?.movePage(at: dragIndex, withPageAt: index)
  82. }
  83. var indexpaths = Set<IndexPath>()
  84. for (_, page) in movePages.enumerated() {
  85. let index = page.pageIndex()
  86. indexpaths.insert(IndexPath(item: Int(index), section: 0))
  87. }
  88. refreshDatas()
  89. collectionView.reloadData()
  90. collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
  91. collectionView.selectionIndexPaths = indexpaths
  92. currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  93. self?.movePages(dragPages: movePages, destinationDex: destinationDex)
  94. }
  95. }
  96. func insertFileComplete(newSelectIndexs: Set<IndexPath>){
  97. refreshDatas()
  98. collectionView.reloadData()
  99. collectionView.selectionIndexPaths = newSelectIndexs
  100. if newSelectIndexs.isEmpty { return }
  101. let firstIndexPath = newSelectIndexs.first
  102. collectionView.scrollToItems(at: [firstIndexPath ?? IndexPath(item: 0, section: 0)], scrollPosition: .top)
  103. currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  104. self?.undoDeleteIndexPaths(deleteIndexPath: newSelectIndexs)
  105. }
  106. }
  107. public func insertFormPages(insertPages: [CPDFPage],pageDex:Int) {
  108. let pageIndexDex: Int = pageDex
  109. var indexpaths = Set<IndexPath>()
  110. let page = insertPages.first
  111. if page?.document == showDocument {
  112. for (i, page) in insertPages.enumerated() {
  113. let isSuccessFul:Bool = showDocument?.insertPageObject(page, at: UInt(pageIndexDex+i)) == true
  114. if(isSuccessFul == true) {
  115. indexpaths.insert(IndexPath(item: pageIndexDex+i, section: 0))
  116. }
  117. }
  118. } else {
  119. if let pasteDocument = page?.document {
  120. var indexs: IndexSet = IndexSet()
  121. for (i, page) in insertPages.enumerated() {
  122. indexs.insert(Int(pasteDocument.index(for: page)))
  123. indexpaths.insert(IndexPath(item: pageIndexDex+i, section: 0))
  124. }
  125. let isSuccessFul:Bool = showDocument?.importPages(indexs, from: pasteDocument, at: UInt(pageIndexDex)) == true
  126. if(isSuccessFul == false){
  127. return
  128. }
  129. }
  130. }
  131. refreshDatas()
  132. collectionView.reloadData()
  133. collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
  134. collectionView.selectionIndexPaths = indexpaths
  135. currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  136. self?.undoDeleteIndexPaths(deleteIndexPath: indexpaths)
  137. }
  138. }
  139. public func insertBlankSize(pageSize: CGSize,pageDex:Int) {
  140. var indexpaths = Set<IndexPath>()
  141. let isSuccessFul = showDocument?.insertBlankPage(pageSize: pageSize, at: pageDex)
  142. if(isSuccessFul == true) {
  143. indexpaths.insert(IndexPath(item: pageDex, section: 0))
  144. }
  145. refreshDatas()
  146. collectionView.reloadData()
  147. collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
  148. collectionView.selectionIndexPaths = indexpaths
  149. currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  150. self?.undoDeleteIndexPaths(deleteIndexPath: indexpaths)
  151. }
  152. }
  153. public func rotatePages(indexPaths: Set<IndexPath>, rotateAngle: Int) {
  154. var tIndexPaths: Set<IndexPath> = []
  155. tIndexPaths = indexPaths
  156. for targetIndexPath in indexPaths {
  157. if let page = showDocument?.page(at: UInt(targetIndexPath.item)) {
  158. var pageRotate = page.rotation + rotateAngle
  159. if(pageRotate == -90) {
  160. pageRotate = 270
  161. } else if (pageRotate == 450) {
  162. pageRotate = 90
  163. }
  164. page.rotation = pageRotate
  165. let cellView = collectionView.item(at: targetIndexPath) as? KMNThumbnailCollectionViewItem
  166. if(cellView != nil) {
  167. cellView?.thumbnailMode.removeCacheImage()
  168. }
  169. }
  170. }
  171. collectionView.reloadItems(at: tIndexPaths) // Ensure correct type conversion
  172. collectionView.selectionIndexPaths = tIndexPaths
  173. currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  174. self?.rotatePages(indexPaths: tIndexPaths, rotateAngle: -rotateAngle)
  175. }
  176. }
  177. public func reversePages(indexs: IndexSet) {
  178. if let doc = showDocument {
  179. var theIdxs = indexs
  180. var res = false
  181. for _ in 0 ..< indexs.count {
  182. guard let first = theIdxs.first else {
  183. break
  184. }
  185. guard let last = theIdxs.last else {
  186. break
  187. }
  188. if first == last {
  189. break
  190. }
  191. res = doc.exchangePage(at: UInt(first), withPageAt: UInt(last))
  192. if res {
  193. theIdxs.remove(first)
  194. theIdxs.remove(last)
  195. }
  196. }
  197. if res {
  198. refreshDatas()
  199. let selected_indexpaths = KMNTools.indexsToIndexpaths(indexs: indexs)
  200. collectionView.reloadItems(at: selected_indexpaths)
  201. collectionView.selectionIndexPaths = selected_indexpaths
  202. currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  203. self?.reversePages(indexs: indexs)
  204. }
  205. }
  206. }
  207. }
  208. public func replacePages(of targetIndexpaths: Set<IndexPath>, with documents: [CPDFDocument]) {
  209. if (targetIndexpaths.count == 0 || documents.count == 0) {
  210. KMPrint("replace invalid.")
  211. return
  212. }
  213. var index = targetIndexpaths.sorted().first!.item
  214. var deletePages:[CPDFPage] = []
  215. let indexSet = KMNTools.indexpathsToIndexs(indexpaths: targetIndexpaths)
  216. for i in indexSet {
  217. if let page = showDocument?.page(at: UInt(i)) {
  218. deletePages.append(page)
  219. }
  220. }
  221. showDocument?.removePage(at: indexSet)
  222. var tIndexPaths: Set<IndexPath> = []
  223. for document in documents {
  224. for i in 0 ..< document.pageCount {
  225. if let page = document.page(at: i) {
  226. showDocument?.insertPageObject(page, at: UInt(index))
  227. tIndexPaths.insert(IndexPath(item: index, section: 0))
  228. index += 1
  229. }
  230. }
  231. thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: document)
  232. }
  233. refreshDatas()
  234. collectionView.reloadData()
  235. collectionView.selectionIndexPaths = tIndexPaths
  236. currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  237. self?.undoReplacePages(of: targetIndexpaths, with: documents, insertIndexSet: KMNTools.indexpathsToIndexs(indexpaths: tIndexPaths), orgPages: deletePages,orgIndexs: indexSet)
  238. }
  239. }
  240. public func deletePages(indexpaths:Set<IndexPath>) {
  241. var changeIndex:IndexSet = []
  242. var deletePages:[CPDFPage] = []
  243. let pageIndexs = KMNTools.indexpathsToIndexs(indexpaths: indexpaths)
  244. for i in pageIndexs {
  245. if let page = showDocument?.page(at: UInt(i)) {
  246. deletePages.append(page)
  247. changeIndex.insert(i)
  248. }
  249. }
  250. showDocument?.removePage(at: changeIndex)
  251. collectionView.selectionIndexPaths = []
  252. refreshDatas()
  253. collectionView.reloadData()
  254. currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  255. self?.undoInsertPages(insertPages: deletePages, indexs: pageIndexs)
  256. }
  257. }
  258. public func movePages(dragPages:[CPDFPage],destinationDex:Int) {
  259. guard !dragPages.isEmpty else { return }
  260. var destinationIndex = destinationDex
  261. let maxDragPageIndex = dragPages.last?.pageIndex() ?? 0
  262. let minDragPageIndex = dragPages.first?.pageIndex() ?? 0
  263. let rangeStart = min(Int(minDragPageIndex), destinationIndex)
  264. let rangeEnd = max(Int(maxDragPageIndex), destinationIndex)
  265. var changePages:[CPDFPage] = []
  266. var changePageIndexs:[Int] = []
  267. for index in (rangeStart...rangeEnd) {
  268. if let changePage = showDocument?.page(at: UInt(index)) {
  269. changePages.append(changePage)
  270. changePageIndexs.append(index)
  271. }
  272. }
  273. for dragPage in dragPages {
  274. let dragIndex = dragPage.pageIndex()
  275. if destinationIndex > dragIndex {
  276. destinationIndex -= 1
  277. showDocument?.movePage(at: dragIndex, withPageAt: UInt(destinationIndex))
  278. } else {
  279. showDocument?.movePage(at: dragIndex, withPageAt: UInt(destinationIndex))
  280. }
  281. destinationIndex += 1
  282. }
  283. var selectIndexPaths: Set<IndexPath> = []
  284. for dragPage in dragPages {
  285. let dragIndex = dragPage.pageIndex()
  286. selectIndexPaths.insert(IndexPath(item: Int(dragIndex), section: 0))
  287. }
  288. refreshDatas()
  289. collectionView.reloadData()
  290. collectionView.selectionIndexPaths = selectIndexPaths
  291. if selectIndexPaths.isEmpty { return }
  292. let firstIndexPath = selectIndexPaths.first
  293. collectionView.scrollToItems(at: [firstIndexPath ?? IndexPath(item: 0, section: 0)], scrollPosition: .top)
  294. currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
  295. self?.undoMovePages(movePages: dragPages, destinationDex: destinationDex, orgPages: changePages, orgPageDexs: changePageIndexs)
  296. }
  297. }
  298. public func insertFromFilePath(fileNames:[String],formDex:Int,indexDex:UInt,selectIndexs:Set<IndexPath>,completionBlock:@escaping (Set<IndexPath>)->Void)-> Void {
  299. let path = fileNames[formDex]
  300. var insertDex = indexDex
  301. var tSelectIndex = selectIndexs
  302. let pathExtension = URL(fileURLWithPath: path).pathExtension.lowercased()
  303. if pathExtension == "pdf", let pdf = CPDFDocument(url: URL(fileURLWithPath: path)) {
  304. if pdf.isLocked {
  305. self.view.window?.windowController?.showCheckPassword(url: pdf.documentURL, type: .owner) { success, resultPassword in
  306. if (resultPassword.isEmpty == false) {
  307. pdf.unlock(withPassword: resultPassword)
  308. for i in 0 ..< pdf.pageCount {
  309. let insertPage = pdf.page(at: i)
  310. self.showDocument?.insertPageObject(insertPage, at: insertDex)
  311. tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
  312. insertDex += 1
  313. }
  314. var tFormDex = formDex
  315. tFormDex += 1
  316. self.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: pdf)
  317. if(tFormDex < fileNames.count) {
  318. return self.insertFromFilePath(fileNames: fileNames, formDex: tFormDex, indexDex: indexDex, selectIndexs: tSelectIndex,completionBlock: completionBlock)
  319. } else {
  320. self.insertFileComplete(newSelectIndexs: tSelectIndex)
  321. return completionBlock(tSelectIndex)
  322. }
  323. }
  324. }
  325. } else {
  326. for i in 0 ..< pdf.pageCount {
  327. let insertPage = pdf.page(at: i)
  328. showDocument?.insertPageObject(insertPage ?? CPDFPage(), at: insertDex)
  329. tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
  330. insertDex += 1
  331. }
  332. var tFormDex = formDex
  333. tFormDex += 1
  334. thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: pdf)
  335. if(tFormDex < fileNames.count) {
  336. return insertFromFilePath(fileNames: fileNames, formDex: tFormDex, indexDex: indexDex, selectIndexs: tSelectIndex,completionBlock: completionBlock)
  337. } else {
  338. insertFileComplete(newSelectIndexs: tSelectIndex)
  339. return completionBlock(tSelectIndex)
  340. }
  341. }
  342. } else if supportDragFileTypes().contains(pathExtension) {
  343. if KMConvertPDFManager.supportImages().contains(pathExtension) {
  344. let isSueccessFul = insertImageFilePath(imagePath: path, pageDex: Int(indexDex))
  345. if(isSueccessFul) {
  346. tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
  347. insertDex += 1
  348. }
  349. var tFormDex = formDex
  350. tFormDex += 1
  351. if(tFormDex < fileNames.count) {
  352. return insertFromFilePath(fileNames: fileNames, formDex: tFormDex, indexDex: indexDex, selectIndexs: tSelectIndex,completionBlock: completionBlock)
  353. } else {
  354. insertFileComplete(newSelectIndexs: tSelectIndex)
  355. return completionBlock(tSelectIndex)
  356. }
  357. } else {
  358. KMNConvertTool.convertOffice(filePath: path) { convertPDFPath in
  359. if (convertPDFPath != nil) {
  360. let pathExtension = URL(fileURLWithPath: convertPDFPath!).pathExtension.lowercased()
  361. if pathExtension == "pdf", let pdf = CPDFDocument(url: URL(fileURLWithPath: convertPDFPath!)) {
  362. for i in 0 ..< pdf.pageCount {
  363. let insertPage = pdf.page(at: i)
  364. self.showDocument?.insertPageObject(insertPage, at: insertDex)
  365. tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
  366. insertDex += 1
  367. }
  368. self.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: pdf)
  369. var tFormDex = formDex
  370. tFormDex += 1
  371. if(tFormDex < fileNames.count) {
  372. return self.insertFromFilePath(fileNames: fileNames, formDex: tFormDex, indexDex: indexDex, selectIndexs: tSelectIndex,completionBlock: completionBlock)
  373. } else {
  374. self.insertFileComplete(newSelectIndexs: tSelectIndex)
  375. return completionBlock(tSelectIndex)
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. public func extractPages(indexpaths: Set<IndexPath>, oneDocumentPerPage: Bool, callback: @escaping KMResultBlock) {
  384. let pageIndexs = KMNTools.indexpathsToIndexs(indexpaths: indexpaths)
  385. let oneDocument = !oneDocumentPerPage
  386. let document = self.showDocument!
  387. /// 提取的页面
  388. var extractPages: Array<CPDFPage> = []
  389. for i in pageIndexs {
  390. guard let page = document.page(at: UInt(i)) else {
  391. continue
  392. }
  393. extractPages.append(page)
  394. }
  395. if (oneDocument) { /// 提取为一个文档
  396. var fileName = document.documentURL.deletingPathExtension().lastPathComponent
  397. fileName.append(" pages ")
  398. fileName.append(KMNTools.newParseSelectedIndexs(selectedIndex: pageIndexs.sorted()))
  399. fileName.append(".pdf")
  400. NSPanel.savePanel(self.view.window!, true) { panel in
  401. panel.nameFieldStringValue = fileName
  402. } completion: { response, url, isOpen in
  403. if (response != .OK) {
  404. callback(.cancel)
  405. return
  406. }
  407. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  408. DispatchQueue.global().async {
  409. var success = false
  410. let pdf = CPDFDocument.init()
  411. success = pdf!.extractAsOneDocument(withPages: extractPages, savePath: url!.path)
  412. DispatchQueue.main.async {
  413. if (success == false) {
  414. callback(.failure)
  415. return
  416. }
  417. if (isOpen == false) {
  418. NSWorkspace.shared.activateFileViewerSelecting([url!])
  419. } else {
  420. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
  421. }
  422. }
  423. callback(.success, [url!])
  424. }
  425. }
  426. }
  427. }
  428. } else {
  429. let panel = NSOpenPanel()
  430. panel.canChooseFiles = false
  431. panel.canChooseDirectories = true
  432. panel.canCreateDirectories = true
  433. panel.allowsMultipleSelection = false
  434. panel.beginSheetModal(for: self.view.window!) { response in
  435. if response != .OK {
  436. callback(.cancel)
  437. return
  438. }
  439. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  440. let outputURL = panel.url
  441. DispatchQueue.global().async {
  442. let folderName = String((document.documentURL.lastPathComponent.split(separator: ".")[0])) + "_extract"
  443. var filePath = URL(fileURLWithPath: outputURL!.path).appendingPathComponent(folderName).path
  444. var i = 1
  445. let testFilePath = filePath
  446. while FileManager.default.fileExists(atPath: filePath) {
  447. filePath = testFilePath + "\(i)"
  448. i += 1
  449. }
  450. try? FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false, attributes: nil)
  451. var successArray: [URL]?
  452. successArray = document.extractPerPageDocument(withPages: extractPages, folerPath: filePath)
  453. DispatchQueue.main.async {
  454. if successArray!.count == 0 {
  455. callback(.failure)
  456. return
  457. }
  458. NSWorkspace.shared.activateFileViewerSelecting(successArray!)
  459. callback(.success, successArray ?? NSURL())
  460. }
  461. }
  462. }
  463. }
  464. }
  465. }
  466. // MARK: - MenuItem
  467. @objc public func copyMenuItemAciton() {
  468. if IAPProductsManager.default().isAvailableAllFunction() == false {
  469. let winC = KMPurchaseCompareWindowController.sharedInstance()
  470. winC?.showWindow(nil)
  471. return
  472. }
  473. KMNThumbnailManager.manager.copyPages = []
  474. let indexpaths = collectionView.selectionIndexPaths
  475. for indexpath in indexpaths.sorted() {
  476. guard let page = showDocument?.page(at: UInt(indexpath.item))?.copy() as? CPDFPage else {
  477. continue
  478. }
  479. KMNThumbnailManager.manager.copyDocument.append(showDocument ?? CPDFDocument())
  480. KMNThumbnailManager.manager.copyPages.append(page)
  481. }
  482. }
  483. @objc public func pastMenuItemAciton(menuitemProperty:ComponentMenuitemProperty) {
  484. if IAPProductsManager.default().isAvailableAllFunction() == false {
  485. let winC = KMPurchaseCompareWindowController.sharedInstance()
  486. winC?.showWindow(nil)
  487. return
  488. }
  489. var pastIndex = 1
  490. let point = menuitemProperty.representedObject as? NSPoint
  491. if(point != nil) {
  492. let pointInCollectionView = collectionView.convert(point!, from: nil)
  493. let visibleItems = collectionView.visibleItems()
  494. let mouseX = pointInCollectionView.x
  495. let mouseY = pointInCollectionView.y
  496. // 获取当前行的所有 cell
  497. let currentRowItems = visibleItems.filter { item in
  498. if let indexPath = collectionView.indexPath(for: item),
  499. let cellAttributes = collectionView.layoutAttributesForItem(at: indexPath) {
  500. return cellAttributes.frame.minY <= mouseY && cellAttributes.frame.maxY >= mouseY
  501. }
  502. return false
  503. }
  504. if(mouseX < 24) { //点击区域在最左边
  505. // 找到最近右边的 cell
  506. let rightMostCell = currentRowItems.compactMap { collectionView.indexPath(for: $0) }
  507. .filter { indexPath in
  508. let cellRect = collectionView.layoutAttributesForItem(at: indexPath)?.frame
  509. return cellRect?.minX ?? 0 > mouseX // 只选择右侧的 cell
  510. }
  511. .sorted { ($0.item < $1.item) } // 按 item 的顺序排列
  512. .first // 选择第一个,即最右边的 cell
  513. pastIndex = (rightMostCell?.item ?? (Int(showDocument?.pageCount ?? 1)))
  514. } else {
  515. // 找到最近右边的 cell
  516. let leftMostCell = currentRowItems.compactMap { collectionView.indexPath(for: $0) }
  517. .filter { indexPath in
  518. let cellRect = collectionView.layoutAttributesForItem(at: indexPath)?.frame
  519. return cellRect?.maxX ?? 0 < mouseX // 只选择左侧的 cell
  520. }
  521. .sorted { $0.item > $1.item } // 按 item 的逆序排列,以选择最近的左侧 cell
  522. .first // 选择第一个,即最近的左边的 cell
  523. pastIndex = (leftMostCell?.item ?? (Int(showDocument?.pageCount ?? 1)) - 1) + 1
  524. }
  525. } else {
  526. let selectedIndexPaths = collectionView.selectionIndexPaths
  527. if(selectedIndexPaths.count > 0) {
  528. let maxmumIndexPath = selectedIndexPaths.max(by: { $0 < $1 })
  529. pastIndex = ((maxmumIndexPath?.item ?? 0) + 1)
  530. } else {
  531. pastIndex = Int(showDocument?.pageCount ?? 0)
  532. }
  533. }
  534. let copyPages = KMNThumbnailManager.manager.copyPages
  535. insertFormPages(insertPages: copyPages, pageDex: pastIndex)
  536. }
  537. @objc public func cutMenuItemAciton() {
  538. if IAPProductsManager.default().isAvailableAllFunction() == false {
  539. let winC = KMPurchaseCompareWindowController.sharedInstance()
  540. winC?.showWindow(nil)
  541. return
  542. }
  543. let indexpaths = collectionView.selectionIndexPaths
  544. for indexpath in indexpaths.sorted() {
  545. guard let page = showDocument?.page(at: UInt(indexpath.item))?.copy() as? CPDFPage else {
  546. continue
  547. }
  548. KMNThumbnailManager.manager.copyDocument.append(showDocument ?? CPDFDocument())
  549. KMNThumbnailManager.manager.copyPages.append(page)
  550. }
  551. deletePages(indexpaths: indexpaths)
  552. }
  553. @objc public func deleteMenuItemAciton() {
  554. if IAPProductsManager.default().isAvailableAllFunction() == false {
  555. let winC = KMPurchaseCompareWindowController.sharedInstance()
  556. winC?.showWindow(nil)
  557. return
  558. }
  559. let indexpaths = collectionView.selectionIndexPaths
  560. deletePages(indexpaths: indexpaths)
  561. }
  562. @objc func sharePageItemAction(menuItem:NSMenuItem) {
  563. if IAPProductsManager.default().isAvailableAllFunction() == false {
  564. let winC = KMPurchaseCompareWindowController.sharedInstance()
  565. winC?.showWindow(nil)
  566. return
  567. }
  568. let indexpaths = collectionView.selectionIndexPaths
  569. let doucument = showDocument
  570. let filename : String = doucument?.documentURL.lastPathComponent ?? ""
  571. let folderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(filename)) ?? ""
  572. try? FileManager.default.removeItem(atPath: folderPath)
  573. let pdfdocument = CPDFDocument()
  574. let ret = pdfdocument?.importPages(KMNTools.indexpathsToIndexs(indexpaths: indexpaths), from: doucument, at: 0) ?? false
  575. let url = URL(fileURLWithPath: folderPath)
  576. if ret {
  577. let success = pdfdocument?.write(toFile: folderPath)
  578. let represent = menuItem.representedObject as? NSSharingService
  579. represent?.perform(withItems: [url])
  580. }
  581. }
  582. @objc public func displayPageSizeAction() {
  583. let indexpaths = collectionView.selectionIndexPaths
  584. isShowPageSize = !isShowPageSize
  585. collectionView.selectionIndexPaths = indexpaths
  586. }
  587. private func insertFilePath(filePath:String,pdfPassword:String?) {
  588. let selectedIndexPaths = collectionView.selectionIndexPaths
  589. let insertPDF = KMNPDFInsertPDFWindowController(showDocument, filePath: filePath, password: pdfPassword, selectionIndexPaths: selectedIndexPaths)
  590. insertPDF.pdfCallback = { [weak self] fileAttribute, insertIdx in
  591. let doc = fileAttribute.pdfDocument
  592. var insertPages: [CPDFPage] = []
  593. for number in fileAttribute.fetchSelectPages() {
  594. if let page = doc?.page(at: UInt(number-1)) {
  595. insertPages.append(page)
  596. }
  597. }
  598. self?.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: doc)
  599. self?.insertFormPages(insertPages: insertPages, pageDex: insertIdx)
  600. }
  601. insertPDF.fileCallback = { [weak self] filePath, insertIdx in
  602. self?.insertFromFilePath(fileNames: [filePath], formDex: 0, indexDex: UInt(insertIdx), selectIndexs: [], completionBlock: { newSelectIndexs in
  603. })
  604. }
  605. insertPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  606. }
  607. // MARK: - public
  608. func updateThumnailItem(updateIndexPaths: Set<IndexPath>,isSelect:Bool) {
  609. for targetIndexPath in updateIndexPaths {
  610. let cellView = collectionView.item(at: targetIndexPath) as? KMNThumbnailCollectionViewItem
  611. if(cellView != nil) {
  612. cellView?.thumbnailMode.removeCacheImage()
  613. }
  614. }
  615. collectionView.reloadItems(at: updateIndexPaths)
  616. if(isSelect) {
  617. if updateIndexPaths.isEmpty { return }
  618. let firstIndexPath = updateIndexPaths.first
  619. collectionView.scrollToItems(at: [firstIndexPath ?? IndexPath(item: 0, section: 0)], scrollPosition: .top)
  620. collectionView.selectionIndexPaths = updateIndexPaths
  621. }
  622. }
  623. func updateAllThumnailItems() {
  624. KMNThumbnailManager.clearCacheFilePath(filePath: showDocument?.documentURL.path ?? "")
  625. collectionView.reloadData()
  626. }
  627. @objc public func insertFromPDFAction() {
  628. if IAPProductsManager.default().isAvailableAllFunction() == false {
  629. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  630. return
  631. }
  632. let openPanel = NSOpenPanel()
  633. openPanel.allowedFileTypes = supportDragFileTypes()
  634. openPanel.allowsMultipleSelection = false
  635. openPanel.beginSheetModal(for: NSWindow.currentWindow()) {[weak self] result in
  636. if result == NSApplication.ModalResponse.OK {
  637. let fileURL = openPanel.url
  638. if(fileURL?.pathExtension == "pdf") {
  639. let pdfDoc = CPDFDocument(url: fileURL)
  640. if let data = pdfDoc?.isLocked, data {
  641. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  642. self?.view.window?.windowController?.showCheckPassword(url: fileURL ?? NSURL.fileURL(withPath: ""), type: .owner) { result, pwd in
  643. if (pwd.isEmpty == false) {
  644. self?.insertFilePath(filePath: fileURL?.path ?? "", pdfPassword: pwd)
  645. }
  646. }
  647. }
  648. } else {
  649. self?.insertFilePath(filePath: fileURL?.path ?? "", pdfPassword: nil)
  650. }
  651. } else {
  652. self?.insertFilePath(filePath: fileURL?.path ?? "", pdfPassword: nil)
  653. }
  654. }
  655. }
  656. }
  657. @objc public func insertFromBlankAction() {
  658. if IAPProductsManager.default().isAvailableAllFunction() == false {
  659. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  660. return
  661. }
  662. let selectedIndexPaths = collectionView.selectionIndexPaths
  663. let insertPDF = KMNPDFInsertBlankWindowController(self.showDocument, selectionIndexPaths: selectedIndexPaths)
  664. insertPDF.callback = { [weak self] pageSize, insertIdx in
  665. self?.insertBlankSize(pageSize: pageSize, pageDex: insertIdx)
  666. }
  667. insertPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  668. }
  669. public func insertFromClipboardAction() {
  670. if IAPProductsManager.default().isAvailableAllFunction() == false {
  671. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  672. return
  673. }
  674. let selectedIndexPaths = collectionView.selectionIndexPaths
  675. var maxmumIndex = 1
  676. if(selectedIndexPaths.count > 0) {
  677. let maxmumIndexPath = selectedIndexPaths.max(by: { $0 < $1 })
  678. maxmumIndex = (maxmumIndexPath?.item ?? 0) + 1
  679. }
  680. var error: NSError?
  681. guard let document: CPDFDocument = KMNConvertTool.openDocumentWithImageFromPasteboard(NSPasteboard.general, error: &error) else {
  682. return
  683. }
  684. if let page: CPDFPage = (document.page(at: 0)) {
  685. insertFormPages(insertPages: [page], pageDex: maxmumIndex)
  686. }
  687. self.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: document)
  688. let ips: Set<IndexPath> = [IndexPath(item: maxmumIndex, section: 0)]
  689. refreshDatas()
  690. collectionView.selectionIndexPaths = ips
  691. collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
  692. }
  693. public func insertFromScannerAction() {
  694. if IAPProductsManager.default().isAvailableAllFunction() == false {
  695. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  696. return
  697. }
  698. let selectedIndexPaths = collectionView.selectionIndexPaths
  699. let maxmumIndexPath = selectedIndexPaths.max(by: { $0 < $1 })
  700. let vc = KMDeviceBrowserWindowController.shared
  701. vc.type = .scanner
  702. vc.importScannerFileCallback = { [weak self] (url: NSURL) -> Void in
  703. if let imag = NSImage(contentsOfFile: url.path! ) {
  704. let index = (maxmumIndexPath?.item ?? 0) + 1
  705. _ = self?.showDocument?.km_insertPage(imag.size, withImage: url.path! , at:UInt(index))
  706. self?.refreshDatas()
  707. let ips: Set<IndexPath> = [IndexPath(item: index, section: 0)]
  708. self?.collectionView.selectionIndexPaths = ips
  709. self?.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
  710. }
  711. }
  712. vc.showWindow(nil)
  713. vc.window?.center()
  714. }
  715. public func canUndo()->Bool {
  716. return true
  717. }
  718. public func undoPDFAction() {
  719. currentUndoManager?.undo()
  720. }
  721. public func canRodo()->Bool {
  722. return true
  723. }
  724. public func redoPDFAction() {
  725. currentUndoManager?.redo()
  726. }
  727. @objc public func extractPDFAction() {
  728. if IAPProductsManager.default().isAvailableAllFunction() == false {
  729. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  730. return
  731. }
  732. let selectedIndexPaths = collectionView.selectionIndexPaths
  733. if selectedIndexPaths.count < 1 {
  734. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Please select one or more pages first to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
  735. return
  736. }
  737. let extractPDF = KMNExtractPDFWindowController(self.showDocument, selectionIndexPaths: collectionView.selectionIndexPaths)
  738. extractPDF.callback = { [weak self] oneDocumentPerPage, isDeletePage in
  739. extractPDF.own_closeEndSheet()
  740. if let _ = self?.showDocument {
  741. self?.extractPages(indexpaths: selectedIndexPaths, oneDocumentPerPage: oneDocumentPerPage, callback: { [weak self] result, params in
  742. if (result == .failure || result == .cancel) {
  743. return
  744. }
  745. if (isDeletePage) {
  746. self?.deletePages(indexpaths: selectedIndexPaths)
  747. }
  748. })
  749. }
  750. }
  751. extractPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  752. }
  753. @objc public func replacePDFAction() {
  754. if IAPProductsManager.default().isAvailableAllFunction() == false {
  755. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  756. return
  757. }
  758. let selectedIndexPaths = collectionView.selectionIndexPaths
  759. if selectedIndexPaths.count < 1 {
  760. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Please select one or more pages first to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
  761. return
  762. }
  763. self.km_open_file_multi { [unowned self] index, params in
  764. if (self.fetchProgressBlockParamsIsPasswordFile(params: params)) { // 加密文档进度回调
  765. return
  766. }
  767. let tFileUrl = self.fetchProgressBlockParamsForFileUrl(params: params)
  768. let pdfExtensions = KMNConvertTool.supportPDFFileType()
  769. if let exn = tFileUrl?.pathExtension, pdfExtensions.contains(exn) {
  770. if (tFileUrl!.path.isPDFValid() == false) {
  771. let alert = NSAlert()
  772. alert.alertStyle = .critical
  773. alert.messageText = KMLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.")
  774. alert.runModal()
  775. }
  776. }
  777. } completionBlock: { [unowned self] documents in
  778. self.replacePages(of: selectedIndexPaths, with: documents)
  779. }
  780. }
  781. public func splitPDFAction() {
  782. if IAPProductsManager.default().isAvailableAllFunction() == false {
  783. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  784. return
  785. }
  786. let selectedIndexPaths = collectionView.selectionIndexPaths
  787. if collectionView.selectionIndexPaths.count < 1 {
  788. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Please select one or more pages first to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.bounds.origin.x + self.view.bounds.size.width/2, self.view.bounds.size.height - 30))
  789. return
  790. }
  791. let splitPDF = KMNSplitPDFWindowController(self.showDocument,selectionIndexPaths: selectedIndexPaths)
  792. splitPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  793. }
  794. public func reversePDFAction() {
  795. if IAPProductsManager.default().isAvailableAllFunction() == false {
  796. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  797. return
  798. }
  799. let selectedIndexPaths = collectionView.selectionIndexPaths
  800. if selectedIndexPaths.count < 2 {
  801. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("No page selected. Please select at least two pages to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
  802. return
  803. }
  804. reversePages(indexs:KMNTools.indexpathsToIndexs(indexpaths: selectedIndexPaths))
  805. }
  806. @objc public func rotatePageLeftAction() {
  807. if IAPProductsManager.default().isAvailableAllFunction() == false {
  808. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  809. return
  810. }
  811. if collectionView.selectionIndexPaths.count < 1 {
  812. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Please select one or more pages first to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
  813. return
  814. }
  815. rotatePages(indexPaths: collectionView.selectionIndexPaths, rotateAngle: -90)
  816. }
  817. @objc public func rotatePageRightAction() {
  818. if IAPProductsManager.default().isAvailableAllFunction() == false {
  819. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  820. return
  821. }
  822. if collectionView.selectionIndexPaths.count < 1 {
  823. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Please select one or more pages first to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
  824. return
  825. }
  826. rotatePages(indexPaths: collectionView.selectionIndexPaths, rotateAngle: 90)
  827. }
  828. public func deletePageAction() {
  829. if IAPProductsManager.default().isAvailableAllFunction() == false {
  830. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  831. return
  832. }
  833. let selectedIndexPaths = collectionView.selectionIndexPaths
  834. if selectedIndexPaths.count < 1 {
  835. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Please select one or more pages first to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
  836. return
  837. } else if selectedIndexPaths.count == (showDocument?.pageCount ?? 0) {
  838. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Can not delete all pages."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
  839. return
  840. }
  841. deletePages(indexpaths: selectedIndexPaths)
  842. }
  843. // MARK: - private
  844. private func fetchProgressBlockParamsIsPasswordFile(params: Any...) -> Bool {
  845. if (params.count <= 2) {
  846. return false
  847. }
  848. return true
  849. }
  850. private func fetchProgressBlockParamsForFileUrl(params: Any...) -> URL? {
  851. if (params.count < 2) {
  852. return nil
  853. }
  854. return params[1] as? URL
  855. }
  856. private func km_open_file_multi(type: KMPasswordInputWindowType = .open, progressBlock: ((_ index: Int, _ params: Any...)->Void)? = nil, completionBlock:@escaping ([CPDFDocument])->Void) {
  857. NSPanel.km_open_multi_success(self.view.window!) { panel in
  858. var array: [String] = []
  859. for fileType in KMConvertPDFManager.supportFileType() {
  860. array.append(fileType)
  861. }
  862. panel.allowedFileTypes = KMNConvertTool.pdfExtensions + array
  863. } completion: { urls in
  864. self.km_add_file_multi(fileUrls: urls, type: type, progressBlock: progressBlock, completionBlock: completionBlock)
  865. }
  866. }
  867. private func km_add_file_multi(fileUrls: [URL] ,type: KMPasswordInputWindowType = .open, progressBlock: ((_ index: Int, _ params: Any...)->Void)? = nil, completionBlock:@escaping ([CPDFDocument])->Void) {
  868. var pdfUrls: [URL] = []
  869. var imageUrls: [URL] = []
  870. var officeUrls: [URL] = []
  871. for url in fileUrls {
  872. let type = url.pathExtension.lowercased()
  873. if (KMNConvertTool.isPDFType(type)) {
  874. pdfUrls.append(url)
  875. }
  876. if (KMNConvertTool.isImageType(type)) {
  877. imageUrls.append(url)
  878. }
  879. if (KMNConvertTool.isOfficeType(type)) {
  880. officeUrls.append(url)
  881. }
  882. }
  883. if (officeUrls.count == 0) {
  884. self.km_add_pdf_multi(fileUrls: pdfUrls, type: type, progressBlock: progressBlock) { documents in
  885. var index = documents.count
  886. var _documents: [CPDFDocument] = []
  887. for imageUrl in imageUrls {
  888. index += 1
  889. let document = CPDFDocument()
  890. let image = NSImage(contentsOfFile: imageUrl.path)
  891. let _ = document?.km_insertPage(image?.size ?? .zero, withImage: imageUrl.path, at: 0)
  892. _documents.append(document!)
  893. if let _callback = progressBlock { // 回调进度
  894. _callback(index, document as Any, imageUrl)
  895. }
  896. }
  897. completionBlock(documents + _documents)
  898. }
  899. return
  900. }
  901. self.km_add_office_multi(fileUrls: officeUrls) { [unowned self] fileUrlStrings in
  902. var officeDocuments: [CPDFDocument] = []
  903. var index = 0
  904. for fileUrlString in fileUrlStrings {
  905. index += 1
  906. let document = CPDFDocument(url: URL(fileURLWithPath: fileUrlString))
  907. officeDocuments.append(document!)
  908. if let _callback = progressBlock { // 回调进度
  909. _callback(index, document as Any, URL(fileURLWithPath: fileUrlString))
  910. }
  911. }
  912. self.km_add_pdf_multi(fileUrls: pdfUrls) { documents in
  913. var index = documents.count + officeDocuments.count
  914. var _documents: [CPDFDocument] = []
  915. for imageUrl in imageUrls {
  916. index += 1
  917. let document = CPDFDocument()
  918. let image = NSImage(contentsOfFile: imageUrl.path)
  919. let _ = document?.km_insertPage(image!.size, withImage: imageUrl.path, at: 0)
  920. _documents.append(document!)
  921. if let _callback = progressBlock { // 回调进度
  922. _callback(index, document as Any, imageUrl)
  923. }
  924. }
  925. completionBlock(officeDocuments + documents + _documents)
  926. }
  927. }
  928. }
  929. private func km_add_pdf_multi(fileUrls: [URL] ,type: KMPasswordInputWindowType = .open, progressBlock: ((_ index: Int, _ params: Any...)->Void)? = nil, completionBlock:@escaping ([CPDFDocument])->Void) {
  930. var results: [CPDFDocument] = []
  931. self.lockedFiles.removeAll()
  932. var index = 0
  933. for url in fileUrls {
  934. let document = CPDFDocument(url: url)
  935. if (document!.isLocked) {
  936. self.lockedFiles.append(url)
  937. continue
  938. }
  939. if let _document = document {
  940. results.append(_document)
  941. }
  942. index += 1
  943. if let _callback = progressBlock {
  944. _callback(index, ((document != nil) ? document : CPDFDocument()) as Any, url)
  945. }
  946. }
  947. if (self.lockedFiles.count == 0) {
  948. completionBlock(results)
  949. return
  950. }
  951. self._openPasswordWindow_loop(fileUrl: self.lockedFiles.first!, type: type) { params in
  952. index += 1
  953. if (params.count <= 2) { // 参数错误
  954. if let _callback = progressBlock { // 回调进度
  955. _callback(index)
  956. }
  957. return
  958. }
  959. let fileUrl = params[0] as! URL
  960. let result = params[1] as! KMPasswordInputWindowResult
  961. let password = params[2] as? String
  962. if (result == .cancel) {
  963. if let _callback = progressBlock { // 回调进度
  964. _callback(index, CPDFDocument() as Any, fileUrl, result)
  965. }
  966. return
  967. }
  968. let document = CPDFDocument(url: fileUrl)
  969. if let _password = password { // 将文档进行解密
  970. document?.unlock(withPassword: _password)
  971. }
  972. if let _callback = progressBlock { // 回调进度
  973. _callback(index, document as Any, fileUrl, result, password as Any)
  974. }
  975. // 将文档加入返回数据
  976. if let _document = document {
  977. results.append(_document)
  978. }
  979. } completionBlock: {
  980. completionBlock(results)
  981. }
  982. }
  983. private func km_add_office_multi(fileUrls: [URL], completionBlock:@escaping ([String])->Void) -> Void {
  984. var fileUrlStrings: [String] = []
  985. let dispatchGroup = Dispatch.DispatchGroup()
  986. for (index, fileUrl) in fileUrls.enumerated() {
  987. let filePath = fileUrl.path
  988. let folderPath = "convertToPDF_\(index).pdf"
  989. let savePath: String? = folderPath.kUrlToPDFFolderPath() as String
  990. if (savePath == nil) {
  991. continue
  992. }
  993. dispatchGroup.enter()
  994. KMConvertPDFManager.convertFile(filePath, savePath: savePath!) { success, errorDic in
  995. if errorDic != nil || !success || !FileManager.default.fileExists(atPath: savePath!) {
  996. dispatchGroup.leave()
  997. if FileManager.default.fileExists(atPath: savePath!) {
  998. try?FileManager.default.removeItem(atPath: savePath!)
  999. }
  1000. let alert = NSAlert.init()
  1001. alert.alertStyle = .critical
  1002. var infoString = ""
  1003. if errorDic != nil {
  1004. for key in (errorDic! as Dictionary).keys {
  1005. infoString = infoString.appendingFormat("%@\n", errorDic![key] as! CVarArg)
  1006. }
  1007. }
  1008. alert.informativeText = KMLocalizedString("Please install Microsoft Office to create PDFs from Office files")
  1009. alert.messageText = KMLocalizedString("Failed to Create PDF", comment: "")
  1010. alert.addButton(withTitle: KMLocalizedString("OK"))
  1011. alert.runModal()
  1012. return
  1013. }
  1014. if !savePath!.isPDFValid() {
  1015. dispatchGroup.leave()
  1016. let alert = NSAlert()
  1017. alert.alertStyle = .critical
  1018. alert.messageText = KMLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.")
  1019. alert.runModal()
  1020. return
  1021. }
  1022. fileUrlStrings.append(savePath!)
  1023. dispatchGroup.leave()
  1024. }
  1025. }
  1026. dispatchGroup.notify(queue: DispatchQueue.main) {
  1027. completionBlock(fileUrlStrings)
  1028. }
  1029. }
  1030. fileprivate func _openPasswordWindow_loop(fileUrl: URL, type: KMPasswordInputWindowType, progressBlock: ((_ params: Any...)->Void)?, completionBlock:@escaping ()->Void) {
  1031. KMPasswordInputWindow.openWindow(window: self.view.window!, type: type, url: fileUrl) { [weak self] result, password in
  1032. // 将结果返回
  1033. if let _callback = progressBlock {
  1034. _callback(fileUrl, result, password as Any)
  1035. }
  1036. // 进行下一个
  1037. self?.lockedFiles.removeFirst()
  1038. if let _fileUrl = self?.lockedFiles.first {
  1039. self?._openPasswordWindow_loop(fileUrl: _fileUrl, type: type, progressBlock: progressBlock, completionBlock: completionBlock)
  1040. } else {
  1041. completionBlock()
  1042. }
  1043. }
  1044. }
  1045. }