|
@@ -22,6 +22,10 @@ class KMNHomeViewController: NSViewController {
|
|
|
|
|
|
var demoVC: WCCompWindowController = WCCompWindowController(windowNibName: "WCCompWindowController")
|
|
|
|
|
|
+
|
|
|
+ //合并
|
|
|
+ var mergeWindowController: KMMergeWindowController?
|
|
|
+
|
|
|
override func viewDidLoad() {
|
|
|
super.viewDidLoad()
|
|
|
// Do view setup here.
|
|
@@ -163,7 +167,8 @@ extension KMNHomeViewController: KMHomeRightViewDelegate {
|
|
|
|
|
|
//点击快捷工具列表中的某一项
|
|
|
func homeRightViewDidQuickToolsItemClicked(_ view: KMHomeRightView, _ toolType: HomeQuickToolType) {
|
|
|
- print("HomeQuickToolType = %d", toolType.rawValue)
|
|
|
+ self.quickToolsActionWith(toolType)
|
|
|
+
|
|
|
}
|
|
|
|
|
|
//最近文件列表删除更新结束后回调
|
|
@@ -191,6 +196,15 @@ extension KMNHomeViewController: KMHomeDragViewDelegate {
|
|
|
self.openFile(withFilePath: filePath)
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ func homeDragView(_ viewController: KMHomeDragView, notSupport: Bool) {
|
|
|
+ if notSupport {
|
|
|
+ let alert = NSAlert()
|
|
|
+ alert.alertStyle = .critical
|
|
|
+ alert.messageText = KMLocalizedString("This file format is not supported. Please enter PDF, picture, or Office file")
|
|
|
+ alert.runModal()
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//MARK: - Open Files
|
|
@@ -688,3 +702,637 @@ extension KMNHomeViewController {
|
|
|
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+//MARK: - Quick Tools Action
|
|
|
+extension KMNHomeViewController {
|
|
|
+ func quickToolsActionWith(_ type: HomeQuickToolType) {
|
|
|
+ switch type {
|
|
|
+ case .Batch:
|
|
|
+ fastTool_Batch()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .MergePDF:
|
|
|
+ fastTool_MergePDF()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .ImageToPDF:
|
|
|
+ imageToPDFAction()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .OCR:
|
|
|
+ fastTool_OCR()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .ConvertPDF:
|
|
|
+ fastTool_ConvertPDF()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .PDFToWord:
|
|
|
+ fastTool_ConvertPDF()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .PDFToExcel:
|
|
|
+ fastTool_PDFToExcel()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .Compression:
|
|
|
+ fastTool_Compression()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .PDFToPPT:
|
|
|
+ fastTool_PDFToPPT()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .Security:
|
|
|
+ fastTool_Security()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .FileCompare:
|
|
|
+ fastTool_FileCompare()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .Watermark:
|
|
|
+ fastTool_Watermark()
|
|
|
+ break
|
|
|
+
|
|
|
+ case .Insert:
|
|
|
+ fastTool_Insert()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .Extract:
|
|
|
+ fastTool_Extract()
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .DigitalSignature:
|
|
|
+
|
|
|
+ break
|
|
|
+
|
|
|
+ case .Print:
|
|
|
+ fastTool_Print()
|
|
|
+
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func imageToPDFAction() {
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+ openPanel.allowedFileTypes = KMImageToPDFMethod.supportedImageTypes()
|
|
|
+ //MARK: 允许多选还是单选,如果是付费用户允许多选
|
|
|
+ openPanel.allowsMultipleSelection = true
|
|
|
+ openPanel.message = KMLocalizedString("Select images to create a new document. To select multiple files press cmd ⌘ button on keyboard and click on the target files one by one.")
|
|
|
+ if IAPProductsManager.default().isAvailableAllFunction(){
|
|
|
+ openPanel.allowsMultipleSelection = true
|
|
|
+ } else {
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
+ }
|
|
|
+
|
|
|
+ openPanel.beginSheetModal(for: NSWindow.currentWindow()) {[weak self] result in
|
|
|
+ if result == NSApplication.ModalResponse.OK {
|
|
|
+ guard let weakSelf = self else { return }
|
|
|
+ let urls = openPanel.urls as [URL]
|
|
|
+
|
|
|
+ weakSelf.openImageToPdfWindow(urls: urls)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func fastTool_Batch() { // Batch
|
|
|
+ if !IAPProductsManager.default().isAvailableAllFunction(){
|
|
|
+ KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ let batchWindowController = KMBatchOperateWindowController.sharedWindowController
|
|
|
+ batchWindowController.window?.makeKeyAndOrderFront("")
|
|
|
+ }
|
|
|
+
|
|
|
+ func fastTool_MergePDF() { // MergePDF
|
|
|
+ mergeWindowController = KMMergeWindowController(windowNibName: "KMMergeWindowController")
|
|
|
+ mergeWindowController!.type = .merge
|
|
|
+ mergeWindowController!.cancelAction = { [unowned self] controller in
|
|
|
+ self.view.window?.endSheet((self.mergeWindowController!.window)!)
|
|
|
+ }
|
|
|
+ view.window?.beginSheet(mergeWindowController!.window!)
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ func fastTool_OCR() { // OCR
|
|
|
+ if !IAPProductsManager.default().isAvailableAllFunction(){
|
|
|
+ let winC = KMPurchaseCompareWindowController.sharedInstance()
|
|
|
+ winC?.showWindow(nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+
|
|
|
+ var arr = KMImageToPDFMethod.supportedImageTypes()
|
|
|
+ arr.append("pdf")
|
|
|
+ openPanel.allowedFileTypes = arr
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
+
|
|
|
+ openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
|
|
|
+ if result == NSApplication.ModalResponse.OK {
|
|
|
+ quickOcr(urls: openPanel.urls)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func quickOcr(urls: Array<URL>) {
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
|
|
|
+ var document: CPDFDocument?
|
|
|
+ for fileURL in urls {
|
|
|
+ if fileURL.lastPathComponent.pathExtension == "pdf"{
|
|
|
+ document = CPDFDocument(url: fileURL)
|
|
|
+ }else{
|
|
|
+ document = CPDFDocument()
|
|
|
+ let image = NSImage(contentsOfFile: fileURL.path)
|
|
|
+ _ = document?.km_insertPage(image!.size, withImage: fileURL.path, at: 0)
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ if document == nil{
|
|
|
+ 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.beginSheetModal(for: NSApp.mainWindow!) { (response) in
|
|
|
+ if response == .alertFirstButtonReturn {
|
|
|
+ // Handle cancel action
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+ let com = KMOCRPDFWindowController(cpdfDocument: document!, pwd: document!.password)
|
|
|
+ self.km_beginSheet(windowC: com)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func fastTool_ConvertPDF() { // 转换PDF
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+ var arr = KMImageToPDFMethod.supportedImageTypes()
|
|
|
+ arr.append("pdf")
|
|
|
+ openPanel.allowedFileTypes = ["pdf","PDF"]
|
|
|
+ //MARK: 允许多选还是单选,如果是付费用户允许多选
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
+
|
|
|
+ openPanel.beginSheetModal(for: self.view.window!) { [self] (result) in
|
|
|
+ if result == NSApplication.ModalResponse.OK {
|
|
|
+ quickConvertPdf(urls: openPanel.urls, type: .WordAdvance)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func quickConvertPdf(urls: Array<URL>, type: KMConvertWithPDFType) {
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
|
|
|
+ var arr: Array<KMBatchOperateFile> = Array()
|
|
|
+ for fileURL in urls {
|
|
|
+ let img = NSImage(contentsOfFile: fileURL.path)
|
|
|
+ if self.isDamageImage(image: img, path: fileURL.path) {
|
|
|
+ let alert = NSAlert()
|
|
|
+ alert.alertStyle = .critical
|
|
|
+ alert.messageText = String(format: KMLocalizedString("The file \"%@\" could not be opened."), fileURL.path.lastPathComponent)
|
|
|
+ alert.informativeText = KMLocalizedString("It may be damaged or use a file format that PDF Reader Pro doesn’t recognize.")
|
|
|
+ alert.addButton(withTitle: KMLocalizedString("Cancel"))
|
|
|
+ alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
|
|
|
+ if response == .alertFirstButtonReturn {
|
|
|
+ // Handle cancel action
|
|
|
+ }
|
|
|
+ }
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ let file = KMBatchOperateFile(filePath: fileURL.path, type: .Convert)
|
|
|
+ arr.append(file)
|
|
|
+ }
|
|
|
+ let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
|
|
|
+ if #available(macOS 10.13, *) {
|
|
|
+ baseWindowController.window?.makeKeyAndOrderFront(nil)
|
|
|
+ } else {
|
|
|
+ baseWindowController.showWindow(nil)
|
|
|
+ }
|
|
|
+ if arr.count > 0 {
|
|
|
+ baseWindowController.checkNeedPasswordSwitchToOperateType(operateType: .Convert, files: arr)
|
|
|
+ }
|
|
|
+ baseWindowController.switchToConvertType(convertType: type)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func fastTool_PDFToPPT() {
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+ openPanel.allowedFileTypes = ["pdf","PDF"]
|
|
|
+ //MARK: 允许多选还是单选,如果是付费用户允许多选
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
+
|
|
|
+ openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
|
|
|
+ if result == NSApplication.ModalResponse.OK {
|
|
|
+ quickConvertPdf(urls: openPanel.urls, type: .PowerPoint)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func fastTool_PDFToExcel() {
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+ openPanel.allowedFileTypes = ["pdf","PDF"]
|
|
|
+ //MARK: 允许多选还是单选,如果是付费用户允许多选
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
+
|
|
|
+ openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
|
|
|
+ if result == NSApplication.ModalResponse.OK {
|
|
|
+ quickConvertPdf(urls: openPanel.urls, type: .Excel)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 压缩
|
|
|
+ func fastTool_Compression() {
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+ openPanel.allowedFileTypes = ["pdf","PDF"]
|
|
|
+ //MARK: 允许多选还是单选,如果是付费用户允许多选
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
+
|
|
|
+ openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
|
|
|
+ if result == NSApplication.ModalResponse.OK {
|
|
|
+ quickCompressPdf(urls: openPanel.urls)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func quickCompressPdf(urls: Array<URL>) {
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
|
|
|
+ var arr: Array<KMBatchOperateFile> = Array()
|
|
|
+ for fileURL in urls {
|
|
|
+ let file = KMBatchOperateFile(filePath: fileURL.path, type: .Compress)
|
|
|
+ arr.append(file)
|
|
|
+ }
|
|
|
+ let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
|
|
|
+ if #available(macOS 10.13, *) {
|
|
|
+ baseWindowController.window?.makeKeyAndOrderFront(nil)
|
|
|
+ } else {
|
|
|
+ baseWindowController.showWindow(nil)
|
|
|
+ }
|
|
|
+ if arr.count > 0 {
|
|
|
+ baseWindowController.checkNeedPasswordSwitchToOperateType(operateType: .Compress, files: arr)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 安全
|
|
|
+ func fastTool_Security() {
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+ openPanel.allowedFileTypes = ["pdf","PDF"]
|
|
|
+ //MARK: 允许多选还是单选,如果是付费用户允许多选
|
|
|
+ openPanel.allowsMultipleSelection = true
|
|
|
+ openPanel.message = KMLocalizedString("To select multiple files press cmd ⌘ button on keyboard and click on the target files one by one.")
|
|
|
+ openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
|
|
|
+ if result == NSApplication.ModalResponse.OK {
|
|
|
+ quickPassword(urls: openPanel.urls)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func quickPassword(urls: Array<URL>) {
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
|
|
|
+ var arr: Array<KMBatchOperateFile> = Array()
|
|
|
+ for fileURL in urls {
|
|
|
+ let file = KMBatchOperateFile(filePath: fileURL.path, type: KMBatchQuickActionManager.defaultManager.actionType == .add ? .AddPassword : .RemovePassword)
|
|
|
+ arr.append(file)
|
|
|
+ }
|
|
|
+ let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
|
|
|
+ if #available(macOS 10.13, *) {
|
|
|
+ baseWindowController.window?.makeKeyAndOrderFront(nil)
|
|
|
+ } else {
|
|
|
+ baseWindowController.showWindow(nil)
|
|
|
+ }
|
|
|
+ if arr.count > 0 {
|
|
|
+ baseWindowController.checkNeedPasswordSwitchToOperateType(operateType: KMBatchQuickActionManager.defaultManager.actionType == .add ? .AddPassword : .RemovePassword, files: arr)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func fastTool_FileCompare() { // 文件对比
|
|
|
+ if !IAPProductsManager.default().isAvailableAllFunction(){
|
|
|
+ let winC = KMPurchaseCompareWindowController.sharedInstance()
|
|
|
+ winC?.showWindow(nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
+ openPanel.allowedFileTypes = ["pdf"]
|
|
|
+ openPanel.beginSheetModal(for: NSApp.mainWindow!) { result in
|
|
|
+ if result == .cancel {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if !openPanel.url!.path.isPDFValid() {
|
|
|
+ let alert = NSAlert()
|
|
|
+ alert.alertStyle = .critical
|
|
|
+ alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
|
|
|
+ alert.runModal()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ KMBaseWindowController.checkPassword(url: URL(fileURLWithPath: openPanel.url!.path), type: .owner) { [unowned self] success, resultPassword in
|
|
|
+ if success {
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
|
|
|
+ let controller = KMCompareWindowController(windowNibName: "KMCompareWindowController")
|
|
|
+
|
|
|
+ controller.password = resultPassword
|
|
|
+ controller.filePath = openPanel.url!.path
|
|
|
+
|
|
|
+ controller.cancelAction = { [unowned self] contr in
|
|
|
+ self.view.window?.endSheet((controller.window)!)
|
|
|
+ }
|
|
|
+
|
|
|
+ controller.contentComplete = { [unowned self] controller, pdfCompareContent, result, oldDocument, document in
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ self.view.window?.endSheet((controller.window)!)
|
|
|
+
|
|
|
+// self.openContentCompareVC(with: pdfCompareContent, results: result, oldDocument: oldDocument, document: document)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ controller.coveringComplete = { [unowned self] controller, document in
|
|
|
+ self.view.window?.endSheet((controller.window)!)
|
|
|
+
|
|
|
+// self.openCoveringCompareVC(with: document)
|
|
|
+ }
|
|
|
+ controller.fileType = .content
|
|
|
+
|
|
|
+ NSWindow.currentWindow().beginSheet(controller.window!)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func fastTool_Watermark() { // 水印
|
|
|
+ if !IAPProductsManager.default().isAvailableAllFunction(){
|
|
|
+ let winC = KMPurchaseCompareWindowController.sharedInstance()
|
|
|
+ winC?.showWindow(nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+ openPanel.allowedFileTypes = ["pdf","PDF"]
|
|
|
+ //MARK: 允许多选还是单选,如果是付费用户允许多选
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
+ openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
|
|
|
+ if result == NSApplication.ModalResponse.OK {
|
|
|
+ quickWaterMark(urls: openPanel.urls)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func quickWaterMark(urls: Array<URL>) {
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
|
|
|
+ var arr: Array<KMBatchOperateFile> = Array()
|
|
|
+ for fileURL in urls {
|
|
|
+ let pdf = CPDFDocument(url: fileURL)
|
|
|
+ if !pdf!.allowsPrinting || !pdf!.allowsCopying {
|
|
|
+ let alert = NSAlert()
|
|
|
+ alert.alertStyle = .critical
|
|
|
+ alert.messageText = NSLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
|
|
|
+ alert.runModal()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let file = KMBatchOperateFile(filePath: fileURL.path, type: KMBatchQuickActionManager.defaultManager.actionType == .add ? .AddWatermark : .RemoveWatermark)
|
|
|
+ arr.append(file)
|
|
|
+ }
|
|
|
+ let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
|
|
|
+ if #available(macOS 10.13, *) {
|
|
|
+ baseWindowController.window?.makeKeyAndOrderFront(nil)
|
|
|
+ } else {
|
|
|
+ baseWindowController.showWindow(nil)
|
|
|
+ }
|
|
|
+ if arr.count > 0 {
|
|
|
+ baseWindowController.checkNeedPasswordSwitchToOperateType(operateType: KMBatchQuickActionManager.defaultManager.actionType == .add ? .AddWatermark : .RemoveWatermark, files: arr)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func fastTool_Background() { // 背景
|
|
|
+ if !IAPProductsManager.default().isAvailableAllFunction(){
|
|
|
+ let winC = KMPurchaseCompareWindowController.sharedInstance()
|
|
|
+ winC?.showWindow(nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+ openPanel.allowedFileTypes = ["pdf","PDF"]
|
|
|
+ //MARK: 允许多选还是单选,如果是付费用户允许多选
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
+ openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
|
|
|
+ if result == NSApplication.ModalResponse.OK {
|
|
|
+ quickBackgroudMark(urls: openPanel.urls)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func quickBackgroudMark(urls: Array<URL>) {
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
|
|
|
+ var arr: Array<KMBatchOperateFile> = Array()
|
|
|
+ for fileURL in urls {
|
|
|
+ let pdf = CPDFDocument(url: fileURL)
|
|
|
+ if !pdf!.allowsPrinting || !pdf!.allowsCopying {
|
|
|
+ let alert = NSAlert()
|
|
|
+ alert.alertStyle = .critical
|
|
|
+ alert.messageText = NSLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
|
|
|
+ alert.runModal()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let file = KMBatchOperateFile(filePath: fileURL.path, type: KMBatchQuickActionManager.defaultManager.actionType == .add ? .AddBackground : .RemoveBackground)
|
|
|
+ arr.append(file)
|
|
|
+ }
|
|
|
+ let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
|
|
|
+ if #available(macOS 10.13, *) {
|
|
|
+ baseWindowController.window?.makeKeyAndOrderFront(nil)
|
|
|
+ } else {
|
|
|
+ baseWindowController.showWindow(nil)
|
|
|
+ }
|
|
|
+ if arr.count > 0 {
|
|
|
+ baseWindowController.checkNeedPasswordSwitchToOperateType(operateType: KMBatchQuickActionManager.defaultManager.actionType == .add ? .AddBackground : .RemoveBackground, files: arr)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ func fastTool_Print() { // 打印
|
|
|
+ KMPrintWindowController.openFiles(window: self.view.window!)
|
|
|
+ }
|
|
|
+
|
|
|
+ func fastTool_Insert() { // 插入
|
|
|
+ if !IAPProductsManager.default().isAvailableAllFunction(){
|
|
|
+ KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+ openPanel.prompt = NSLocalizedString("Insert", comment: "")
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
+ openPanel.allowedFileTypes = ["pdf"]
|
|
|
+
|
|
|
+ openPanel.beginSheetModal(for: NSApp.mainWindow!) { result in
|
|
|
+ if result == .OK {
|
|
|
+
|
|
|
+ let windowC = KMPDFInsertWindowController(fileURL: openPanel.url!)
|
|
|
+
|
|
|
+ windowC.callback = { [weak self] idx, params in
|
|
|
+ if params.count >= 4 {
|
|
|
+ if let doc = params.first as? CPDFDocument {
|
|
|
+ self?.savePDFDocument(doc, password: params[1] as? String ?? "")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ self?.km_endSheet()
|
|
|
+ }
|
|
|
+ self.km_beginSheet(windowC: windowC)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func savePDFDocument(_ pdf: CPDFDocument, password: String) -> Void {
|
|
|
+ DispatchQueue.global(qos: .`default`).async {
|
|
|
+ var isSuccessfully = false
|
|
|
+ if pdf.isEncrypted {
|
|
|
+ let dic = [
|
|
|
+ CPDFDocumentWriteOption.userPasswordOption : password,
|
|
|
+ CPDFDocumentWriteOption.ownerPasswordOption : password
|
|
|
+ ]
|
|
|
+ isSuccessfully = pdf.write(to: pdf.documentURL, withOptions: dic)
|
|
|
+ } else {
|
|
|
+ isSuccessfully = pdf.write(to: pdf.documentURL)
|
|
|
+ }
|
|
|
+ if !isSuccessfully {
|
|
|
+ if let data = pdf.dataRepresentation() {
|
|
|
+ isSuccessfully = NSData(data: data).write(to: pdf.documentURL, atomically: true)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DispatchQueue.main.sync {
|
|
|
+ if isSuccessfully {
|
|
|
+ let workspace = NSWorkspace.shared
|
|
|
+ let url = URL(fileURLWithPath: pdf.documentURL?.path ?? "")
|
|
|
+ workspace.activateFileViewerSelecting([url])
|
|
|
+ } else {
|
|
|
+ let alert = NSAlert()
|
|
|
+ alert.alertStyle = .critical
|
|
|
+ alert.messageText = NSLocalizedString("Failed to insert page(s)!", comment: "")
|
|
|
+ alert.runModal()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func fastTool_Extract() { // 提取
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+ openPanel.prompt = "提取"
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
+ openPanel.allowedFileTypes = ["pdf"]
|
|
|
+ openPanel.beginSheetModal(for: NSApp.mainWindow!) { result in
|
|
|
+ if result == .OK {
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
|
|
|
+ let insertWindowController: KMPDFInsertPageWindow = KMPDFInsertPageWindow.init(documentPath: openPanel.url!, toolType: .Extract)
|
|
|
+ insertWindowController.beginSheetExtractModal(for: self.view.window!) { pdfDocument, pages, oneDocumentPerPage, isDeletePage in
|
|
|
+ self.extractPageAction(pdfDocument, pages, oneDocumentPerPage, isDeletePage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func extractPageAction(_ pdfDocument: CPDFDocument, _ pages: [CPDFPage], _ oneDocumentPerPage: Bool, _ isDeletePage: Bool) -> Void {
|
|
|
+
|
|
|
+ if pages.count < 1 {
|
|
|
+ let alert = NSAlert()
|
|
|
+ alert.alertStyle = .critical
|
|
|
+ alert.messageText = NSLocalizedString("Please select two or more pages first to organize.", comment: "")
|
|
|
+ alert.runModal()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if !oneDocumentPerPage {
|
|
|
+ let fileName = pdfDocument.getFileNameAccordingSelctPages(pages)
|
|
|
+ let outputSavePanel = NSSavePanel()
|
|
|
+ outputSavePanel.allowedFileTypes = ["pdf"]
|
|
|
+ outputSavePanel.nameFieldStringValue = fileName
|
|
|
+
|
|
|
+ outputSavePanel.beginSheetModal(for: self.view.window!) { result in
|
|
|
+ if result == .OK {
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
+ let saveFilePath = outputSavePanel.url?.path
|
|
|
+ DispatchQueue.global().async {
|
|
|
+ var pdf = CPDFDocument.init()
|
|
|
+ let success = (pdf!.extractAsOneDocument(withPages: pages, savePath: saveFilePath)) as Bool
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ if success {
|
|
|
+ let workspace = NSWorkspace.shared
|
|
|
+ let url = URL(fileURLWithPath: saveFilePath!)
|
|
|
+ workspace.activateFileViewerSelecting([url])
|
|
|
+
|
|
|
+ if isDeletePage {
|
|
|
+ for page in pages {
|
|
|
+ let indexPage = pdfDocument.index(for: page)
|
|
|
+ pdfDocument.removePage(at: indexPage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ let panel = NSOpenPanel()
|
|
|
+ panel.canChooseFiles = false
|
|
|
+ panel.canChooseDirectories = true
|
|
|
+ panel.canCreateDirectories = true
|
|
|
+ panel.allowsMultipleSelection = false
|
|
|
+ panel.beginSheetModal(for: self.view.window!) { result in
|
|
|
+ if result == .OK {
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
+ let outputURL = panel.url
|
|
|
+ DispatchQueue.global().async {
|
|
|
+ let folderName = String(pdfDocument.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)
|
|
|
+
|
|
|
+ let successArray = pdfDocument.extractPerPageDocument(withPages: pages, folerPath: filePath)
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ if successArray!.count > 0 {
|
|
|
+ NSWorkspace.shared.activateFileViewerSelecting(successArray!)
|
|
|
+
|
|
|
+ if !isDeletePage {
|
|
|
+ for page in pages {
|
|
|
+ let indexPage = pdfDocument.index(for: page)
|
|
|
+ pdfDocument.removePage(at: indexPage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|