Преглед на файлове

【综合】合并补充图片格式

tangchao преди 5 месеца
родител
ревизия
cf471640aa

+ 5 - 2
PDF Office/PDF Master/Class/PDFTools/Merge/MergeNew/KMMergeWindowController.swift

@@ -274,8 +274,11 @@ extension KMMergeWindowController {
             }
         }
         
-        
-        let fileName = (files.first?.filePath.deletingPathExtension.lastPathComponent ?? "") + "_Merged"
+        var theFilepath = files.first?.filePath
+        if let filepath = files.first?.oriFilePath {
+            theFilepath = filepath
+        }
+        let fileName = (theFilepath?.deletingPathExtension.lastPathComponent ?? "") + "_Merged"
         DispatchQueue.main.async {
             if self.oldPDFDocument.outlineRoot == nil {
                 self.oldPDFDocument.outlineRoot = PDFOutline.init()

+ 10 - 2
PDF Office/PDF Master/Class/PDFTools/Merge/MergeNew/View/KMMergeBlankView/KMMergeBlankView.swift

@@ -86,7 +86,11 @@ extension KMMergeBlankDragView {
                     if path.lowercased().hasSuffix("pdf") {
                         isCanDrag = true
                     } else {
-                        isCanDrag = false
+                        if KMTools.isImageType(path.pathExtension) {
+                            isCanDrag = true
+                        } else {
+                            isCanDrag = false
+                        }
                         break
                     }
                 }
@@ -98,7 +102,11 @@ extension KMMergeBlankDragView {
                     if resolvedUrl.path.lowercased().hasSuffix("pdf") {
                         isCanDrag = true
                     } else {
-                        isCanDrag = false
+                        if KMTools.isImageType(resolvedUrl.pathExtension) {
+                            isCanDrag = true
+                        } else {
+                            isCanDrag = false
+                        }
                     }
                 } else {
                     print("无法解析文件地址")

+ 66 - 3
PDF Office/PDF Master/Class/PDFTools/Merge/MergeNew/View/KMMergeView.swift

@@ -94,7 +94,15 @@ class KMMergeView: KMBaseXibView {
         boxLabel.textColor = KMAppearance.Layout.h0Color()
         
         blankView.dragAction = { [weak self] filePaths in
-            self?.addFilePaths(urls: filePaths)
+            var theFileUrls: [URL] = []
+            for fileUrl in filePaths {
+                if KMTools.isImageType(fileUrl.pathExtension) {
+                    self?.addImageFile(fileUrl: fileUrl)
+                } else {
+                    theFileUrls.append(fileUrl)
+                }
+            }
+            self?.addFilePaths(urls: theFileUrls)
         }
         
         blankView.mouseDownAction = { [unowned self] view in
@@ -210,7 +218,11 @@ extension KMMergeView: NSTableViewDelegate {
             if url!.path.lowercased().hasSuffix("pdf") {
                 isCanDrag = true
             } else {
-                isCanDrag = false
+                if KMTools.isImageType(url?.pathExtension ?? "") {
+                    isCanDrag = true
+                } else {
+                    isCanDrag = false
+                }
             }
         } else if (pboard.availableType(from: [MyTableCellViewDataType]) != nil) {
             result = .every
@@ -257,7 +269,15 @@ extension KMMergeView: NSTableViewDelegate {
 //                   array.append(URL(string: path)!)
 ////                       }
 //               }
-            addFilePaths(urls: array)
+            var theFileUrls: [URL] = []
+            for fileUrl in array {
+                if KMTools.isImageType(fileUrl.pathExtension) {
+                    self.addImageFile(fileUrl: fileUrl)
+                } else {
+                    theFileUrls.append(fileUrl)
+                }
+            }
+            addFilePaths(urls: theFileUrls)
             result = true
        } else if pboard.availableType(from: [MyTableCellViewDataType]) != nil {
            guard let data = info.draggingPasteboard.data(forType: MyTableCellViewDataType),
@@ -392,6 +412,40 @@ extension KMMergeView {
         }
     }
     
+    func addImageFile(fileUrl: URL) {
+        if let image = NSImage(contentsOf: fileUrl) {
+            if let page = PDFPage(image: image) {
+                let document = PDFDocument()
+                document.insert(page, at: 0)
+                
+                var path = self._saveImagePath() + "/" + fileUrl.deletingPathExtension().lastPathComponent + ".pdf"
+                path = KMTools.getUniqueFilePath(filePath: path)
+                let result = document.write(toFile: path)
+                if result {
+                    let file = KMFileAttribute()
+                    file.filePath = path
+                    file.oriFilePath = fileUrl.path
+                    file.myPDFDocument = document
+                    
+//                    let attribe = try?FileManager.default.attributesOfItem(atPath: fileURL.path)
+//                    let fileSize = attribe?[FileAttributeKey.size] as? CGFloat ?? 0
+//                    size = fileSize + size
+//
+//                    if !IAPProductsManager.default().isAvailableAllFunction() && (files.count >= 2 || size > 20 * 1024 * 1024) {
+//                        KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
+//                        return
+//                    }
+                    
+                    self.files.append(file)
+                }
+            }
+        } else {
+            Task {
+                _ = await KMAlertTool.runModel(message: NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: ""))
+            }
+        }
+    }
+    
     func openPasswordFile(completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
         if lockFiles.count != 0 {
             let file = lockFiles[lockFilesIndex]
@@ -429,4 +483,13 @@ extension KMMergeView {
         
         return false
     }
+    
+    private func _saveImagePath() -> String {
+        let rootPath = KMDataManager.fetchAppSupportOfBundleIdentifierDirectory()
+        let path = rootPath.appendingPathComponent("Merge").path
+        if FileManager.default.fileExists(atPath: path) == false {
+            try?FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false)
+        }
+        return path
+    }
 }