KMPDFEditExtractWindow.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // KMPDFEditExtractWindow.swift
  3. // PDF Reader Pro
  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 weak 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. if (pageString.isEmpty == false) {
  24. self._selectedPagesName = pageString
  25. self._attribute?.bAllPage = false
  26. self._attribute?.pagesType = .custom
  27. self._attribute?.pagesString = pageString
  28. } else {
  29. self._attribute?.bAllPage = true
  30. self._attribute?.pagesType = .all
  31. }
  32. self._attribute?.filePath = document.documentURL.path
  33. // self._attribute?.pdfDocument = self._pdfDocument!
  34. }
  35. convenience init(fileURL documentPath: URL, selectedPagesName pageString: String) {
  36. self.init(windowNibName: "KMPDFEditExtractWindow")
  37. self._selectedPagesName = pageString
  38. self._pdfDocument = CPDFDocument(url: documentPath)
  39. self._attribute = KMFileAttribute()
  40. if (pageString.isEmpty == false) {
  41. self._selectedPagesName = pageString
  42. self._attribute?.bAllPage = false
  43. self._attribute?.pagesType = .custom
  44. self._attribute?.pagesString = pageString
  45. } else {
  46. self._attribute?.bAllPage = true
  47. self._attribute?.pagesType = .all
  48. }
  49. self._attribute?.filePath = documentPath.path
  50. // self._attribute?.pdfDocument = self._pdfDocument!
  51. }
  52. override func windowDidLoad() {
  53. super.windowDidLoad()
  54. self._extractPerPageFlag = false
  55. self.extractAsDocumentsPerPageButton.title = KMLocalizedString("Each page in a separate file", nil)
  56. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  57. // if ([self.pdfDocument isEncrypted] && [self.pdfDocument isLocked]) {
  58. // PasswordWindowController *com = [[PasswordWindowController alloc] initWithWindowNibName:@"PasswordWindowController"];
  59. // com.fileURL = [self.pdfDocument documentURL];
  60. // [com beginSheetModalForWindow:self.window completionHandler:^(NSString *password) {
  61. // if (password) {
  62. // self.attribute.password = password;
  63. // [self.pdfDocument unlockWithPassword:password];
  64. // } else {
  65. // self.pdfDocument = nil;
  66. // [NSApp endSheet:[self window]];
  67. // [[self window] orderOut:self];
  68. // [self release];
  69. // }
  70. //
  71. // }];
  72. // [com release];
  73. // }
  74. // });
  75. self.window?.title = KMLocalizedString("Extract",nil)
  76. self.cancelButton.title = KMLocalizedString("Cancel",nil)
  77. self.extractButton.title = KMLocalizedString("Extract",nil)
  78. self.deleteExtractButton.title = KMLocalizedString("Delete pages after extraction",nil)
  79. self.deleteExtractButton.wantsLayer = true
  80. self.extractAsDocumentsPerPageButton.wantsLayer = true
  81. }
  82. /*
  83. - (void)updateButtonStateWithONButton:(NSButton *)sender
  84. {
  85. sender.state = NSControlStateValueOn;
  86. }
  87. */
  88. @IBAction func buttonItemClicked_Cancel(_ sender: AnyObject) {
  89. self._pdfDocument = nil;
  90. guard let block = self.callback else {
  91. return
  92. }
  93. block(nil, nil, false, false)
  94. }
  95. @IBAction func buttonItemClicked_Extract(_ sender: AnyObject) {
  96. if let data = self._attribute?.fetchSelectPages().isEmpty, data {
  97. let alert = NSAlert()
  98. alert.alertStyle = .critical
  99. alert.messageText = String(format: "%@ %@", self._attribute?.filePath.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
  100. alert.runModal()
  101. return
  102. }
  103. if (self.deleteExtractButton.state == .on && self._pdfDocument!.pageCount == (self._attribute?.fetchSelectPages().count)!) {
  104. Task {
  105. _ = 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)))
  106. }
  107. return;
  108. }
  109. guard let block = self.callback else {
  110. return
  111. }
  112. block(self._pdfDocument, self._attribute, self._extractPerPageFlag, self.deleteExtractButton.state == .on)
  113. }
  114. @IBAction func buttonItemClicked_OnePDFPerPage(_ sender: AnyObject) {
  115. if (self.extractAsDocumentsPerPageButton.state == .on) {
  116. self._extractPerPageFlag = true
  117. } else {
  118. self._extractPerPageFlag = false
  119. }
  120. }
  121. }