123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- //
- // 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?.bAllPage = false
- self._attribute?.pagesType = .odd
- self._attribute?.pagesString = pageString
- if (pageString.isEmpty == false) {
- self._selectedPagesName = pageString
- self._attribute?.bAllPage = false
- } else {
- self._attribute?.bAllPage = true
- }
- self._attribute?.filePath = document.documentURL.path
- // self._attribute?.pdfDocument = self._pdfDocument!
- }
-
- convenience init(fileURL documentPath: URL, selectedPagesName pageString: String) {
- self.init(windowNibName: "KMPDFEditExtractWindow")
-
- self._selectedPagesName = pageString
- self._pdfDocument = CPDFDocument(url: documentPath)
-
- self._attribute = KMFileAttribute()
- self._attribute?.pagesString = pageString
- if (pageString.isEmpty == false) {
- self._selectedPagesName = pageString
-
- self._attribute?.bAllPage = false
- } else {
- self._attribute?.bAllPage = true
- }
- 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)
-
- // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- // if ([self.pdfDocument isEncrypted] && [self.pdfDocument isLocked]) {
- // PasswordWindowController *com = [[PasswordWindowController alloc] initWithWindowNibName:@"PasswordWindowController"];
- // com.fileURL = [self.pdfDocument documentURL];
- // [com beginSheetModalForWindow:self.window completionHandler:^(NSString *password) {
- // if (password) {
- // self.attribute.password = password;
- // [self.pdfDocument unlockWithPassword:password];
- // } else {
- // self.pdfDocument = nil;
- // [NSApp endSheet:[self window]];
- // [[self window] orderOut:self];
- // [self release];
- // }
- //
- // }];
- // [com release];
- // }
-
- // });
-
- 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
- }
-
- /*
- - (void)updateButtonStateWithONButton:(NSButton *)sender
- {
- sender.state = NSControlStateValueOn;
- }
- */
-
- @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 {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = String(format: "%@ %@", self._attribute?.filePath.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
- alert.runModal()
- return
- }
-
- if (self.deleteExtractButton.state == .on && self._pdfDocument!.pageCount == (self._attribute?.fetchSelectPages().count)!) {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = String(format: "%@ %@", self._attribute?.filePath.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
- alert.runModal()
- 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
- }
- }
- }
|