KMPDFEditExtractWindow.swift 5.5 KB

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