123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- //
- // KMAIOpenPDFFilesVC.swift
- // PDF Master
- //
- // Created by wanjun on 2023/5/22.
- //
- import Cocoa
- class KMAIOpenPDFFilesVC: NSViewController {
-
- @IBOutlet weak var openPDFFilesLabel: NSTextField!
- @IBOutlet weak var openPDFFilesImageView: NSImageView!
- @IBOutlet weak var selectYourFilesBox: KMBox!
- @IBOutlet weak var selectYourFilesLabel: NSTextField!
- @IBOutlet weak var selectYourFilesImageView: NSImageView!
- @IBOutlet weak var orDropFilesHereToOpenLabel: NSTextField!
- @IBOutlet weak var creatPDFLabel: NSTextField!
- @IBOutlet weak var newFromFilesLabel: NSTextField!
- @IBOutlet weak var newFromFilesImageView: NSImageView!
- @IBOutlet weak var newBlankPageLabel: NSTextField!
- @IBOutlet weak var newBlankPageImageView: NSImageView!
- @IBOutlet weak var importFromScannerLabel: NSTextField!
- @IBOutlet weak var importFromScannerImageView: NSImageView!
- var deviceBrowserWC: KMDeviceBrowserWindowController?
-
- @IBOutlet weak var leftBox: NSBox!
- @IBOutlet weak var rightBox: NSBox!
- @IBOutlet weak var leftBoxRightConstraint: NSLayoutConstraint!
- @IBOutlet weak var rightBoxLeftConstraint: NSLayoutConstraint!
- @IBOutlet weak var rightBoxTopConstraint: NSLayoutConstraint!
- @IBOutlet weak var rightBoxHeightConstraint: NSLayoutConstraint!
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- self.initLocalization()
- self.initializeUI()
- }
-
-
- // MARK: initialize
-
- func initializeUI() -> Void {
- self.openPDFFilesLabel.textColor = NSColor.km_init(hex: "#252629")
- self.openPDFFilesLabel.font = NSFont.SFProTextSemiboldFont(20.0)
- self.openPDFFilesImageView.image = NSImage(named: "icon_empty_addFiles")
- self.selectYourFilesBox.cornerRadius = 4.0
- self.selectYourFilesBox.fillColor = NSColor.km_init(hex: "#1770F4")
- self.selectYourFilesLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
- self.selectYourFilesLabel.font = NSFont.SFProTextRegularFont(16.0)
- self.selectYourFilesImageView.image = NSImage(named: "icon_SelectYourFiles")
- self.orDropFilesHereToOpenLabel.textColor = NSColor.km_init(hex: "#616469")
- self.orDropFilesHereToOpenLabel.font = NSFont.SFProTextRegularFont(14.0)
-
- self.creatPDFLabel.textColor = NSColor.km_init(hex: "#252629")
- self.creatPDFLabel.font = NSFont.SFProTextSemiboldFont(20.0)
- self.newFromFilesLabel.textColor = NSColor.km_init(hex: "#252629")
- self.newFromFilesLabel.font = NSFont.SFProTextRegularFont(16.0)
- self.newFromFilesImageView.image = NSImage(named: "icon_empty_NewFromFiles")
- self.newBlankPageLabel.textColor = NSColor.km_init(hex: "#252629")
- self.newBlankPageLabel.font = NSFont.SFProTextRegularFont(16.0)
- self.newBlankPageImageView.image = NSImage(named: "icon_empty_NewBlackPage")
- self.importFromScannerLabel.textColor = NSColor.km_init(hex: "#252629")
- self.importFromScannerLabel.font = NSFont.SFProTextRegularFont(16.0)
- self.importFromScannerImageView.image = NSImage(named: "icon_empty_ImportFromScanner")
-
- self.selectYourFilesBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
- if mouseEntered {
- self.selectYourFilesBox.fillColor = NSColor.km_init(hex: "#3F8FF6")
- } else {
- self.selectYourFilesBox.fillColor = NSColor.km_init(hex: "#1770F4")
- }
- }
- self.selectYourFilesBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
- if downEntered {
- self.openPDFButtonAction()
- }
- }
- }
-
- func initLocalization() -> Void {
- self.openPDFFilesLabel.stringValue = NSLocalizedString("Open PDF Files", comment: "")
- self.selectYourFilesLabel.stringValue = NSLocalizedString("Select Your Files", comment: "")
- self.orDropFilesHereToOpenLabel.stringValue = NSLocalizedString("or drop files here to open", comment: "")
- self.creatPDFLabel.stringValue = NSLocalizedString("Create PDF", comment: "")
- self.newFromFilesLabel.stringValue = NSLocalizedString("New From Files", comment: "")
- self.newBlankPageLabel.stringValue = NSLocalizedString("New Blank Page", comment: "")
- self.importFromScannerLabel.stringValue = NSLocalizedString("Import From Scanner", comment: "")
- }
-
- // MARK: Private Methods
-
- func imageToJPG(filePath: String, savePath: String) -> String {
- if NSString(string: NSString(string: filePath).lastPathComponent).pathExtension == "png" ||
- NSString(string: NSString(string: filePath).lastPathComponent).pathExtension == "PNG" {
- let imageName = NSString(string: NSString(string: filePath).lastPathComponent).deletingPathExtension
- let jpgPath = self.fetchDifferentFilePath(filePath: savePath + "/" + imageName + ".jpg")
- if (!FileManager.default.fileExists(atPath: jpgPath as String)) {
- FileManager.default.createFile(atPath: jpgPath as String, contents: nil)
- }
- // 加载 PNG 图像
- guard let pngImage = NSImage(contentsOfFile: filePath) else {
- KMPrint("Failed to load PNG image")
- return filePath
- }
- // 创建 NSBitmapImageRep 对象,并将 PNG 图像绘制到其中
- let bitmap = NSBitmapImageRep(data: pngImage.tiffRepresentation!)
- let rect = NSRect(origin: .zero, size: bitmap!.size)
- bitmap?.draw(in: rect)
- // 将 PNG 图像数据转换为 JPG 图像数据
- guard let jpgData = bitmap?.representation(using: .jpeg, properties: [:]) else {
- KMPrint("Failed to convert PNG to JPG")
- return filePath
- }
- // 保存 JPG 图像数据到文件
- let fileURL = URL(fileURLWithPath: jpgPath)
- do {
- try jpgData.write(to: fileURL)
- KMPrint("JPG image saved successfully")
- return fileURL.path
- } catch {
- KMPrint("Failed to save JPG image: \(error.localizedDescription)")
- return filePath
- }
- }
-
- return filePath
- }
-
- func openImageFile(url: URL) -> Void {
- let filePath = url.path
-
- let fileName: NSString = url.lastPathComponent as NSString
- let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath()).deletingLastPathComponent
- let imageName = NSString(string: NSString(string: filePath).lastPathComponent).deletingPathExtension
-
- let path = self.fetchDifferentFilePath(filePath: savePath + "/" + imageName + ".pdf")
-
- if (!FileManager.default.fileExists(atPath: path.deletingLastPathComponent as String)) {
- try?FileManager.default.createDirectory(atPath: path.deletingLastPathComponent as String, withIntermediateDirectories: true, attributes: nil)
- }
-
- if (!FileManager.default.fileExists(atPath: path as String)) {
- FileManager.default.createFile(atPath: path as String, contents: nil)
- }
- let document = CPDFDocument.init()
- var success = false
-
- let jpgPath = self.imageToJPG(filePath: filePath, savePath: savePath)
- //FIXME: 无法插入图片
- let image = NSImage(contentsOfFile: jpgPath)
- let insertPageSuccess = document?.insertPage(image!.size, withImage: jpgPath, at: document!.pageCount)
- if insertPageSuccess != nil {
- //信号量控制异步
- let semaphore = DispatchSemaphore(value: 0)
- DispatchQueue.global().async {
- success = ((document?.write(toFile: path)) != nil)
- semaphore.signal()
- }
- semaphore.wait()
- } else {
- }
- if success {
- if !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
- }
- NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: path), display: true) { document, documentWasAlreadyOpen, error in
- if error != nil {
- NSApp.presentError(error!)
- return
- }
- }
- }
- }
-
- func openOfficeFile(url: URL) -> Void {
- let filePath = url.path
- let folderPath = "convertToPDF.pdf"
- let savePath = folderPath.kUrlToPDFFolderPath()
-
- if (!FileManager.default.fileExists(atPath: savePath!.deletingLastPathComponent as String)) {
- try?FileManager.default.createDirectory(atPath: savePath!.deletingLastPathComponent as String, withIntermediateDirectories: true, attributes: nil)
- }
-
- if (!FileManager.default.fileExists(atPath: savePath! as String)) {
- FileManager.default.createFile(atPath: savePath! as String, contents: nil)
- }
- if savePath == nil {
- return
- }
- KMConvertPDFManagerOC.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()
- 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()
- return
- }
- NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath!), display: true) { document, documentWasAlreadyOpen, error in
- if error != nil {
- NSApp.presentError(error!)
- return
- }
- }
- }
- }
-
- func fetchUniquePath(_ originalPath: String) -> String {
- var path = originalPath
- let dManager = FileManager.default
- if !dManager.fileExists(atPath: path) {
- if path.extension.count < 1 {
- path = path.stringByAppendingPathExtension("pdf")
- }
- return path
- } else {
- let originalFullFileName = path.lastPathComponent
- let originalFileName = path.lastPathComponent.deletingPathExtension.lastPathComponent
- let originalExtension = path.extension
- let startIndex: Int = 0
- let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
- let fileLocatePath = originalPath.substring(to: endIndex)
- var i = 1
- while (1 != 0) {
- var newName = String(format: "%@%ld", originalFileName, i)
- newName = String(format: "%@%@", newName, originalExtension)
- let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
- if !dManager.fileExists(atPath: newPath) {
- return newPath
- } else {
- i+=1
- continue
- }
- }
- }
- }
-
- func fetchDifferentFilePath(filePath: String) -> String {
- var resultFilePath = filePath
- var index: Int = 0
- while (FileManager.default.fileExists(atPath: resultFilePath)) {
- index += 1
- let path = NSString(string: filePath).deletingPathExtension + "(" + String(index) + ")"
- resultFilePath = NSString(string: path).appendingPathExtension(NSString(string: filePath).pathExtension)!
- }
-
- return resultFilePath;
- }
-
- func openFile(withFilePath path: URL) -> Void {
- let type = path.pathExtension.lowercased()
- if (type == "pdf") {
- if !path.path.isPDFValid() {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("This file format is not supported, please drag in PDF, picture, Office format files", comment: "")
- alert.runModal()
- return
- }
- NSDocumentController.shared.openDocument(withContentsOf: path, display: true) { document, documentWasAlreadyOpen, error in
- if error != nil {
- NSApp.presentError(error!)
- return
- }
- }
- } else if (type == "jpg") ||
- (type == "cur") ||
- (type == "bmp") ||
- (type == "jpeg") ||
- (type == "gif") ||
- (type == "png") ||
- (type == "tiff") ||
- (type == "tif") ||
- (type == "ico") ||
- (type == "icns") ||
- (type == "tga") ||
- (type == "psd") ||
- (type == "eps") ||
- (type == "hdr") ||
- (type == "jp2") ||
- (type == "jpc") ||
- (type == "pict") ||
- (type == "sgi") ||
- (type == "heic") {
- openImageFile(url: path)
- } else if (type == "doc") ||
- (type == "docx") ||
- (type == "xls") ||
- (type == "xlsx") ||
- (type == "ppt") ||
- (type == "pptx") ||
- (type == "pptx") {
- let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
- let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath())
- openOfficeFile(url: path)
- }
- }
-
- func refreshLayout(isLimit limit: Bool) -> Void {
- if limit {
- if self.rightBoxTopConstraint != nil {
- self.rightBoxTopConstraint.constant = 348 + 40
- }
- if self.leftBoxRightConstraint != nil {
- self.leftBoxRightConstraint.constant = 32
- }
- if self.rightBoxLeftConstraint != nil {
- self.rightBoxLeftConstraint.constant = 32
- }
- if self.rightBoxHeightConstraint != nil {
- self.rightBoxHeightConstraint.constant = leftBox.frame.width
- }
- } else {
- if self.rightBoxTopConstraint != nil {
- self.rightBoxTopConstraint.constant = 40
- }
- if self.leftBoxRightConstraint != nil {
- self.leftBoxRightConstraint.constant = 380
- }
- if self.rightBoxLeftConstraint != nil {
- self.rightBoxLeftConstraint.constant = 32 + leftBox.frame.width + 20
- }
- if self.rightBoxHeightConstraint != nil {
- self.rightBoxHeightConstraint.constant = 328
- }
- }
- }
-
- // MARK: Action
-
- @IBAction func createPDFAction(_ sender: NSButton) {
- let tag = sender.tag;
- if tag == 0 {
- // New From Files
- self.trackEvent_create(eventName: "New From File")
-
- self.openSupportPDFButtonAction()
- } else if tag == 1 {
- // New Blank Page
- self.trackEvent_create(eventName: "New Blank Page")
-
- self.openBlankPage("")
- } else if tag == 2 {
- // Import From Scanner
- self.trackEvent_create(eventName: "Import From Scanner")
-
- self.importFromScanner("")
- }
- }
-
- func openPDFButtonAction() {
- NSOpenPanel.km_open_pdf_multi_success(self.view.window!, panel: nil) { urls in
- for url in urls {
- if !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()
- } else {
- NSDocumentController.shared.openDocument(withContentsOf: url, display: true) { document, documentWasAlreadyOpen, error in
- if error != nil {
- NSApp.presentError(error!)
- } else {
-
- }
- }
- }
- }
- }
- }
-
- func openSupportPDFButtonAction() {
- var window = self.view.window
- if (window == nil) {
- window = NSApp.mainWindow
- }
- NSPanel.km_open_multi_success(window!) { panel in
- var array: [String] = []
- for fileType in KMConvertPDFManagerOC.supportFileType() {
- if let string = fileType as? String {
- array.append(string)
- }
- }
- panel.allowedFileTypes = KMTools.pdfExtensions + array
- } completion: { urls in
- for url in urls {
- let type = url.pathExtension.lowercased()
- if (type == "pdf" || type == "PDF") {
- NSDocumentController.shared.km_safe_openDocument(withContentsOf: url, display: true) { _, _, _ in
-
- }
- } else if (type == "jpg") ||
- (type == "cur") ||
- (type == "bmp") ||
- (type == "jpeg") ||
- (type == "gif") ||
- (type == "png") ||
- (type == "tiff") ||
- (type == "tif") ||
- (type == "ico") ||
- (type == "icns") ||
- (type == "tga") ||
- (type == "psd") ||
- (type == "eps") ||
- (type == "hdr") ||
- (type == "jp2") ||
- (type == "jpc") ||
- (type == "pict") ||
- (type == "sgi") ||
- (type == "heic") {
- self.openImageFile(url: url)
- } else if (type == "doc") ||
- (type == "docx") ||
- (type == "xls") ||
- (type == "xlsx") ||
- (type == "ppt") ||
- (type == "pptx") ||
- (type == "pptx") {
- self.openOfficeFile(url: url)
- }
- }
- }
- }
-
- @IBAction func openBlankPage(_ sender: Any) {
- let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
- let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath())
- if (!FileManager.default.fileExists(atPath: savePath.deletingLastPathComponent as String)) {
- try?FileManager.default.createDirectory(atPath: savePath.deletingLastPathComponent as String, withIntermediateDirectories: true, attributes: nil)
- }
-
- if (!FileManager.default.fileExists(atPath: savePath as String)) {
- FileManager.default.createFile(atPath: savePath as String, contents: nil)
- }
- let pdfDocument = CPDFDocument()
- pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
- pdfDocument?.write(to: URL(fileURLWithPath: savePath))
- NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
- if error != nil {
- NSApp.presentError(error!)
- } else {
- if document is KMMainDocument {
- let newDocument = document
- (newDocument as! KMMainDocument).isNewCreated = true
- }
- }
- }
- }
-
- @IBAction func importFromScanner(_ sender: Any) {
- deviceBrowserWC = KMDeviceBrowserWindowController.init(windowNibName: "KMDeviceBrowserWindowController")
- deviceBrowserWC!.type = .scanner
- deviceBrowserWC!.importScannerFileCallback = { [unowned self](url: NSURL) -> Void in
- openFile(withFilePath: url as URL)
- }
- deviceBrowserWC!.showWindow(NSApp.mainWindow)
- }
- }
- // MARK: - Analytics (埋点)
- extension KMAIOpenPDFFilesVC {
- func trackEvent_create(eventName: String) -> Void {
- KMAnalytics.trackEvent(eventName: eventName, parameters: [
- KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.home,
- KMAnalytics.Parameter.labelKey : KMAnalytics.Label.create_Btn], platform: .AppCenter, appTarget: .all)
- }
- }
|