1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264 |
- //
- // KMMainViewController+Action.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2022/12/15.
- //
- import Foundation
- import KMComponentLibrary
- extension KMNThumbnailBaseViewController {
-
- private func insertImageFilePath(imagePath:String,pageDex:Int)->Bool{
- var isSuccessFul:Bool = false
- if (FileManager.default.fileExists(atPath: imagePath)) {
- if let image = NSImage(contentsOfFile: imagePath) {
- isSuccessFul = showDocument?.km_insertPage(image.size, withImage: imagePath, at: UInt(pageDex)) == true
- }
- }
- return isSuccessFul
- }
-
- private func undoDeleteIndexPaths(deleteIndexPath: Set<IndexPath>) {
- var changeIndex:IndexSet = []
- var deletePages:[CPDFPage] = []
-
- let pageIndexs = KMNTools.indexpathsToIndexs(indexpaths: deleteIndexPath)
- for i in pageIndexs {
- if let page = showDocument?.page(at: UInt(i)) {
- deletePages.append(page)
- changeIndex.insert(i)
- }
- }
-
- showDocument?.removePage(at: pageIndexs)
- refreshDatas()
- collectionView.reloadData()
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoInsertPages(insertPages: deletePages, indexs: pageIndexs)
- }
- thumbnailBaseViewDelegate?.thumbnailViewControlleHaveChange?(pageEditVC: self)
- }
-
- private func undoInsertPages(insertPages:[CPDFPage],indexs: IndexSet) {
- var indexpaths = Set<IndexPath>()
- var count: Int = 0
- for index in indexs {
- guard let pageCount = showDocument?.pageCount, index <= pageCount, count < insertPages.count, index != -1 else {
- KMPrint("index invalid. index: \(index)")
- break
- }
-
- showDocument?.insertPageObject(insertPages[count], at: UInt(index))
- indexpaths.insert(IndexPath(item: index, section: 0))
- count += 1
- }
-
- refreshDatas()
- collectionView.reloadData()
- collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
- collectionView.selectionIndexPaths = indexpaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.deletePages(indexpaths: indexpaths)
- }
- thumbnailBaseViewDelegate?.thumbnailViewControlleHaveChange?(pageEditVC: self)
- }
-
- private func undoReplacePages(of targetIndexpaths: Set<IndexPath>, with documents: [CPDFDocument],insertIndexSet:IndexSet,orgPages:[CPDFPage],orgIndexs: IndexSet) {
-
- showDocument?.removePage(at: insertIndexSet)
-
- var indexpaths = Set<IndexPath>()
- var count: Int = 0
- for index in orgIndexs {
- guard let pageCount = showDocument?.pageCount, index <= pageCount, count < orgPages.count, index != -1 else {
- KMPrint("index invalid. index: \(index)")
- break
- }
-
- showDocument?.insertPageObject(orgPages[count], at: UInt(index))
- indexpaths.insert(IndexPath(item: index, section: 0))
- count += 1
- }
-
- refreshDatas()
- collectionView.reloadData()
- collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
- collectionView.selectionIndexPaths = indexpaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.replacePages(of: targetIndexpaths, with: documents)
- }
- thumbnailBaseViewDelegate?.thumbnailViewControlleHaveChange?(pageEditVC: self)
- }
-
- private func undoMovePages(movePages:[CPDFPage],destinationDex:Int,orgPages:[CPDFPage],orgPageDexs:[Int]) {
-
- for (i, page) in orgPages.enumerated() {
- let dragIndex = page.pageIndex()
- let index:UInt = UInt(orgPageDexs[i])
- showDocument?.movePage(at: dragIndex, withPageAt: index)
- }
-
- var indexpaths = Set<IndexPath>()
- for (_, page) in movePages.enumerated() {
- let index = page.pageIndex()
- indexpaths.insert(IndexPath(item: Int(index), section: 0))
- }
- refreshDatas()
- collectionView.reloadData()
- collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
- collectionView.selectionIndexPaths = indexpaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.movePages(dragPages: movePages, destinationDex: destinationDex)
- }
- thumbnailBaseViewDelegate?.thumbnailViewControlleHaveChange?(pageEditVC: self)
- }
-
- func insertFileComplete(newSelectIndexs: Set<IndexPath>){
- refreshDatas()
- collectionView.reloadData()
-
- collectionView.selectionIndexPaths = newSelectIndexs
-
- if newSelectIndexs.isEmpty { return }
-
- let firstIndexPath = newSelectIndexs.first
- collectionView.scrollToItems(at: [firstIndexPath ?? IndexPath(item: 0, section: 0)], scrollPosition: .top)
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoDeleteIndexPaths(deleteIndexPath: newSelectIndexs)
- }
-
- thumbnailBaseViewDelegate?.thumbnailViewControlleHaveChange?(pageEditVC: self)
- }
-
- public func insertFormPages(insertPages: [CPDFPage],pageDex:Int) {
- let pageIndexDex: Int = pageDex
- var indexpaths = Set<IndexPath>()
-
- let page = insertPages.first
- if page?.document == showDocument {
- for (i, page) in insertPages.enumerated() {
- let isSuccessFul:Bool = showDocument?.insertPageObject(page, at: UInt(pageIndexDex+i)) == true
- if(isSuccessFul == true) {
- indexpaths.insert(IndexPath(item: pageIndexDex+i, section: 0))
- }
- }
- } else {
- if let pasteDocument = page?.document {
- var indexs: IndexSet = IndexSet()
- for (i, page) in insertPages.enumerated() {
- indexs.insert(Int(pasteDocument.index(for: page)))
- indexpaths.insert(IndexPath(item: pageIndexDex+i, section: 0))
- }
-
- let isSuccessFul:Bool = showDocument?.importPages(indexs, from: pasteDocument, at: UInt(pageIndexDex)) == true
- if(isSuccessFul == false){
- return
- }
- }
- }
-
- refreshDatas()
- collectionView.reloadData()
- collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
- collectionView.selectionIndexPaths = indexpaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoDeleteIndexPaths(deleteIndexPath: indexpaths)
- }
- thumbnailBaseViewDelegate?.thumbnailViewControlleHaveChange?(pageEditVC: self)
- }
-
- public func insertBlankSize(pageSize: CGSize,pageDex:Int) {
- var indexpaths = Set<IndexPath>()
- let isSuccessFul = showDocument?.insertBlankPage(pageSize: pageSize, at: pageDex)
- if(isSuccessFul == true) {
- indexpaths.insert(IndexPath(item: pageDex, section: 0))
- }
-
- refreshDatas()
- collectionView.reloadData()
- collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
- collectionView.selectionIndexPaths = indexpaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoDeleteIndexPaths(deleteIndexPath: indexpaths)
- }
- thumbnailBaseViewDelegate?.thumbnailViewControlleHaveChange?(pageEditVC: self)
- }
-
- public func rotatePages(indexPaths: Set<IndexPath>, rotateAngle: Int) {
- var tIndexPaths: Set<IndexPath> = []
- tIndexPaths = indexPaths
- for targetIndexPath in indexPaths {
- if let page = showDocument?.page(at: UInt(targetIndexPath.item)) {
- var pageRotate = page.rotation + rotateAngle
- if(pageRotate == -90) {
- pageRotate = 270
- } else if (pageRotate == 450) {
- pageRotate = 90
-
- }
- page.rotation = pageRotate
-
- let cellView = collectionView.item(at: targetIndexPath) as? KMNThumbnailCollectionViewItem
- if(cellView != nil) {
- cellView?.thumbnailMode.removeCacheImage()
- }
- }
- }
-
- reloadDatas() // Ensure correct type conversion
- collectionView.selectionIndexPaths = tIndexPaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.rotatePages(indexPaths: tIndexPaths, rotateAngle: -rotateAngle)
- }
- thumbnailBaseViewDelegate?.thumbnailViewControlleHaveChange?(pageEditVC: self)
- }
-
- public func reversePages(indexs: IndexSet) {
- if let doc = showDocument {
- var theIdxs = indexs
- var res = false
- for _ in 0 ..< indexs.count {
- guard let first = theIdxs.first else {
- break
- }
- guard let last = theIdxs.last else {
- break
- }
- if first == last {
- break
- }
- res = doc.exchangePage(at: UInt(first), withPageAt: UInt(last))
- if res {
- theIdxs.remove(first)
- theIdxs.remove(last)
-
- }
- }
- if res {
- refreshDatas()
- let selected_indexpaths = KMNTools.indexsToIndexpaths(indexs: indexs)
-
- collectionView.reloadItems(at: selected_indexpaths)
-
- collectionView.selectionIndexPaths = selected_indexpaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.reversePages(indexs: indexs)
- }
-
- thumbnailBaseViewDelegate?.thumbnailViewControlleHaveChange?(pageEditVC: self)
- }
- }
- }
-
- public func replacePages(of targetIndexpaths: Set<IndexPath>, with documents: [CPDFDocument]) {
- if (targetIndexpaths.count == 0 || documents.count == 0) {
- KMPrint("replace invalid.")
- return
- }
- var index = targetIndexpaths.sorted().first!.item
-
- var deletePages:[CPDFPage] = []
-
- let indexSet = KMNTools.indexpathsToIndexs(indexpaths: targetIndexpaths)
- for i in indexSet {
- if let page = showDocument?.page(at: UInt(i)) {
- deletePages.append(page)
- }
- }
- showDocument?.removePage(at: indexSet)
-
- var tIndexPaths: Set<IndexPath> = []
- for document in documents {
- for i in 0 ..< document.pageCount {
- if let page = document.page(at: i) {
- showDocument?.insertPageObject(page, at: UInt(index))
- tIndexPaths.insert(IndexPath(item: index, section: 0))
- index += 1
- }
- }
- thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: document)
- }
-
- refreshDatas()
- collectionView.reloadData()
- collectionView.selectionIndexPaths = tIndexPaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoReplacePages(of: targetIndexpaths, with: documents, insertIndexSet: KMNTools.indexpathsToIndexs(indexpaths: tIndexPaths), orgPages: deletePages,orgIndexs: indexSet)
- }
-
- thumbnailBaseViewDelegate?.thumbnailViewControlleHaveChange?(pageEditVC: self)
- }
-
- public func deletePages(indexpaths:Set<IndexPath>) {
- var changeIndex:IndexSet = []
- var deletePages:[CPDFPage] = []
-
- let pageIndexs = KMNTools.indexpathsToIndexs(indexpaths: indexpaths)
- for i in pageIndexs {
- if let page = showDocument?.page(at: UInt(i)) {
- deletePages.append(page)
- changeIndex.insert(i)
- }
- }
- showDocument?.removePage(at: changeIndex)
- collectionView.selectionIndexPaths = []
- refreshDatas()
- collectionView.reloadData()
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoInsertPages(insertPages: deletePages, indexs: pageIndexs)
- }
- thumbnailBaseViewDelegate?.thumbnailViewControlleHaveChange?(pageEditVC: self)
- }
-
- public func movePages(dragPages:[CPDFPage],destinationDex:Int) {
- guard !dragPages.isEmpty else { return }
-
- var destinationIndex = destinationDex
- let maxDragPageIndex = dragPages.last?.pageIndex() ?? 0
- let minDragPageIndex = dragPages.first?.pageIndex() ?? 0
-
- let rangeStart = min(Int(minDragPageIndex), destinationIndex)
- let rangeEnd = max(Int(maxDragPageIndex), destinationIndex)
-
- var changePages:[CPDFPage] = []
- var changePageIndexs:[Int] = []
- for index in (rangeStart...rangeEnd) {
- if let changePage = showDocument?.page(at: UInt(index)) {
- changePages.append(changePage)
- changePageIndexs.append(index)
- }
- }
- for dragPage in dragPages {
- let dragIndex = dragPage.pageIndex()
- if destinationIndex > dragIndex {
- destinationIndex -= 1
- showDocument?.movePage(at: dragIndex, withPageAt: UInt(destinationIndex))
- } else {
- showDocument?.movePage(at: dragIndex, withPageAt: UInt(destinationIndex))
- }
- destinationIndex += 1
- }
-
- var selectIndexPaths: Set<IndexPath> = []
-
- for dragPage in dragPages {
- let dragIndex = dragPage.pageIndex()
- selectIndexPaths.insert(IndexPath(item: Int(dragIndex), section: 0))
- }
-
- refreshDatas()
- collectionView.reloadData()
- collectionView.selectionIndexPaths = selectIndexPaths
- if selectIndexPaths.isEmpty { return }
- let firstIndexPath = selectIndexPaths.first
- collectionView.scrollToItems(at: [firstIndexPath ?? IndexPath(item: 0, section: 0)], scrollPosition: .top)
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoMovePages(movePages: dragPages, destinationDex: destinationDex, orgPages: changePages, orgPageDexs: changePageIndexs)
- }
- thumbnailBaseViewDelegate?.thumbnailViewControlleHaveChange?(pageEditVC: self)
- }
-
- public func insertFromFilePath(fileNames:[String],formDex:Int,indexDex:UInt,selectIndexs:Set<IndexPath>,completionBlock:@escaping (Set<IndexPath>)->Void)-> Void {
- let path = fileNames[formDex]
- var insertDex = indexDex
- var tSelectIndex = selectIndexs
-
- let pathExtension = URL(fileURLWithPath: path).pathExtension.lowercased()
- if pathExtension == "pdf", let pdf = CPDFDocument(url: URL(fileURLWithPath: path)) {
- if pdf.isLocked {
- NSWindowController.checkPassword(url: pdf.documentURL, type: .owner) { success, resultPassword in
- if (resultPassword.isEmpty == false) {
- pdf.unlock(withPassword: resultPassword)
-
- for i in 0 ..< pdf.pageCount {
- let insertPage = pdf.page(at: i)
- self.showDocument?.insertPageObject(insertPage, at: insertDex)
- tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
- insertDex += 1
- }
-
- var tFormDex = formDex
- tFormDex += 1
- self.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: pdf)
- if(tFormDex < fileNames.count) {
- return self.insertFromFilePath(fileNames: fileNames, formDex: tFormDex, indexDex: indexDex, selectIndexs: tSelectIndex,completionBlock: completionBlock)
- } else {
- self.insertFileComplete(newSelectIndexs: tSelectIndex)
- return completionBlock(tSelectIndex)
- }
- }
- }
- } else {
- for i in 0 ..< pdf.pageCount {
- let insertPage = pdf.page(at: i)
- showDocument?.insertPageObject(insertPage ?? CPDFPage(), at: insertDex)
- tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
- insertDex += 1
- }
- var tFormDex = formDex
- tFormDex += 1
- thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: pdf)
- if(tFormDex < fileNames.count) {
- return insertFromFilePath(fileNames: fileNames, formDex: tFormDex, indexDex: indexDex, selectIndexs: tSelectIndex,completionBlock: completionBlock)
- } else {
- insertFileComplete(newSelectIndexs: tSelectIndex)
- return completionBlock(tSelectIndex)
- }
-
- }
- } else if supportDragFileTypes().contains(pathExtension) {
- if KMConvertPDFManager.supportImages().contains(pathExtension) {
- let isSueccessFul = insertImageFilePath(imagePath: path, pageDex: Int(indexDex))
- if(isSueccessFul) {
- tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
- insertDex += 1
- }
-
- var tFormDex = formDex
- tFormDex += 1
- if(tFormDex < fileNames.count) {
- return insertFromFilePath(fileNames: fileNames, formDex: tFormDex, indexDex: indexDex, selectIndexs: tSelectIndex,completionBlock: completionBlock)
- } else {
- insertFileComplete(newSelectIndexs: tSelectIndex)
- return completionBlock(tSelectIndex)
- }
-
- } else {
- KMNConvertTool.convertOffice(filePath: path) { convertPDFPath in
- if (convertPDFPath != nil) {
- let pathExtension = URL(fileURLWithPath: convertPDFPath!).pathExtension.lowercased()
- if pathExtension == "pdf", let pdf = CPDFDocument(url: URL(fileURLWithPath: convertPDFPath!)) {
- for i in 0 ..< pdf.pageCount {
- let insertPage = pdf.page(at: i)
- self.showDocument?.insertPageObject(insertPage, at: insertDex)
- tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
- insertDex += 1
- }
- self.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: pdf)
-
- var tFormDex = formDex
- tFormDex += 1
- if(tFormDex < fileNames.count) {
- return self.insertFromFilePath(fileNames: fileNames, formDex: tFormDex, indexDex: indexDex, selectIndexs: tSelectIndex,completionBlock: completionBlock)
- } else {
- self.insertFileComplete(newSelectIndexs: tSelectIndex)
- return completionBlock(tSelectIndex)
- }
-
- }
- }
- }
- }
- }
- }
-
- public func extractPages(indexpaths: Set<IndexPath>, oneDocumentPerPage: Bool, callback: @escaping KMResultBlock) {
- let pageIndexs = KMNTools.indexpathsToIndexs(indexpaths: indexpaths)
- let oneDocument = !oneDocumentPerPage
- let document = self.showDocument!
- /// 提取的页面
- var extractPages: Array<CPDFPage> = []
- for i in pageIndexs {
- guard let page = document.page(at: UInt(i)) else {
- continue
- }
- extractPages.append(page)
- }
-
- if (oneDocument) { /// 提取为一个文档
- var fileName = document.documentURL.deletingPathExtension().lastPathComponent
- fileName.append(" pages ")
- fileName.append(KMNTools.newParseSelectedIndexs(selectedIndex: pageIndexs.sorted()))
- fileName.append(".pdf")
- NSPanel.savePanel(self.view.window!, true) { panel in
- panel.nameFieldStringValue = fileName
- } completion: { response, url, isOpen in
- if (response != .OK) {
- callback(.cancel)
- return
- }
-
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
- DispatchQueue.global().async {
- var success = false
- let pdf = CPDFDocument.init()
- success = pdf!.extractAsOneDocument(withPages: extractPages, savePath: url!.path)
- DispatchQueue.main.async {
- if (success == false) {
- callback(.failure)
- return
- }
-
- if (isOpen == false) {
- NSWorkspace.shared.activateFileViewerSelecting([url!])
- } else {
- NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
- }
- }
- callback(.success, [url!])
- }
- }
- }
- }
- } else {
- let panel = NSOpenPanel()
- panel.canChooseFiles = false
- panel.canChooseDirectories = true
- panel.canCreateDirectories = true
- panel.allowsMultipleSelection = false
- panel.beginSheetModal(for: self.view.window!) { response in
- if response != .OK {
- callback(.cancel)
- return
- }
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
- let outputURL = panel.url
- DispatchQueue.global().async {
- let folderName = String((document.documentURL.lastPathComponent.split(separator: ".")[0])) + "_extract"
-
- var filePath = URL(fileURLWithPath: outputURL!.path).appendingPathComponent(folderName).path
- var i = 1
- let testFilePath = filePath
- while FileManager.default.fileExists(atPath: filePath) {
- filePath = testFilePath + "\(i)"
- i += 1
- }
-
- try? FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false, attributes: nil)
-
- var successArray: [URL]?
- successArray = document.extractPerPageDocument(withPages: extractPages, folerPath: filePath)
-
- DispatchQueue.main.async {
- if successArray!.count == 0 {
- callback(.failure)
- return
- }
- NSWorkspace.shared.activateFileViewerSelecting(successArray!)
- callback(.success, successArray ?? NSURL())
- }
- }
- }
- }
- }
- }
-
- // MARK: - MenuItem
- @objc public func copyMenuItemAciton() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- winC?.showWindow(nil)
- return
- }
- KMNThumbnailManager.manager.copyPages = []
-
- let indexpaths = collectionView.selectionIndexPaths
- for indexpath in indexpaths.sorted() {
- guard let page = showDocument?.page(at: UInt(indexpath.item))?.copy() as? CPDFPage else {
- continue
- }
-
- KMNThumbnailManager.manager.copyDocument.append(showDocument ?? CPDFDocument())
- KMNThumbnailManager.manager.copyPages.append(page)
- }
- }
-
- @objc public func pastMenuItemAciton(menuitemProperty:ComponentMenuitemProperty?) {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- winC?.showWindow(nil)
- return
- }
-
- var pastIndex = 1
- let point = menuitemProperty?.representedObject as? NSPoint
-
- let selectedIndexPaths = collectionView.selectionIndexPaths
- if(selectedIndexPaths.count > 0) {
- let maxmumIndexPath = selectedIndexPaths.max(by: { $0 < $1 })
- pastIndex = ((maxmumIndexPath?.item ?? 0) + 1)
- } else {
- if(point != nil) {
- let pointInCollectionView = collectionView.convert(point!, from: nil)
-
- let visibleItems = collectionView.visibleItems()
- let mouseX = pointInCollectionView.x
- let mouseY = pointInCollectionView.y
-
- // 获取当前行的所有 cell
- let currentRowItems = visibleItems.filter { item in
- if let indexPath = collectionView.indexPath(for: item),
- let cellAttributes = collectionView.layoutAttributesForItem(at: indexPath) {
- return cellAttributes.frame.minY <= mouseY && cellAttributes.frame.maxY >= mouseY
- }
- return false
- }
- if(mouseX < 24) { //点击区域在最左边
- // 找到最近右边的 cell
- let rightMostCell = currentRowItems.compactMap { collectionView.indexPath(for: $0) }
- .filter { indexPath in
- let cellRect = collectionView.layoutAttributesForItem(at: indexPath)?.frame
- return cellRect?.minX ?? 0 > mouseX // 只选择右侧的 cell
- }
- .sorted { ($0.item < $1.item) } // 按 item 的顺序排列
- .first // 选择第一个,即最右边的 cell
- pastIndex = (rightMostCell?.item ?? (Int(showDocument?.pageCount ?? 1)))
- } else {
- // 找到最近右边的 cell
- let leftMostCell = currentRowItems.compactMap { collectionView.indexPath(for: $0) }
- .filter { indexPath in
- let cellRect = collectionView.layoutAttributesForItem(at: indexPath)?.frame
- return cellRect?.maxX ?? 0 < mouseX // 只选择左侧的 cell
- }
- .sorted { $0.item > $1.item } // 按 item 的逆序排列,以选择最近的左侧 cell
- .first // 选择第一个,即最近的左边的 cell
-
- pastIndex = (leftMostCell?.item ?? (Int(showDocument?.pageCount ?? 1)) - 1) + 1
- }
- } else {
- pastIndex = Int(showDocument?.pageCount ?? 0)
- }
- }
- let copyPages = KMNThumbnailManager.manager.copyPages
-
- insertFormPages(insertPages: copyPages, pageDex: pastIndex)
- }
-
- @objc public func cutMenuItemAciton() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- winC?.showWindow(nil)
- return
- }
-
- KMNThumbnailManager.manager.copyPages = []
- let indexpaths = collectionView.selectionIndexPaths
- for indexpath in indexpaths.sorted() {
- guard let page = showDocument?.page(at: UInt(indexpath.item))?.copy() as? CPDFPage else {
- continue
- }
-
- KMNThumbnailManager.manager.copyDocument.append(showDocument ?? CPDFDocument())
- KMNThumbnailManager.manager.copyPages.append(page)
- }
-
- deletePages(indexpaths: indexpaths)
- }
-
- @objc public func deleteMenuItemAciton() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- winC?.showWindow(nil)
- return
- }
- let indexpaths = collectionView.selectionIndexPaths
- deletePages(indexpaths: indexpaths)
- }
-
- @objc func sharePageItemAction(menuItem:NSMenuItem) {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- winC?.showWindow(nil)
- return
- }
- let indexpaths = collectionView.selectionIndexPaths
-
- let doucument = showDocument
- let filename : String = doucument?.documentURL.lastPathComponent ?? ""
- let folderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(filename)) ?? ""
-
- try? FileManager.default.removeItem(atPath: folderPath)
-
- let pdfdocument = CPDFDocument()
- let ret = pdfdocument?.importPages(KMNTools.indexpathsToIndexs(indexpaths: indexpaths), from: doucument, at: 0) ?? false
- let url = URL(fileURLWithPath: folderPath)
- if ret {
- let success = pdfdocument?.write(toFile: folderPath)
- let represent = menuItem.representedObject as? NSSharingService
- represent?.perform(withItems: [url])
- }
- }
-
- @objc public func displayPageSizeAction() {
- let indexpaths = collectionView.selectionIndexPaths
- isShowPageSize = !isShowPageSize
- collectionView.selectionIndexPaths = indexpaths
- }
-
- private func insertFilePath(filePath:String,pdfPassword:String?) {
- var selectedIndexPaths = collectionView.selectionIndexPaths
- if(selectedIndexPaths.count <= 0) {
- selectedIndexPaths = pdfviewSelectionIndexPaths
- }
- let insertPDF = KMNPDFInsertPDFWindowController(showDocument, filePath: filePath, password: pdfPassword, selectionIndexPaths: selectedIndexPaths)
- insertPDF.pdfCallback = { [weak self] fileAttribute, insertIdx in
- let doc = fileAttribute.pdfDocument
-
- var insertPages: [CPDFPage] = []
-
- for number in fileAttribute.fetchSelectPages() {
- if let page = doc?.page(at: UInt(number-1)) {
- insertPages.append(page)
- }
- }
- self?.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: doc)
-
- self?.insertFormPages(insertPages: insertPages, pageDex: insertIdx)
- }
- insertPDF.fileCallback = { [weak self] filePath, insertIdx in
- self?.insertFromFilePath(fileNames: [filePath], formDex: 0, indexDex: UInt(insertIdx), selectIndexs: [], completionBlock: { newSelectIndexs in
- })
- }
-
- insertPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
- }
-
- // MARK: - public
- func updateThumnailItem(updateIndexPaths: Set<IndexPath>,isSelect:Bool) {
-
- for targetIndexPath in updateIndexPaths {
- let cellView = collectionView.item(at: targetIndexPath) as? KMNThumbnailCollectionViewItem
- if(cellView != nil) {
- cellView?.thumbnailMode.removeCacheImage()
- }
- }
-
- collectionView.reloadItems(at: updateIndexPaths)
- if(isSelect) {
- if updateIndexPaths.isEmpty { return }
- let firstIndexPath = updateIndexPaths.first
- collectionView.scrollToItems(at: [firstIndexPath ?? IndexPath(item: 0, section: 0)], scrollPosition: .top)
-
- collectionView.selectionIndexPaths = updateIndexPaths
- }
- }
-
- func updateAllThumnailItems() {
- KMNThumbnailManager.clearCacheFilePath(filePath: showDocument?.documentURL.path ?? "")
-
- collectionView.reloadData()
- }
-
- @objc public func insertFromPDFAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- let openPanel = NSOpenPanel()
- openPanel.allowedFileTypes = supportDragFileTypes()
- openPanel.allowsMultipleSelection = false
- openPanel.beginSheetModal(for: NSWindow.currentWindow()) {[weak self] result in
- if result == NSApplication.ModalResponse.OK {
- let fileURL = openPanel.url
- if(fileURL?.pathExtension == "pdf") {
- let pdfDoc = CPDFDocument(url: fileURL)
- if let data = pdfDoc?.isLocked, data {
- DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
- NSWindowController.checkPassword(url: fileURL ?? NSURL.fileURL(withPath: ""), type: .owner) { result, pwd in
- if (pwd.isEmpty == false) {
- self?.insertFilePath(filePath: fileURL?.path ?? "", pdfPassword: pwd)
- }
- }
- }
- } else {
- self?.insertFilePath(filePath: fileURL?.path ?? "", pdfPassword: nil)
- }
- } else {
- self?.insertFilePath(filePath: fileURL?.path ?? "", pdfPassword: nil)
- }
- }
- }
- }
-
- @objc public func insertFromBlankAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- var selectedIndexPaths = collectionView.selectionIndexPaths
- if(selectedIndexPaths.count <= 0) {
- selectedIndexPaths = pdfviewSelectionIndexPaths
- }
-
- let insertPDF = KMNPDFInsertBlankWindowController(self.showDocument, selectionIndexPaths: selectedIndexPaths)
- insertPDF.callback = { [weak self] pageSize, insertIdx in
- self?.insertBlankSize(pageSize: pageSize, pageDex: insertIdx)
- }
- insertPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
- }
-
- public func insertFromClipboardAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
- var selectedIndexPaths = collectionView.selectionIndexPaths
- if(selectedIndexPaths.count <= 0) {
- selectedIndexPaths = pdfviewSelectionIndexPaths
- }
- var maxmumIndex = 1
- if(selectedIndexPaths.count > 0) {
- let maxmumIndexPath = selectedIndexPaths.max(by: { $0 < $1 })
- maxmumIndex = (maxmumIndexPath?.item ?? 0) + 1
- }
-
- var error: NSError?
- guard let document: CPDFDocument = KMNConvertTool.openDocumentWithImageFromPasteboard(NSPasteboard.general, error: &error) else {
- return
- }
- var insetPages:[CPDFPage] = []
- for i in 0 ..< document.pageCount {
- let page: CPDFPage = (document.page(at: i))
- insetPages.append(page)
- }
- if insetPages.count > 0 {
- insertFormPages(insertPages: insetPages, pageDex: maxmumIndex)
- }
-
- self.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: document)
- let ips: Set<IndexPath> = [IndexPath(item: maxmumIndex, section: 0)]
-
- refreshDatas()
- collectionView.selectionIndexPaths = ips
- collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
- }
-
- public func insertFromScannerAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- let selectedIndexPaths = collectionView.selectionIndexPaths
- let maxmumIndexPath = selectedIndexPaths.max(by: { $0 < $1 })
-
- let vc = KMDeviceBrowserWindowController.shared
- vc.type = .scanner
- vc.importScannerFileCallback = { [weak self] (url: NSURL) -> Void in
- if let imag = NSImage(contentsOfFile: url.path! ) {
- let index = (maxmumIndexPath?.item ?? 0) + 1
- _ = self?.showDocument?.km_insertPage(imag.size, withImage: url.path! , at:UInt(index))
-
- self?.reloadDatas()
-
- let ips: Set<IndexPath> = [IndexPath(item: index, section: 0)]
-
- self?.collectionView.selectionIndexPaths = ips
- self?.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
-
- vc.close()
- }
- }
- vc.showWindow(nil)
- vc.window?.center()
- }
-
-
- public func canUndo()->Bool {
- return true
- }
-
- public func undoPDFAction() {
- currentUndoManager?.undo()
- }
-
- public func canRodo()->Bool {
- return true
- }
-
- public func redoPDFAction() {
- currentUndoManager?.redo()
- }
-
- @objc public func extractPDFAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- let selectedIndexPaths = collectionView.selectionIndexPaths
-
- if selectedIndexPaths.count < 1 {
- _ = 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))
- return
- }
-
- let extractPDF = KMNExtractPDFWindowController(self.showDocument, selectionIndexPaths: collectionView.selectionIndexPaths)
- extractPDF.callback = { [weak self] oneDocumentPerPage, isDeletePage in
- extractPDF.own_closeEndSheet()
- if let _ = self?.showDocument {
- self?.extractPages(indexpaths: selectedIndexPaths, oneDocumentPerPage: oneDocumentPerPage, callback: { [weak self] result, params in
- if (result == .failure || result == .cancel) {
- return
- }
- if (isDeletePage) {
- self?.deletePages(indexpaths: selectedIndexPaths)
- }
- })
- }
- }
- extractPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
- }
-
- @objc public func replacePDFAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- let selectedIndexPaths = collectionView.selectionIndexPaths
-
- if selectedIndexPaths.count < 1 {
- _ = 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))
- return
- }
-
- self.km_open_file_multi { [unowned self] index, params in
- if (self.fetchProgressBlockParamsIsPasswordFile(params: params)) { // 加密文档进度回调
- return
- }
-
- let tFileUrl = self.fetchProgressBlockParamsForFileUrl(params: params)
- let pdfExtensions = KMNConvertTool.supportPDFFileType()
- if let exn = tFileUrl?.pathExtension, pdfExtensions.contains(exn) {
- if (tFileUrl!.path.isPDFValid() == false) {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = KMLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.")
- alert.runModal()
- }
- }
- } completionBlock: { [unowned self] documents in
- self.replacePages(of: selectedIndexPaths, with: documents)
- }
- }
-
- public func splitPDFAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- let selectedIndexPaths = collectionView.selectionIndexPaths
-
- if collectionView.selectionIndexPaths.count < 1 {
- _ = 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))
- return
- }
-
- let splitPDF = KMNSplitPDFWindowController(self.showDocument,selectionIndexPaths: selectedIndexPaths)
- splitPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
- }
-
- public func reversePDFAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
- let selectedIndexPaths = collectionView.selectionIndexPaths
-
- if selectedIndexPaths.count < 2 {
- _ = 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))
- return
- }
- reversePages(indexs:KMNTools.indexpathsToIndexs(indexpaths: selectedIndexPaths))
- }
-
- @objc public func rotatePageLeftAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- if collectionView.selectionIndexPaths.count < 1 {
- _ = 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))
- return
- }
- rotatePages(indexPaths: collectionView.selectionIndexPaths, rotateAngle: -90)
- }
-
- @objc public func rotatePageRightAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- if collectionView.selectionIndexPaths.count < 1 {
- _ = 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))
- return
- }
- rotatePages(indexPaths: collectionView.selectionIndexPaths, rotateAngle: 90)
- }
-
- public func deletePageAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- let selectedIndexPaths = collectionView.selectionIndexPaths
-
- if selectedIndexPaths.count < 1 {
- _ = 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))
- return
- } else if selectedIndexPaths.count == (showDocument?.pageCount ?? 0) {
- _ = 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))
- return
-
- }
-
- deletePages(indexpaths: selectedIndexPaths)
- }
-
- // MARK: - private
- private func fetchProgressBlockParamsIsPasswordFile(params: Any...) -> Bool {
- if (params.count <= 2) {
- return false
- }
- return true
- }
-
- private func fetchProgressBlockParamsForFileUrl(params: Any...) -> URL? {
- if (params.count < 2) {
- return nil
- }
- return params[1] as? URL
- }
-
- private func km_open_file_multi(type: KMPasswordInputWindowType = .owner, progressBlock: ((_ index: Int, _ params: Any...)->Void)? = nil, completionBlock:@escaping ([CPDFDocument])->Void) {
- NSPanel.km_open_multi_success(self.view.window!) { panel in
- var array: [String] = []
- for fileType in KMConvertPDFManager.supportFileType() {
- array.append(fileType)
- }
- panel.allowedFileTypes = KMNConvertTool.pdfExtensions + array
- } completion: { urls in
- self.km_add_file_multi(fileUrls: urls, type: type, progressBlock: progressBlock, completionBlock: completionBlock)
- }
- }
-
- private func km_add_file_multi(fileUrls: [URL] ,type: KMPasswordInputWindowType = .open, progressBlock: ((_ index: Int, _ params: Any...)->Void)? = nil, completionBlock:@escaping ([CPDFDocument])->Void) {
- var pdfUrls: [URL] = []
- var imageUrls: [URL] = []
- var officeUrls: [URL] = []
- for url in fileUrls {
- let type = url.pathExtension.lowercased()
- if (KMNConvertTool.isPDFType(type)) {
- pdfUrls.append(url)
- }
- if (KMNConvertTool.isImageType(type)) {
- imageUrls.append(url)
- }
- if (KMNConvertTool.isOfficeType(type)) {
- officeUrls.append(url)
- }
- }
-
- if (officeUrls.count == 0) {
- self.km_add_pdf_multi(fileUrls: pdfUrls, type: type, progressBlock: progressBlock) { documents in
- var index = documents.count
- var _documents: [CPDFDocument] = []
- for imageUrl in imageUrls {
- index += 1
- let document = CPDFDocument()
- let image = NSImage(contentsOfFile: imageUrl.path)
- let _ = document?.km_insertPage(image?.size ?? .zero, withImage: imageUrl.path, at: 0)
- _documents.append(document!)
-
- if let _callback = progressBlock { // 回调进度
- _callback(index, document as Any, imageUrl)
- }
- }
-
- completionBlock(documents + _documents)
- }
- return
- }
-
- self.km_add_office_multi(fileUrls: officeUrls) { [unowned self] fileUrlStrings in
- var officeDocuments: [CPDFDocument] = []
- var index = 0
- for fileUrlString in fileUrlStrings {
- index += 1
- let document = CPDFDocument(url: URL(fileURLWithPath: fileUrlString))
- officeDocuments.append(document!)
-
- if let _callback = progressBlock { // 回调进度
- _callback(index, document as Any, URL(fileURLWithPath: fileUrlString))
- }
- }
-
- self.km_add_pdf_multi(fileUrls: pdfUrls) { documents in
- var index = documents.count + officeDocuments.count
- var _documents: [CPDFDocument] = []
- for imageUrl in imageUrls {
- index += 1
- let document = CPDFDocument()
- let image = NSImage(contentsOfFile: imageUrl.path)
- let _ = document?.km_insertPage(image!.size, withImage: imageUrl.path, at: 0)
- _documents.append(document!)
-
- if let _callback = progressBlock { // 回调进度
- _callback(index, document as Any, imageUrl)
- }
- }
-
- completionBlock(officeDocuments + documents + _documents)
- }
- }
- }
-
- private func km_add_pdf_multi(fileUrls: [URL] ,type: KMPasswordInputWindowType = .open, progressBlock: ((_ index: Int, _ params: Any...)->Void)? = nil, completionBlock:@escaping ([CPDFDocument])->Void) {
- var results: [CPDFDocument] = []
-
- self.lockedFiles.removeAll()
- var index = 0
- for url in fileUrls {
- let document = CPDFDocument(url: url)
- if (document!.isLocked) {
- self.lockedFiles.append(url)
- continue
- }
-
- if let _document = document {
- results.append(_document)
- }
-
- index += 1
- if let _callback = progressBlock {
- _callback(index, ((document != nil) ? document : CPDFDocument()) as Any, url)
- }
- }
-
- if (self.lockedFiles.count == 0) {
- completionBlock(results)
- return
- }
-
- self._openPasswordWindow_loop(fileUrl: self.lockedFiles.first!, type: type) { params in
- index += 1
- if (params.count <= 2) { // 参数错误
- if let _callback = progressBlock { // 回调进度
- _callback(index)
- }
- return
- }
-
- let fileUrl = params[0] as! URL
- let result = params[1] as! KMPasswordInputWindowResult
- let password = params[2] as? String
- if (result == .cancel) {
- if let _callback = progressBlock { // 回调进度
- _callback(index, CPDFDocument() as Any, fileUrl, result)
- }
- return
- }
-
- let document = CPDFDocument(url: fileUrl)
- if let _password = password { // 将文档进行解密
- document?.unlock(withPassword: _password)
- }
- if let _callback = progressBlock { // 回调进度
- _callback(index, document as Any, fileUrl, result, password as Any)
- }
- // 将文档加入返回数据
- if let _document = document {
- results.append(_document)
- }
- } completionBlock: {
- completionBlock(results)
- }
- }
-
- private func km_add_office_multi(fileUrls: [URL], completionBlock:@escaping ([String])->Void) -> Void {
- var fileUrlStrings: [String] = []
-
- let dispatchGroup = Dispatch.DispatchGroup()
- for (index, fileUrl) in fileUrls.enumerated() {
- let filePath = fileUrl.path
- let folderPath = "convertToPDF_\(index).pdf"
- let savePath: String? = folderPath.kUrlToPDFFolderPath() as String
- if (savePath == nil) {
- continue
- }
-
- dispatchGroup.enter()
- KMConvertPDFManager.convertFile(filePath, savePath: savePath!) { success, errorDic in
- if errorDic != nil || !success || !FileManager.default.fileExists(atPath: savePath!) {
- dispatchGroup.leave()
-
- if FileManager.default.fileExists(atPath: savePath!) {
- try?FileManager.default.removeItem(atPath: savePath!)
- }
- let alert = NSAlert.init()
- alert.alertStyle = .critical
- var infoString = ""
- if errorDic != nil {
- for key in (errorDic! as Dictionary).keys {
- infoString = infoString.appendingFormat("%@\n", errorDic![key] as! CVarArg)
- }
- }
- alert.informativeText = KMLocalizedString("Please install Microsoft Office to create PDFs from Office files")
- alert.messageText = KMLocalizedString("Failed to Create PDF", comment: "")
- alert.addButton(withTitle: KMLocalizedString("OK"))
- alert.runModal()
- return
- }
-
- if !savePath!.isPDFValid() {
- dispatchGroup.leave()
-
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = KMLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.")
- alert.runModal()
- return
- }
-
- fileUrlStrings.append(savePath!)
- dispatchGroup.leave()
- }
- }
-
- dispatchGroup.notify(queue: DispatchQueue.main) {
- completionBlock(fileUrlStrings)
- }
- }
-
- fileprivate func _openPasswordWindow_loop(fileUrl: URL, type: KMPasswordInputWindowType, progressBlock: ((_ params: Any...)->Void)?, completionBlock:@escaping ()->Void) {
- KMPasswordInputWindow.openWindow(window: self.view.window!, type: type, url: fileUrl) { [weak self] result, password in
- // 将结果返回
- if let _callback = progressBlock {
- _callback(fileUrl, result, password as Any)
- }
-
- // 进行下一个
- self?.lockedFiles.removeFirst()
- if let _fileUrl = self?.lockedFiles.first {
- self?._openPasswordWindow_loop(fileUrl: _fileUrl, type: type, progressBlock: progressBlock, completionBlock: completionBlock)
- } else {
- completionBlock()
- }
- }
- }
- }
|