|
@@ -8,72 +8,12 @@
|
|
|
import Foundation
|
|
|
|
|
|
extension KMNThumbnailBaseViewController {
|
|
|
- public func fileNameWithSelectedPages(_ itemIndexes: IndexSet) -> String {
|
|
|
- var pagesName = ""
|
|
|
- if (itemIndexes.count > 1) {
|
|
|
- pagesName.append(" pages")
|
|
|
- } else {
|
|
|
- pagesName.append(" page")
|
|
|
- }
|
|
|
-
|
|
|
- let docmentName = showDocument?.documentURL.deletingPathExtension().lastPathComponent ?? ""
|
|
|
- let tFileName = String(format: "%@ %@", pagesName,KMNTools.parseIndexSet(indexSet: itemIndexes))
|
|
|
- return String(format: "%@%@", docmentName,tFileName)
|
|
|
- }
|
|
|
-
|
|
|
- private 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
|
|
|
- }
|
|
|
-
|
|
|
- public func selectPages(with array: [Int]) {
|
|
|
- var selectIndexPaths: Set<IndexPath> = []
|
|
|
-
|
|
|
- for i in 0..<(showDocument?.pageCount ?? 0) {
|
|
|
- if array.contains(Int(i)) {
|
|
|
- selectIndexPaths.insert(IndexPath(item: Int(i), section: 0))
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- collectionView.selectionIndexPaths = selectIndexPaths
|
|
|
-
|
|
|
- if selectIndexPaths.isEmpty { return }
|
|
|
-
|
|
|
- let firstIndexPath = selectIndexPaths.first
|
|
|
- collectionView.scrollToItems(at: [firstIndexPath ?? IndexPath(item: 0, section: 0)], scrollPosition: .top)
|
|
|
- }
|
|
|
|
|
|
- public func indexpathsToIndexs(indexpaths: Set<IndexPath>) -> IndexSet {
|
|
|
- var indexs = IndexSet()
|
|
|
- for indexPath in indexpaths {
|
|
|
- indexs.insert(indexPath.item)
|
|
|
- }
|
|
|
- return indexs
|
|
|
- }
|
|
|
-
|
|
|
- public func indexsToIndexpaths(indexs: IndexSet) -> Set<IndexPath> {
|
|
|
- var indexpaths = Set<IndexPath>()
|
|
|
- for index in indexs {
|
|
|
- indexpaths.insert(IndexPath(item: index, section: 0))
|
|
|
- }
|
|
|
- return indexpaths
|
|
|
- }
|
|
|
-
|
|
|
- public func insertFormPDF(insertPages: [CPDFPage],pageDex:Int) {
|
|
|
+ public func insertFormPages(insertPages: [CPDFPage],pageDex:Int) {
|
|
|
var pageIndexDex: Int = pageDex
|
|
|
var indexpaths = Set<IndexPath>()
|
|
|
for page in insertPages {
|
|
|
- let isSuccessFul = showDocument?.insertPageObject(page, at: UInt(pageIndexDex))
|
|
|
+ let isSuccessFul = showDocument?.insertPageObject(page, at: UInt(pageIndexDex))
|
|
|
if(isSuccessFul == true) {
|
|
|
indexpaths.insert(IndexPath(item: pageIndexDex, section: 0))
|
|
|
pageIndexDex += 1
|
|
@@ -85,49 +25,19 @@ extension KMNThumbnailBaseViewController {
|
|
|
collectionView.selectionIndexPaths = indexpaths
|
|
|
}
|
|
|
|
|
|
- public func insertBlankPage(pageSize: CGSize,pageDex:Int) {
|
|
|
+ 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))
|
|
|
- }
|
|
|
-
|
|
|
+ if(isSuccessFul == true) {
|
|
|
+ indexpaths.insert(IndexPath(item: pageDex, section: 0))
|
|
|
+ }
|
|
|
+
|
|
|
refreshDatas()
|
|
|
collectionView.reloadData()
|
|
|
collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
|
|
|
collectionView.selectionIndexPaths = indexpaths
|
|
|
}
|
|
|
|
|
|
- public 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
|
|
|
- }
|
|
|
-
|
|
|
public func insertImageFilePath(imagePath:String,pageDex:Int)->Bool{
|
|
|
var isSuccessFul:Bool = false
|
|
|
if (FileManager.default.fileExists(atPath: imagePath)) {
|
|
@@ -148,7 +58,7 @@ extension KMNThumbnailBaseViewController {
|
|
|
pageRotate = 270
|
|
|
} else if (pageRotate == 450) {
|
|
|
pageRotate = 90
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
page.rotation = pageRotate
|
|
|
|
|
@@ -161,7 +71,6 @@ extension KMNThumbnailBaseViewController {
|
|
|
|
|
|
collectionView.reloadItems(at: tIndexPaths) // Ensure correct type conversion
|
|
|
collectionView.selectionIndexPaths = tIndexPaths
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public func reversePages(indexs: IndexSet) {
|
|
@@ -180,15 +89,15 @@ extension KMNThumbnailBaseViewController {
|
|
|
}
|
|
|
res = doc.exchangePage(at: UInt(first), withPageAt: UInt(last))
|
|
|
if res {
|
|
|
- theIdxs.remove(first)
|
|
|
+ theIdxs.remove(first)
|
|
|
theIdxs.remove(last)
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
if res {
|
|
|
refreshDatas()
|
|
|
- let selected_indexpaths = self.indexsToIndexpaths(indexs: indexs)
|
|
|
-
|
|
|
+ let selected_indexpaths = KMNTools.indexsToIndexpaths(indexs: indexs)
|
|
|
+
|
|
|
collectionView.reloadItems(at: selected_indexpaths)
|
|
|
|
|
|
collectionView.selectionIndexPaths = selected_indexpaths
|
|
@@ -204,10 +113,10 @@ extension KMNThumbnailBaseViewController {
|
|
|
var index = targetIndexpaths.sorted().first!.item
|
|
|
var tIndexPaths: Set<IndexPath> = []
|
|
|
|
|
|
- var indexSet = indexpathsToIndexs(indexpaths: targetIndexpaths)
|
|
|
+ var indexSet = KMNTools.indexpathsToIndexs(indexpaths: targetIndexpaths)
|
|
|
|
|
|
showDocument?.removePage(at: indexSet)
|
|
|
-
|
|
|
+
|
|
|
for document in documents {
|
|
|
for i in 0 ..< document.pageCount {
|
|
|
let page = document.page(at: i)
|
|
@@ -219,18 +128,15 @@ extension KMNThumbnailBaseViewController {
|
|
|
}
|
|
|
|
|
|
refreshDatas()
|
|
|
-
|
|
|
collectionView.reloadData()
|
|
|
-
|
|
|
collectionView.selectionIndexPaths = tIndexPaths
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public func deletePages(indexpaths:Set<IndexPath>) {
|
|
|
var changeIndex:IndexSet = []
|
|
|
var tIndexPaths: Set<IndexPath> = []
|
|
|
tIndexPaths = indexpaths
|
|
|
-
|
|
|
+
|
|
|
for targetIndexPath in tIndexPaths {
|
|
|
changeIndex.insert(targetIndexPath.item)
|
|
|
|
|
@@ -238,16 +144,16 @@ extension KMNThumbnailBaseViewController {
|
|
|
if(cellView != nil) {
|
|
|
cellView?.thumbnailMode.removeCacheImage()
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
showDocument?.removePage(at: changeIndex)
|
|
|
refreshDatas()
|
|
|
- collectionView.deleteItems(at: tIndexPaths)
|
|
|
+ collectionView.reloadData()
|
|
|
}
|
|
|
|
|
|
public func movePages(pages:[CPDFPage],destinationDex:Int) {
|
|
|
var destinationIndex = destinationDex
|
|
|
-
|
|
|
+
|
|
|
for dragPage in dragLocalityPages {
|
|
|
let dragIndex = dragPage.pageIndex()
|
|
|
if destinationIndex > dragIndex {
|
|
@@ -259,18 +165,22 @@ extension KMNThumbnailBaseViewController {
|
|
|
destinationIndex += 1
|
|
|
}
|
|
|
|
|
|
- var selectArray:[Int] = []
|
|
|
+ var selectIndexPaths: Set<IndexPath> = []
|
|
|
+
|
|
|
for dragPage in dragLocalityPages {
|
|
|
let dragIndex = dragPage.pageIndex()
|
|
|
- selectArray.append(Int(dragIndex))
|
|
|
+ selectIndexPaths.insert(IndexPath(item: Int(dragIndex), section: 0))
|
|
|
}
|
|
|
|
|
|
refreshDatas()
|
|
|
collectionView.reloadData()
|
|
|
- self.selectPages(with: selectArray)
|
|
|
+ collectionView.selectionIndexPaths = selectIndexPaths
|
|
|
+ if selectIndexPaths.isEmpty { return }
|
|
|
+ let firstIndexPath = selectIndexPaths.first
|
|
|
+ collectionView.scrollToItems(at: [firstIndexPath ?? IndexPath(item: 0, section: 0)], scrollPosition: .top)
|
|
|
}
|
|
|
|
|
|
- public func insertFromFilePath(fileNames:[String],formDex:Int,indexDex:UInt,selectIndexs:[Int],completionBlock:@escaping ([Int])->Void)-> Void {
|
|
|
+ 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
|
|
@@ -285,7 +195,7 @@ extension KMNThumbnailBaseViewController {
|
|
|
for i in 0 ..< pdf.pageCount {
|
|
|
let insertPage = pdf.page(at: i)
|
|
|
self.showDocument?.insertPageObject(insertPage, at: insertDex)
|
|
|
- tSelectIndex.append(Int(insertDex))
|
|
|
+ tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
|
|
|
insertDex += 1
|
|
|
}
|
|
|
|
|
@@ -297,14 +207,13 @@ extension KMNThumbnailBaseViewController {
|
|
|
} else {
|
|
|
return completionBlock(tSelectIndex)
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
for i in 0 ..< pdf.pageCount {
|
|
|
let insertPage = pdf.page(at: i)
|
|
|
showDocument?.insertPageObject(insertPage ?? CPDFPage(), at: insertDex)
|
|
|
- tSelectIndex.append(Int(insertDex))
|
|
|
+ tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
|
|
|
insertDex += 1
|
|
|
}
|
|
|
var tFormDex = formDex
|
|
@@ -319,10 +228,9 @@ extension KMNThumbnailBaseViewController {
|
|
|
}
|
|
|
} else if supportDragFileTypes().contains(pathExtension) {
|
|
|
if KMConvertPDFManager.supportImages().contains(pathExtension) {
|
|
|
-
|
|
|
- let isSueccessFul = insertImageFilePath(imagePath: path, pageDex: Int(indexDex))
|
|
|
+ let isSueccessFul = insertImageFilePath(imagePath: path, pageDex: Int(indexDex))
|
|
|
if(isSueccessFul) {
|
|
|
- tSelectIndex.append(Int(insertDex))
|
|
|
+ tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
|
|
|
insertDex += 1
|
|
|
}
|
|
|
|
|
@@ -335,18 +243,18 @@ extension KMNThumbnailBaseViewController {
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
- convertOffice(filePath: path) { convertPDFPath in
|
|
|
+ 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.append(Int(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) {
|
|
@@ -359,57 +267,11 @@ extension KMNThumbnailBaseViewController {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private func convertOffice(filePath: String, completionBlock:@escaping (String?)->Void) -> Void {
|
|
|
- let today = Date()
|
|
|
- let dateFormatter = DateFormatter()
|
|
|
- dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
|
|
|
- let dateString = dateFormatter.string(from: today)
|
|
|
-
|
|
|
- let folderPath = "convertToPDF_office_" + dateString + "." + "pdf"
|
|
|
-
|
|
|
- let savePath: String? = folderPath.kUrlToPDFFolderPath() as String
|
|
|
-
|
|
|
- KMConvertPDFManager.convertFile(filePath, savePath: savePath!) { success, errorDic in
|
|
|
- if errorDic != nil || !success || !FileManager.default.fileExists(atPath: savePath!) {
|
|
|
-
|
|
|
- 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 = NSLocalizedString("Please install Microsoft Office to create PDFs from Office files", comment: "")
|
|
|
- alert.messageText = NSLocalizedString("Failed to Create PDF", comment: "")
|
|
|
- alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
|
|
|
- alert.runModal()
|
|
|
- completionBlock(nil)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- if !savePath!.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()
|
|
|
- completionBlock(nil)
|
|
|
- return
|
|
|
- }
|
|
|
- completionBlock(savePath)
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public func extractPages(indexpaths: Set<IndexPath>, oneDocumentPerPage: Bool, callback: @escaping KMResultBlock) {
|
|
|
- let pageIndexs = self.indexpathsToIndexs(indexpaths: indexpaths)
|
|
|
+ let pageIndexs = KMNTools.indexpathsToIndexs(indexpaths: indexpaths)
|
|
|
let oneDocument = !oneDocumentPerPage
|
|
|
let document = self.showDocument!
|
|
|
/// 提取的页面
|
|
@@ -453,7 +315,6 @@ extension KMNThumbnailBaseViewController {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- return
|
|
|
} else {
|
|
|
let panel = NSOpenPanel()
|
|
|
panel.canChooseFiles = false
|
|
@@ -489,7 +350,6 @@ extension KMNThumbnailBaseViewController {
|
|
|
return
|
|
|
}
|
|
|
NSWorkspace.shared.activateFileViewerSelecting(successArray!)
|
|
|
-
|
|
|
callback(.success, successArray ?? NSURL())
|
|
|
}
|
|
|
}
|