KMNThumbnailBaseViewController+Action.swift 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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. extractPages.append(document.page(at: UInt(i)))
  391. }
  392. if (oneDocument) { /// 提取为一个文档
  393. var fileName = document.documentURL.deletingPathExtension().lastPathComponent
  394. fileName.append(" pages ")
  395. fileName.append(KMNTools.newParseSelectedIndexs(selectedIndex: pageIndexs.sorted()))
  396. fileName.append(".pdf")
  397. NSPanel.savePanel(self.view.window!, true) { panel in
  398. panel.nameFieldStringValue = fileName
  399. } completion: { response, url, isOpen in
  400. if (response != .OK) {
  401. callback(.cancel)
  402. return
  403. }
  404. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  405. DispatchQueue.global().async {
  406. var success = false
  407. let pdf = CPDFDocument.init()
  408. success = pdf!.extractAsOneDocument(withPages: extractPages, savePath: url!.path)
  409. DispatchQueue.main.async {
  410. if (success == false) {
  411. callback(.failure)
  412. return
  413. }
  414. if (isOpen == false) {
  415. NSWorkspace.shared.activateFileViewerSelecting([url!])
  416. } else {
  417. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
  418. }
  419. }
  420. callback(.success, [url!])
  421. }
  422. }
  423. }
  424. }
  425. } else {
  426. let panel = NSOpenPanel()
  427. panel.canChooseFiles = false
  428. panel.canChooseDirectories = true
  429. panel.canCreateDirectories = true
  430. panel.allowsMultipleSelection = false
  431. panel.beginSheetModal(for: self.view.window!) { response in
  432. if response != .OK {
  433. callback(.cancel)
  434. return
  435. }
  436. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  437. let outputURL = panel.url
  438. DispatchQueue.global().async {
  439. let folderName = String((document.documentURL.lastPathComponent.split(separator: ".")[0])) + "_extract"
  440. var filePath = URL(fileURLWithPath: outputURL!.path).appendingPathComponent(folderName).path
  441. var i = 1
  442. let testFilePath = filePath
  443. while FileManager.default.fileExists(atPath: filePath) {
  444. filePath = testFilePath + "\(i)"
  445. i += 1
  446. }
  447. try? FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false, attributes: nil)
  448. var successArray: [URL]?
  449. successArray = document.extractPerPageDocument(withPages: extractPages, folerPath: filePath)
  450. DispatchQueue.main.async {
  451. if successArray!.count == 0 {
  452. callback(.failure)
  453. return
  454. }
  455. NSWorkspace.shared.activateFileViewerSelecting(successArray!)
  456. callback(.success, successArray ?? NSURL())
  457. }
  458. }
  459. }
  460. }
  461. }
  462. }
  463. // MARK: - MenuItem
  464. @objc public func copyMenuItemAciton() {
  465. if IAPProductsManager.default().isAvailableAllFunction() == false {
  466. let winC = KMPurchaseCompareWindowController.sharedInstance()
  467. winC?.showWindow(nil)
  468. return
  469. }
  470. KMNThumbnailManager.manager.copyPages = []
  471. let indexpaths = collectionView.selectionIndexPaths
  472. for indexpath in indexpaths.sorted() {
  473. guard let page = showDocument?.page(at: UInt(indexpath.item)).copy() as? CPDFPage else {
  474. continue
  475. }
  476. KMNThumbnailManager.manager.copyDocument.append(showDocument ?? CPDFDocument())
  477. KMNThumbnailManager.manager.copyPages.append(page)
  478. }
  479. }
  480. @objc public func pastMenuItemAciton(menuitemProperty:ComponentMenuitemProperty) {
  481. if IAPProductsManager.default().isAvailableAllFunction() == false {
  482. let winC = KMPurchaseCompareWindowController.sharedInstance()
  483. winC?.showWindow(nil)
  484. return
  485. }
  486. var pastIndex = 1
  487. let point = menuitemProperty.representedObject as? NSPoint
  488. if(point != nil) {
  489. let pointInCollectionView = collectionView.convert(point!, from: nil)
  490. let visibleItems = collectionView.visibleItems()
  491. let mouseX = pointInCollectionView.x
  492. let mouseY = pointInCollectionView.y
  493. // 获取当前行的所有 cell
  494. let currentRowItems = visibleItems.filter { item in
  495. if let indexPath = collectionView.indexPath(for: item),
  496. let cellAttributes = collectionView.layoutAttributesForItem(at: indexPath) {
  497. return cellAttributes.frame.minY <= mouseY && cellAttributes.frame.maxY >= mouseY
  498. }
  499. return false
  500. }
  501. if(mouseX < 24) { //点击区域在最左边
  502. // 找到最近右边的 cell
  503. let rightMostCell = currentRowItems.compactMap { collectionView.indexPath(for: $0) }
  504. .filter { indexPath in
  505. let cellRect = collectionView.layoutAttributesForItem(at: indexPath)?.frame
  506. return cellRect?.minX ?? 0 > mouseX // 只选择右侧的 cell
  507. }
  508. .sorted { ($0.item < $1.item) } // 按 item 的顺序排列
  509. .first // 选择第一个,即最右边的 cell
  510. pastIndex = (rightMostCell?.item ?? (Int(showDocument?.pageCount ?? 1)))
  511. } else {
  512. // 找到最近右边的 cell
  513. let leftMostCell = currentRowItems.compactMap { collectionView.indexPath(for: $0) }
  514. .filter { indexPath in
  515. let cellRect = collectionView.layoutAttributesForItem(at: indexPath)?.frame
  516. return cellRect?.maxX ?? 0 < mouseX // 只选择左侧的 cell
  517. }
  518. .sorted { $0.item > $1.item } // 按 item 的逆序排列,以选择最近的左侧 cell
  519. .first // 选择第一个,即最近的左边的 cell
  520. pastIndex = (leftMostCell?.item ?? (Int(showDocument?.pageCount ?? 1)) - 1) + 1
  521. }
  522. } else {
  523. let selectedIndexPaths = collectionView.selectionIndexPaths
  524. if(selectedIndexPaths.count > 0) {
  525. let maxmumIndexPath = selectedIndexPaths.max(by: { $0 < $1 })
  526. pastIndex = ((maxmumIndexPath?.item ?? 0) + 1)
  527. } else {
  528. pastIndex = Int(showDocument?.pageCount ?? 0)
  529. }
  530. }
  531. let copyPages = KMNThumbnailManager.manager.copyPages
  532. insertFormPages(insertPages: copyPages, pageDex: pastIndex)
  533. }
  534. @objc public func cutMenuItemAciton() {
  535. if IAPProductsManager.default().isAvailableAllFunction() == false {
  536. let winC = KMPurchaseCompareWindowController.sharedInstance()
  537. winC?.showWindow(nil)
  538. return
  539. }
  540. let indexpaths = collectionView.selectionIndexPaths
  541. for indexpath in indexpaths.sorted() {
  542. guard let page = showDocument?.page(at: UInt(indexpath.item)).copy() as? CPDFPage else {
  543. continue
  544. }
  545. KMNThumbnailManager.manager.copyDocument.append(showDocument ?? CPDFDocument())
  546. KMNThumbnailManager.manager.copyPages.append(page)
  547. }
  548. deletePages(indexpaths: indexpaths)
  549. }
  550. @objc public func deleteMenuItemAciton() {
  551. if IAPProductsManager.default().isAvailableAllFunction() == false {
  552. let winC = KMPurchaseCompareWindowController.sharedInstance()
  553. winC?.showWindow(nil)
  554. return
  555. }
  556. let indexpaths = collectionView.selectionIndexPaths
  557. deletePages(indexpaths: indexpaths)
  558. }
  559. @objc func sharePageItemAction(menuItem:NSMenuItem) {
  560. if IAPProductsManager.default().isAvailableAllFunction() == false {
  561. let winC = KMPurchaseCompareWindowController.sharedInstance()
  562. winC?.showWindow(nil)
  563. return
  564. }
  565. let indexpaths = collectionView.selectionIndexPaths
  566. let doucument = showDocument
  567. let filename : String = doucument?.documentURL.lastPathComponent ?? ""
  568. let folderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(filename)) ?? ""
  569. try? FileManager.default.removeItem(atPath: folderPath)
  570. let pdfdocument = CPDFDocument()
  571. let ret = pdfdocument?.importPages(KMNTools.indexpathsToIndexs(indexpaths: indexpaths), from: doucument, at: 0) ?? false
  572. let url = URL(fileURLWithPath: folderPath)
  573. if ret {
  574. let success = pdfdocument?.write(toFile: folderPath)
  575. let represent = menuItem.representedObject as? NSSharingService
  576. represent?.perform(withItems: [url])
  577. }
  578. }
  579. @objc public func displayPageSizeAction() {
  580. let indexpaths = collectionView.selectionIndexPaths
  581. isShowPageSize = !isShowPageSize
  582. collectionView.selectionIndexPaths = indexpaths
  583. }
  584. private func insertFilePath(filePath:String,pdfPassword:String?) {
  585. let selectedIndexPaths = collectionView.selectionIndexPaths
  586. let insertPDF = KMNPDFInsertPDFWindowController(showDocument, filePath: filePath, password: pdfPassword, selectionIndexPaths: selectedIndexPaths)
  587. insertPDF.pdfCallback = { [weak self] fileAttribute, insertIdx in
  588. let doc = fileAttribute.pdfDocument
  589. var insertPages: [CPDFPage] = []
  590. for number in fileAttribute.fetchSelectPages() {
  591. if let page = doc?.page(at: UInt(number-1)) {
  592. insertPages.append(page)
  593. }
  594. }
  595. self?.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: doc)
  596. self?.insertFormPages(insertPages: insertPages, pageDex: insertIdx)
  597. }
  598. insertPDF.fileCallback = { [weak self] filePath, insertIdx in
  599. self?.insertFromFilePath(fileNames: [filePath], formDex: 0, indexDex: UInt(insertIdx), selectIndexs: [], completionBlock: { newSelectIndexs in
  600. })
  601. }
  602. insertPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  603. }
  604. // MARK: - public
  605. func updateThumnailItem(updateIndexPaths: Set<IndexPath>,isSelect:Bool) {
  606. for targetIndexPath in updateIndexPaths {
  607. let cellView = collectionView.item(at: targetIndexPath) as? KMNThumbnailCollectionViewItem
  608. if(cellView != nil) {
  609. cellView?.thumbnailMode.removeCacheImage()
  610. }
  611. }
  612. collectionView.reloadItems(at: updateIndexPaths)
  613. if(isSelect) {
  614. if updateIndexPaths.isEmpty { return }
  615. let firstIndexPath = updateIndexPaths.first
  616. collectionView.scrollToItems(at: [firstIndexPath ?? IndexPath(item: 0, section: 0)], scrollPosition: .top)
  617. collectionView.selectionIndexPaths = updateIndexPaths
  618. }
  619. }
  620. func updateAllThumnailItems() {
  621. KMNThumbnailManager.clearCacheFilePath(filePath: showDocument?.documentURL.path ?? "")
  622. collectionView.reloadData()
  623. }
  624. @objc public func insertFromPDFAction() {
  625. if IAPProductsManager.default().isAvailableAllFunction() == false {
  626. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  627. return
  628. }
  629. let openPanel = NSOpenPanel()
  630. openPanel.allowedFileTypes = supportDragFileTypes()
  631. openPanel.allowsMultipleSelection = false
  632. openPanel.beginSheetModal(for: NSWindow.currentWindow()) {[weak self] result in
  633. if result == NSApplication.ModalResponse.OK {
  634. let fileURL = openPanel.url
  635. if(fileURL?.pathExtension == "pdf") {
  636. let pdfDoc = CPDFDocument(url: fileURL)
  637. if let data = pdfDoc?.isLocked, data {
  638. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  639. self?.view.window?.windowController?.showCheckPassword(url: fileURL ?? NSURL.fileURL(withPath: ""), type: .owner) { result, pwd in
  640. if (pwd.isEmpty == false) {
  641. self?.insertFilePath(filePath: fileURL?.path ?? "", pdfPassword: pwd)
  642. }
  643. }
  644. }
  645. } else {
  646. self?.insertFilePath(filePath: fileURL?.path ?? "", pdfPassword: nil)
  647. }
  648. } else {
  649. self?.insertFilePath(filePath: fileURL?.path ?? "", pdfPassword: nil)
  650. }
  651. }
  652. }
  653. }
  654. @objc public func insertFromBlankAction() {
  655. if IAPProductsManager.default().isAvailableAllFunction() == false {
  656. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  657. return
  658. }
  659. let selectedIndexPaths = collectionView.selectionIndexPaths
  660. let insertPDF = KMNPDFInsertBlankWindowController(self.showDocument, selectionIndexPaths: selectedIndexPaths)
  661. insertPDF.callback = { [weak self] pageSize, insertIdx in
  662. self?.insertBlankSize(pageSize: pageSize, pageDex: insertIdx)
  663. }
  664. insertPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  665. }
  666. public func insertFromClipboardAction() {
  667. if IAPProductsManager.default().isAvailableAllFunction() == false {
  668. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  669. return
  670. }
  671. let selectedIndexPaths = collectionView.selectionIndexPaths
  672. var maxmumIndex = 1
  673. if(selectedIndexPaths.count > 0) {
  674. let maxmumIndexPath = selectedIndexPaths.max(by: { $0 < $1 })
  675. maxmumIndex = (maxmumIndexPath?.item ?? 0) + 1
  676. }
  677. var error: NSError?
  678. guard let document: CPDFDocument = KMNConvertTool.openDocumentWithImageFromPasteboard(NSPasteboard.general, error: &error) else {
  679. return
  680. }
  681. if let page: CPDFPage = (document.page(at: 0)) {
  682. insertFormPages(insertPages: [page], pageDex: maxmumIndex)
  683. }
  684. self.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: document)
  685. let ips: Set<IndexPath> = [IndexPath(item: maxmumIndex, section: 0)]
  686. refreshDatas()
  687. collectionView.selectionIndexPaths = ips
  688. collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
  689. }
  690. public func insertFromScannerAction() {
  691. if IAPProductsManager.default().isAvailableAllFunction() == false {
  692. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  693. return
  694. }
  695. let selectedIndexPaths = collectionView.selectionIndexPaths
  696. let maxmumIndexPath = selectedIndexPaths.max(by: { $0 < $1 })
  697. let vc = KMDeviceBrowserWindowController.shared
  698. vc.type = .scanner
  699. vc.importScannerFileCallback = { [weak self] (url: NSURL) -> Void in
  700. if let imag = NSImage(contentsOfFile: url.path! ) {
  701. let index = (maxmumIndexPath?.item ?? 0) + 1
  702. _ = self?.showDocument?.km_insertPage(imag.size, withImage: url.path! , at:UInt(index))
  703. self?.refreshDatas()
  704. let ips: Set<IndexPath> = [IndexPath(item: index, section: 0)]
  705. self?.collectionView.selectionIndexPaths = ips
  706. self?.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
  707. }
  708. }
  709. vc.showWindow(nil)
  710. vc.window?.center()
  711. }
  712. public func canUndo()->Bool {
  713. return true
  714. }
  715. public func undoPDFAction() {
  716. currentUndoManager?.undo()
  717. }
  718. public func canRodo()->Bool {
  719. return true
  720. }
  721. public func redoPDFAction() {
  722. currentUndoManager?.redo()
  723. }
  724. @objc public func extractPDFAction() {
  725. if IAPProductsManager.default().isAvailableAllFunction() == false {
  726. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  727. return
  728. }
  729. let selectedIndexPaths = collectionView.selectionIndexPaths
  730. if selectedIndexPaths.count < 1 {
  731. _ = 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))
  732. return
  733. }
  734. let extractPDF = KMNExtractPDFWindowController(self.showDocument, selectionIndexPaths: collectionView.selectionIndexPaths)
  735. extractPDF.callback = { [weak self] oneDocumentPerPage, isDeletePage in
  736. extractPDF.own_closeEndSheet()
  737. if let _ = self?.showDocument {
  738. self?.extractPages(indexpaths: selectedIndexPaths, oneDocumentPerPage: oneDocumentPerPage, callback: { [weak self] result, params in
  739. if (result == .failure || result == .cancel) {
  740. return
  741. }
  742. if (isDeletePage) {
  743. self?.deletePages(indexpaths: selectedIndexPaths)
  744. }
  745. })
  746. }
  747. }
  748. extractPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  749. }
  750. @objc public func replacePDFAction() {
  751. if IAPProductsManager.default().isAvailableAllFunction() == false {
  752. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  753. return
  754. }
  755. let selectedIndexPaths = collectionView.selectionIndexPaths
  756. if selectedIndexPaths.count < 1 {
  757. _ = 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))
  758. return
  759. }
  760. self.km_open_file_multi { [unowned self] index, params in
  761. if (self.fetchProgressBlockParamsIsPasswordFile(params: params)) { // 加密文档进度回调
  762. return
  763. }
  764. let tFileUrl = self.fetchProgressBlockParamsForFileUrl(params: params)
  765. let pdfExtensions = KMNConvertTool.supportPDFFileType()
  766. if let exn = tFileUrl?.pathExtension, pdfExtensions.contains(exn) {
  767. if (tFileUrl!.path.isPDFValid() == false) {
  768. let alert = NSAlert()
  769. alert.alertStyle = .critical
  770. alert.messageText = KMLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.")
  771. alert.runModal()
  772. }
  773. }
  774. } completionBlock: { [unowned self] documents in
  775. self.replacePages(of: selectedIndexPaths, with: documents)
  776. }
  777. }
  778. public func splitPDFAction() {
  779. if IAPProductsManager.default().isAvailableAllFunction() == false {
  780. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  781. return
  782. }
  783. let selectedIndexPaths = collectionView.selectionIndexPaths
  784. if collectionView.selectionIndexPaths.count < 1 {
  785. _ = 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))
  786. return
  787. }
  788. let splitPDF = KMNSplitPDFWindowController(self.showDocument,selectionIndexPaths: selectedIndexPaths)
  789. splitPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  790. }
  791. public func reversePDFAction() {
  792. if IAPProductsManager.default().isAvailableAllFunction() == false {
  793. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  794. return
  795. }
  796. let selectedIndexPaths = collectionView.selectionIndexPaths
  797. if selectedIndexPaths.count < 2 {
  798. _ = 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))
  799. return
  800. }
  801. reversePages(indexs:KMNTools.indexpathsToIndexs(indexpaths: selectedIndexPaths))
  802. }
  803. @objc public func rotatePageLeftAction() {
  804. if IAPProductsManager.default().isAvailableAllFunction() == false {
  805. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  806. return
  807. }
  808. if collectionView.selectionIndexPaths.count < 1 {
  809. _ = 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))
  810. return
  811. }
  812. rotatePages(indexPaths: collectionView.selectionIndexPaths, rotateAngle: -90)
  813. }
  814. @objc public func rotatePageRightAction() {
  815. if IAPProductsManager.default().isAvailableAllFunction() == false {
  816. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  817. return
  818. }
  819. if collectionView.selectionIndexPaths.count < 1 {
  820. _ = 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))
  821. return
  822. }
  823. rotatePages(indexPaths: collectionView.selectionIndexPaths, rotateAngle: 90)
  824. }
  825. public func deletePageAction() {
  826. if IAPProductsManager.default().isAvailableAllFunction() == false {
  827. KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
  828. return
  829. }
  830. let selectedIndexPaths = collectionView.selectionIndexPaths
  831. if selectedIndexPaths.count < 1 {
  832. _ = 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))
  833. return
  834. } else if selectedIndexPaths.count == (showDocument?.pageCount ?? 0) {
  835. _ = 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))
  836. return
  837. }
  838. deletePages(indexpaths: selectedIndexPaths)
  839. }
  840. // MARK: - private
  841. private func fetchProgressBlockParamsIsPasswordFile(params: Any...) -> Bool {
  842. if (params.count <= 2) {
  843. return false
  844. }
  845. return true
  846. }
  847. private func fetchProgressBlockParamsForFileUrl(params: Any...) -> URL? {
  848. if (params.count < 2) {
  849. return nil
  850. }
  851. return params[1] as? URL
  852. }
  853. private func km_open_file_multi(type: KMPasswordInputWindowType = .open, progressBlock: ((_ index: Int, _ params: Any...)->Void)? = nil, completionBlock:@escaping ([CPDFDocument])->Void) {
  854. NSPanel.km_open_multi_success(self.view.window!) { panel in
  855. var array: [String] = []
  856. for fileType in KMConvertPDFManager.supportFileType() {
  857. array.append(fileType)
  858. }
  859. panel.allowedFileTypes = KMNConvertTool.pdfExtensions + array
  860. } completion: { urls in
  861. self.km_add_file_multi(fileUrls: urls, type: type, progressBlock: progressBlock, completionBlock: completionBlock)
  862. }
  863. }
  864. private func km_add_file_multi(fileUrls: [URL] ,type: KMPasswordInputWindowType = .open, progressBlock: ((_ index: Int, _ params: Any...)->Void)? = nil, completionBlock:@escaping ([CPDFDocument])->Void) {
  865. var pdfUrls: [URL] = []
  866. var imageUrls: [URL] = []
  867. var officeUrls: [URL] = []
  868. for url in fileUrls {
  869. let type = url.pathExtension.lowercased()
  870. if (KMNConvertTool.isPDFType(type)) {
  871. pdfUrls.append(url)
  872. }
  873. if (KMNConvertTool.isImageType(type)) {
  874. imageUrls.append(url)
  875. }
  876. if (KMNConvertTool.isOfficeType(type)) {
  877. officeUrls.append(url)
  878. }
  879. }
  880. if (officeUrls.count == 0) {
  881. self.km_add_pdf_multi(fileUrls: pdfUrls, type: type, progressBlock: progressBlock) { documents in
  882. var index = documents.count
  883. var _documents: [CPDFDocument] = []
  884. for imageUrl in imageUrls {
  885. index += 1
  886. let document = CPDFDocument()
  887. let image = NSImage(contentsOfFile: imageUrl.path)
  888. let _ = document?.km_insertPage(image?.size ?? .zero, withImage: imageUrl.path, at: 0)
  889. _documents.append(document!)
  890. if let _callback = progressBlock { // 回调进度
  891. _callback(index, document as Any, imageUrl)
  892. }
  893. }
  894. completionBlock(documents + _documents)
  895. }
  896. return
  897. }
  898. self.km_add_office_multi(fileUrls: officeUrls) { [unowned self] fileUrlStrings in
  899. var officeDocuments: [CPDFDocument] = []
  900. var index = 0
  901. for fileUrlString in fileUrlStrings {
  902. index += 1
  903. let document = CPDFDocument(url: URL(fileURLWithPath: fileUrlString))
  904. officeDocuments.append(document!)
  905. if let _callback = progressBlock { // 回调进度
  906. _callback(index, document as Any, URL(fileURLWithPath: fileUrlString))
  907. }
  908. }
  909. self.km_add_pdf_multi(fileUrls: pdfUrls) { documents in
  910. var index = documents.count + officeDocuments.count
  911. var _documents: [CPDFDocument] = []
  912. for imageUrl in imageUrls {
  913. index += 1
  914. let document = CPDFDocument()
  915. let image = NSImage(contentsOfFile: imageUrl.path)
  916. let _ = document?.km_insertPage(image!.size, withImage: imageUrl.path, at: 0)
  917. _documents.append(document!)
  918. if let _callback = progressBlock { // 回调进度
  919. _callback(index, document as Any, imageUrl)
  920. }
  921. }
  922. completionBlock(officeDocuments + documents + _documents)
  923. }
  924. }
  925. }
  926. private func km_add_pdf_multi(fileUrls: [URL] ,type: KMPasswordInputWindowType = .open, progressBlock: ((_ index: Int, _ params: Any...)->Void)? = nil, completionBlock:@escaping ([CPDFDocument])->Void) {
  927. var results: [CPDFDocument] = []
  928. self.lockedFiles.removeAll()
  929. var index = 0
  930. for url in fileUrls {
  931. let document = CPDFDocument(url: url)
  932. if (document!.isLocked) {
  933. self.lockedFiles.append(url)
  934. continue
  935. }
  936. if let _document = document {
  937. results.append(_document)
  938. }
  939. index += 1
  940. if let _callback = progressBlock {
  941. _callback(index, ((document != nil) ? document : CPDFDocument()) as Any, url)
  942. }
  943. }
  944. if (self.lockedFiles.count == 0) {
  945. completionBlock(results)
  946. return
  947. }
  948. self._openPasswordWindow_loop(fileUrl: self.lockedFiles.first!, type: type) { params in
  949. index += 1
  950. if (params.count <= 2) { // 参数错误
  951. if let _callback = progressBlock { // 回调进度
  952. _callback(index)
  953. }
  954. return
  955. }
  956. let fileUrl = params[0] as! URL
  957. let result = params[1] as! KMPasswordInputWindowResult
  958. let password = params[2] as? String
  959. if (result == .cancel) {
  960. if let _callback = progressBlock { // 回调进度
  961. _callback(index, CPDFDocument() as Any, fileUrl, result)
  962. }
  963. return
  964. }
  965. let document = CPDFDocument(url: fileUrl)
  966. if let _password = password { // 将文档进行解密
  967. document?.unlock(withPassword: _password)
  968. }
  969. if let _callback = progressBlock { // 回调进度
  970. _callback(index, document as Any, fileUrl, result, password as Any)
  971. }
  972. // 将文档加入返回数据
  973. if let _document = document {
  974. results.append(_document)
  975. }
  976. } completionBlock: {
  977. completionBlock(results)
  978. }
  979. }
  980. private func km_add_office_multi(fileUrls: [URL], completionBlock:@escaping ([String])->Void) -> Void {
  981. var fileUrlStrings: [String] = []
  982. let dispatchGroup = Dispatch.DispatchGroup()
  983. for (index, fileUrl) in fileUrls.enumerated() {
  984. let filePath = fileUrl.path
  985. let folderPath = "convertToPDF_\(index).pdf"
  986. let savePath: String? = folderPath.kUrlToPDFFolderPath() as String
  987. if (savePath == nil) {
  988. continue
  989. }
  990. dispatchGroup.enter()
  991. KMConvertPDFManager.convertFile(filePath, savePath: savePath!) { success, errorDic in
  992. if errorDic != nil || !success || !FileManager.default.fileExists(atPath: savePath!) {
  993. dispatchGroup.leave()
  994. if FileManager.default.fileExists(atPath: savePath!) {
  995. try?FileManager.default.removeItem(atPath: savePath!)
  996. }
  997. let alert = NSAlert.init()
  998. alert.alertStyle = .critical
  999. var infoString = ""
  1000. if errorDic != nil {
  1001. for key in (errorDic! as Dictionary).keys {
  1002. infoString = infoString.appendingFormat("%@\n", errorDic![key] as! CVarArg)
  1003. }
  1004. }
  1005. alert.informativeText = KMLocalizedString("Please install Microsoft Office to create PDFs from Office files")
  1006. alert.messageText = KMLocalizedString("Failed to Create PDF", comment: "")
  1007. alert.addButton(withTitle: KMLocalizedString("OK"))
  1008. alert.runModal()
  1009. return
  1010. }
  1011. if !savePath!.isPDFValid() {
  1012. dispatchGroup.leave()
  1013. let alert = NSAlert()
  1014. alert.alertStyle = .critical
  1015. alert.messageText = KMLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.")
  1016. alert.runModal()
  1017. return
  1018. }
  1019. fileUrlStrings.append(savePath!)
  1020. dispatchGroup.leave()
  1021. }
  1022. }
  1023. dispatchGroup.notify(queue: DispatchQueue.main) {
  1024. completionBlock(fileUrlStrings)
  1025. }
  1026. }
  1027. fileprivate func _openPasswordWindow_loop(fileUrl: URL, type: KMPasswordInputWindowType, progressBlock: ((_ params: Any...)->Void)?, completionBlock:@escaping ()->Void) {
  1028. KMPasswordInputWindow.openWindow(window: self.view.window!, type: type, url: fileUrl) { [weak self] result, password in
  1029. // 将结果返回
  1030. if let _callback = progressBlock {
  1031. _callback(fileUrl, result, password as Any)
  1032. }
  1033. // 进行下一个
  1034. self?.lockedFiles.removeFirst()
  1035. if let _fileUrl = self?.lockedFiles.first {
  1036. self?._openPasswordWindow_loop(fileUrl: _fileUrl, type: type, progressBlock: progressBlock, completionBlock: completionBlock)
  1037. } else {
  1038. completionBlock()
  1039. }
  1040. }
  1041. }
  1042. }