KMNThumbnailBaseViewController+Action.swift 54 KB

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