|
@@ -7,6 +7,7 @@
|
|
|
|
|
|
|
|
|
|
typealias KMPDFEditViewControllerSelectionDidChange = (_ selectedIndexs: Set<IndexPath>) -> ()
|
|
typealias KMPDFEditViewControllerSelectionDidChange = (_ selectedIndexs: Set<IndexPath>) -> ()
|
|
|
|
+typealias KMIndexChooseBlock = (_ index: Int) -> Void
|
|
|
|
|
|
class KMPDFEditViewController: KMPDFThumbViewBaseController {
|
|
class KMPDFEditViewController: KMPDFThumbViewBaseController {
|
|
|
|
|
|
@@ -177,6 +178,12 @@ class KMPDFEditViewController: KMPDFThumbViewBaseController {
|
|
self.insertBlankPageAction()
|
|
self.insertBlankPageAction()
|
|
} else if (index == KMPageEditType.insert_custom_page) {
|
|
} else if (index == KMPageEditType.insert_custom_page) {
|
|
self.item_insertCustomPage(sender: nil)
|
|
self.item_insertCustomPage(sender: nil)
|
|
|
|
+ } else if (index == KMPageEditType.insert_fromImage) {
|
|
|
|
+ self.item_insertFromImage(sender: nil)
|
|
|
|
+ } else if (index == KMPageEditType.insert_fromClipboard) {
|
|
|
|
+ self.item_insertFromClipboard(sender: nil)
|
|
|
|
+ } else if (index == KMPageEditType.insert_fromScanner) {
|
|
|
|
+ self.item_insertFromScanner(sender: nil)
|
|
} else if (index == KMPageEditType.extract_selected_pages) {
|
|
} else if (index == KMPageEditType.extract_selected_pages) {
|
|
self.extractSelectPageItemAction()
|
|
self.extractSelectPageItemAction()
|
|
} else if (index == KMPageEditType.split_selected_pages) {
|
|
} else if (index == KMPageEditType.split_selected_pages) {
|
|
@@ -1174,6 +1181,151 @@ class KMPDFEditViewController: KMPDFThumbViewBaseController {
|
|
self.km_beginSheet(windowC: windowC)
|
|
self.km_beginSheet(windowC: windowC)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @objc func item_insertFromImage(sender: NSMenuItem?) {
|
|
|
|
+ if IAPProductsManager.default().isAvailableAllFunction() == false {
|
|
|
|
+ KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
|
+ openPanel.allowedFileTypes = KMImageToPDFMethod.supportedImageTypes()
|
|
|
|
+ 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.", nil)
|
|
|
|
+ openPanel.canChooseDirectories = false
|
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
|
+ openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
|
|
|
|
+ if result == NSApplication.ModalResponse.OK {
|
|
|
|
+ if let fileURL = openPanel.urls.first {
|
|
|
|
+// self.choosePositionWindow {[weak self] index in
|
|
|
|
+// /// 插入位置
|
|
|
|
+ if let imag = NSImage(contentsOfFile: fileURL.path ) {
|
|
|
|
+ let index = self.getInsertIndex() + 1
|
|
|
|
+ let document = CPDFDocument()
|
|
|
|
+ _ = document?.km_insertPage(imag.size, withImage: fileURL.path , at: 0)
|
|
|
|
+ if let page: CPDFPage = (document?.page(at: 0)) {
|
|
|
|
+ self.insertPages(pages: [page], at: IndexSet(integer: index))
|
|
|
|
+ }
|
|
|
|
+ let ips: Set<IndexPath> = [IndexPath(item: index, section: 0)]
|
|
|
|
+ self.thumbnailView.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
|
|
|
|
+ }
|
|
|
|
+// }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ func choosePositionWindow(completion: @escaping KMIndexChooseBlock) -> Void {
|
|
|
|
+// guard let doc = self.pdfDocument else {
|
|
|
|
+// NSSound.beep()
|
|
|
|
+// return
|
|
|
|
+// }
|
|
|
|
+// let windowC = KMFileInsertPositionWindowController(document: doc)
|
|
|
|
+// windowC.currentPage = self.getSelecteIndex() + 1
|
|
|
|
+// windowC.insertLocation = 3
|
|
|
|
+// windowC.callback = { [weak self] pdfDoc, _, insertI in
|
|
|
|
+// if insertI >= 0 {
|
|
|
|
+// if (completion != nil) {
|
|
|
|
+// completion(insertI);
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// self?.km_endSheet()
|
|
|
|
+// }
|
|
|
|
+// self.km_beginSheet(windowC: windowC)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @objc func item_insertFromClipboard(sender: NSMenuItem?) {
|
|
|
|
+ if IAPProductsManager.default().isAvailableAllFunction() == false {
|
|
|
|
+ KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+// self.choosePositionWindow {[weak self] index in
|
|
|
|
+ /// 插入位置
|
|
|
|
+ var error: NSError?
|
|
|
|
+ guard let document: CPDFDocument = self.openDocumentWithImageFromPasteboard(NSPasteboard.general, error: &error) else {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ let index = self.getInsertIndex() + 1
|
|
|
|
+ if let page: CPDFPage = (document.page(at: 0)) {
|
|
|
|
+ self.insertPages(pages: [page], at: IndexSet(integer: index))
|
|
|
|
+ }
|
|
|
|
+ let ips: Set<IndexPath> = [IndexPath(item: index, section: 0)]
|
|
|
|
+ self.thumbnailView.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
|
|
|
|
+// }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @objc func item_insertFromScanner(sender: NSMenuItem?) {
|
|
|
|
+ if IAPProductsManager.default().isAvailableAllFunction() == false {
|
|
|
|
+ KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let vc = KMDeviceBrowserWindowController.shared
|
|
|
|
+ vc.type = .scanner
|
|
|
|
+ vc.importScannerFileCallback = { [weak self](url: NSURL) -> Void in
|
|
|
|
+// self?.choosePositionWindow {[weak self] index in
|
|
|
|
+ /// 插入位置
|
|
|
|
+ if let imag = NSImage(contentsOfFile: url.path! ) {
|
|
|
|
+ let index = (self?.getInsertIndex())! + 1
|
|
|
|
+ let document = CPDFDocument()
|
|
|
|
+ _ = document?.km_insertPage(imag.size, withImage: url.path! , at: 0)
|
|
|
|
+ if let page: CPDFPage = (document?.page(at: 0)) {
|
|
|
|
+ self?.insertPages(pages: [page], at: IndexSet(integer: index))
|
|
|
|
+ }
|
|
|
|
+ let ips: Set<IndexPath> = [IndexPath(item: index, section: 0)]
|
|
|
|
+ self?.thumbnailView.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
|
|
|
|
+ }
|
|
|
|
+// }
|
|
|
|
+ vc.close()
|
|
|
|
+ }
|
|
|
|
+ vc.showWindow(NSApp.mainWindow)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ func openDocumentWithImageFromPasteboard(_ pboard: NSPasteboard, error outError: AutoreleasingUnsafeMutablePointer<NSError?>?) -> CPDFDocument? {
|
|
|
|
+ var document: CPDFDocument? = nil
|
|
|
|
+ var data: Data? = nil
|
|
|
|
+
|
|
|
|
+ if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.pdf.rawValue]) {
|
|
|
|
+ data = pboard.data(forType: NSPasteboard.PasteboardType.pdf)
|
|
|
|
+ } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.postScript.rawValue]) {
|
|
|
|
+ data = pboard.data(forType: NSPasteboard.PasteboardType.postScript)
|
|
|
|
+ } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.tiff.rawValue]) {
|
|
|
|
+ data = convertTIFFDataToPDF(pboard.data(forType: NSPasteboard.PasteboardType.tiff) ?? Data())
|
|
|
|
+ } else {
|
|
|
|
+ let images = pboard.readObjects(forClasses: [NSImage.self], options: [:])
|
|
|
|
+ let strings = pboard.readObjects(forClasses: [NSAttributedString.self], options: [:])
|
|
|
|
+ if images?.count ?? 0 > 0 {
|
|
|
|
+ data = convertTIFFDataToPDF((images![0] as AnyObject).tiffRepresentation!)
|
|
|
|
+ } else if strings?.count ?? 0 > 0 {
|
|
|
|
+ data = KMOCTool.convertStringsToPDF(withString: strings ?? [""]) // convertStringsToPDF(strings!)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if let data = data {
|
|
|
|
+ document = CPDFDocument(data: data)
|
|
|
|
+
|
|
|
|
+ } else if let outError = outError {
|
|
|
|
+ outError.pointee = NSError(domain: "SKDocumentErrorDomain", code: 3, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("Unable to load data from clipboard", comment: "Error description")])
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return document
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ func convertTIFFDataToPDF(_ tiffData: Data) -> Data? {
|
|
|
|
+ guard let imsrc = CGImageSourceCreateWithData(tiffData as CFData, [kCGImageSourceTypeIdentifierHint: kUTTypeTIFF] as CFDictionary), CGImageSourceGetCount(imsrc) > 0, let cgImage = CGImageSourceCreateImageAtIndex(imsrc, 0, nil) else { return nil }
|
|
|
|
+ let pdfData = NSMutableData(capacity: tiffData.count)
|
|
|
|
+ let consumer = CGDataConsumer(data: pdfData! as CFMutableData)!
|
|
|
|
+
|
|
|
|
+ var rect = CGRect(x: 0, y: 0, width: CGFloat(cgImage.width), height: CGFloat(cgImage.height))
|
|
|
|
+ let ctxt = CGContext(consumer: consumer, mediaBox: &rect, nil)
|
|
|
|
+ ctxt!.beginPDFPage(nil)
|
|
|
|
+ ctxt!.draw(cgImage, in: rect)
|
|
|
|
+ ctxt!.endPDFPage()
|
|
|
|
+ ctxt!.closePDF()
|
|
|
|
+
|
|
|
|
+ return pdfData as? Data
|
|
|
|
+ }
|
|
|
|
+
|
|
func extractSelectPageItemAction() {
|
|
func extractSelectPageItemAction() {
|
|
if IAPProductsManager.default().isAvailableAllFunction() == false {
|
|
if IAPProductsManager.default().isAvailableAllFunction() == false {
|
|
KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
|
|
KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
|