123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- //
- // KMPDFEditExtractWindow.swift
- // Cisdem PDFMaster
- //
- // 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
- }
- }
- }
|