KMPDFEditExtractWindow.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // KMPDFEditExtractWindow.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/11/14.
  6. //
  7. import Cocoa
  8. class KMPDFEditExtractWindow: NSWindowController {
  9. @IBOutlet var cancelButton: NSButton!
  10. @IBOutlet var extractButton: NSButton!
  11. @IBOutlet var deleteExtractButton: NSButton!
  12. @IBOutlet var extractAsDocumentsPerPageButton: NSButton! //每页提取为单个PDF
  13. var callback: ((CPDFDocument?, KMFileAttribute?, Bool, Bool)->Void)?
  14. private var _extractPerPageFlag = false //是否按页提取标志位;
  15. private var _selectedPagesName = ""
  16. private var _pdfDocument: CPDFDocument?
  17. private var _attribute: KMFileAttribute?
  18. convenience init(document: CPDFDocument, selectedPagesName pageString: String) {
  19. self.init(windowNibName: "KMPDFEditExtractWindow")
  20. self._selectedPagesName = pageString
  21. self._pdfDocument = document
  22. self._attribute = KMFileAttribute()
  23. self._attribute?.pagesString = pageString
  24. if (pageString.isEmpty == false) {
  25. self._selectedPagesName = pageString
  26. self._attribute?.bAllPage = false
  27. } else {
  28. self._attribute?.bAllPage = true
  29. }
  30. self._attribute?.filePath = document.documentURL.path
  31. self._attribute?.pdfDocument = self._pdfDocument!
  32. }
  33. convenience init(fileURL documentPath: URL, selectedPagesName pageString: String) {
  34. self.init(windowNibName: "KMPDFEditExtractWindow")
  35. self._selectedPagesName = pageString
  36. self._pdfDocument = CPDFDocument(url: documentPath)
  37. self._attribute = KMFileAttribute()
  38. self._attribute?.pagesString = pageString
  39. if (pageString.isEmpty == false) {
  40. self._selectedPagesName = pageString
  41. self._attribute?.bAllPage = false
  42. } else {
  43. self._attribute?.bAllPage = true
  44. }
  45. self._attribute?.filePath = documentPath.path
  46. self._attribute?.pdfDocument = self._pdfDocument!
  47. }
  48. override func windowDidLoad() {
  49. super.windowDidLoad()
  50. self._extractPerPageFlag = false
  51. self.extractAsDocumentsPerPageButton.title = KMLocalizedString("Each page in a separate file", nil)
  52. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  53. // if ([self.pdfDocument isEncrypted] && [self.pdfDocument isLocked]) {
  54. // PasswordWindowController *com = [[PasswordWindowController alloc] initWithWindowNibName:@"PasswordWindowController"];
  55. // com.fileURL = [self.pdfDocument documentURL];
  56. // [com beginSheetModalForWindow:self.window completionHandler:^(NSString *password) {
  57. // if (password) {
  58. // self.attribute.password = password;
  59. // [self.pdfDocument unlockWithPassword:password];
  60. // } else {
  61. // self.pdfDocument = nil;
  62. // [NSApp endSheet:[self window]];
  63. // [[self window] orderOut:self];
  64. // [self release];
  65. // }
  66. //
  67. // }];
  68. // [com release];
  69. // }
  70. // });
  71. self.window?.title = KMLocalizedString("Extract",nil)
  72. self.cancelButton.title = KMLocalizedString("Cancel",nil)
  73. self.extractButton.title = KMLocalizedString("Extract",nil)
  74. self.deleteExtractButton.title = KMLocalizedString("Delete pages after extraction",nil)
  75. self.deleteExtractButton.wantsLayer = true
  76. self.extractAsDocumentsPerPageButton.wantsLayer = true
  77. }
  78. /*
  79. - (void)updateButtonStateWithONButton:(NSButton *)sender
  80. {
  81. sender.state = NSControlStateValueOn;
  82. }
  83. */
  84. @IBAction func buttonItemClicked_Cancel(_ sender: AnyObject) {
  85. self._pdfDocument = nil;
  86. guard let block = self.callback else {
  87. return
  88. }
  89. block(nil, nil, false, false)
  90. }
  91. @IBAction func buttonItemClicked_Extract(_ sender: AnyObject) {
  92. if let data = self._attribute?.fetchSelectPages().isEmpty, data {
  93. let alert = NSAlert()
  94. alert.alertStyle = .critical
  95. alert.messageText = String(format: "%@ %@", self._attribute?.filePath.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
  96. alert.runModal()
  97. return
  98. }
  99. if (self.deleteExtractButton.state == .on && self._pdfDocument!.pageCount == (self._attribute?.fetchSelectPages().count)!) {
  100. let alert = NSAlert()
  101. alert.alertStyle = .critical
  102. alert.messageText = String(format: "%@ %@", self._attribute?.filePath.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
  103. alert.runModal()
  104. return;
  105. }
  106. guard let block = self.callback else {
  107. return
  108. }
  109. block(self._pdfDocument, self._attribute, self._extractPerPageFlag, self.deleteExtractButton.state == .on)
  110. }
  111. @IBAction func buttonItemClicked_OnePDFPerPage(_ sender: AnyObject) {
  112. if (self.extractAsDocumentsPerPageButton.state == .on) {
  113. self._extractPerPageFlag = true
  114. } else {
  115. self._extractPerPageFlag = false
  116. }
  117. }
  118. }