// // KMPDFEditExtractWindow.swift // PDF Reader Pro // // Created by tangchao on 2023/11/14. // import Cocoa class KMPDFEditExtractWindow: NSWindowController { @IBOutlet var cancelButton: NSButton! @IBOutlet var extractButton: NSButton! @IBOutlet var deleteExtractButton: NSButton! @IBOutlet var extractAsDocumentsPerPageButton: NSButton! //每页提取为单个PDF var callback: ((CPDFDocument?, KMFileAttribute?, Bool, Bool)->Void)? private var _extractPerPageFlag = false //是否按页提取标志位 private var _selectedPagesName = "" private weak var _pdfDocument: CPDFDocument? private var _attribute: KMFileAttribute? convenience init(document: CPDFDocument, selectedPagesName pageString: String) { self.init(windowNibName: "KMPDFEditExtractWindow") self._selectedPagesName = pageString self._pdfDocument = document self._attribute = KMFileAttribute() self._attribute?.pageCnt = Int(document.pageCount) if (pageString.isEmpty == false) { self._selectedPagesName = pageString self._attribute?.bAllPage = false self._attribute?.pagesType = .custom self._attribute?.pagesString = pageString } else { self._attribute?.bAllPage = true self._attribute?.pagesType = .all } self._attribute?.filePath = document.documentURL.path } convenience init(fileURL documentPath: URL, selectedPagesName pageString: String) { self.init(windowNibName: "KMPDFEditExtractWindow") self._selectedPagesName = pageString self._pdfDocument = CPDFDocument(url: documentPath) self._attribute = KMFileAttribute() if (pageString.isEmpty == false) { self._selectedPagesName = pageString self._attribute?.bAllPage = false self._attribute?.pagesType = .custom self._attribute?.pagesString = pageString } else { self._attribute?.bAllPage = true self._attribute?.pagesType = .all } self._attribute?.filePath = documentPath.path // self._attribute?.pdfDocument = self._pdfDocument! } override func windowDidLoad() { super.windowDidLoad() self._extractPerPageFlag = false self.extractAsDocumentsPerPageButton.title = KMLocalizedString("Each page in a separate file", nil) self.window?.title = KMLocalizedString("Extract",nil) self.cancelButton.title = KMLocalizedString("Cancel",nil) self.extractButton.title = KMLocalizedString("Extract",nil) self.deleteExtractButton.title = KMLocalizedString("Delete pages after extraction",nil) self.deleteExtractButton.wantsLayer = true self.extractAsDocumentsPerPageButton.wantsLayer = true } @IBAction func buttonItemClicked_Cancel(_ sender: AnyObject) { self._pdfDocument = nil; guard let block = self.callback else { return } block(nil, nil, false, false) } @IBAction func buttonItemClicked_Extract(_ sender: AnyObject) { if let data = self._attribute?.fetchSelectPages().isEmpty, data { Task { _ = await KMAlertTool.runModel(message: String(format: "%@ %@", self._attribute?.filePath.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))) } return } let pageCnt = self._pdfDocument?.pageCount ?? 0 let pages = self._attribute?.fetchSelectPages() ?? [] if (self.deleteExtractButton.state == .on && pageCnt == pages.count) { Task { _ = await KMAlertTool.runModel(message: String(format: "%@ %@", self._attribute?.filePath.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))) } return } guard let block = self.callback else { return } block(self._pdfDocument, self._attribute, self._extractPerPageFlag, self.deleteExtractButton.state == .on) } @IBAction func buttonItemClicked_OnePDFPerPage(_ sender: AnyObject) { if (self.extractAsDocumentsPerPageButton.state == .on) { self._extractPerPageFlag = true } else { self._extractPerPageFlag = false } } }