KMPDFEditExtractWindow.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. self.window?.title = KMLocalizedString("Extract",nil)
  57. self.cancelButton.title = KMLocalizedString("Cancel",nil)
  58. self.extractButton.title = KMLocalizedString("Extract",nil)
  59. self.deleteExtractButton.title = KMLocalizedString("Delete pages after extraction",nil)
  60. self.deleteExtractButton.wantsLayer = true
  61. self.extractAsDocumentsPerPageButton.wantsLayer = true
  62. }
  63. /*
  64. - (void)updateButtonStateWithONButton:(NSButton *)sender
  65. {
  66. sender.state = NSControlStateValueOn;
  67. }
  68. */
  69. @IBAction func buttonItemClicked_Cancel(_ sender: AnyObject) {
  70. self._pdfDocument = nil;
  71. guard let block = self.callback else {
  72. return
  73. }
  74. block(nil, nil, false, false)
  75. }
  76. @IBAction func buttonItemClicked_Extract(_ sender: AnyObject) {
  77. if let data = self._attribute?.fetchSelectPages().isEmpty, data {
  78. let alert = NSAlert()
  79. alert.alertStyle = .critical
  80. alert.messageText = String(format: "%@ %@", self._attribute?.filePath.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
  81. alert.runModal()
  82. return
  83. }
  84. if (self.deleteExtractButton.state == .on && self._pdfDocument!.pageCount == (self._attribute?.fetchSelectPages().count)!) {
  85. Task {
  86. _ = 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)))
  87. }
  88. return;
  89. }
  90. guard let block = self.callback else {
  91. return
  92. }
  93. block(self._pdfDocument, self._attribute, self._extractPerPageFlag, self.deleteExtractButton.state == .on)
  94. }
  95. @IBAction func buttonItemClicked_OnePDFPerPage(_ sender: AnyObject) {
  96. if (self.extractAsDocumentsPerPageButton.state == .on) {
  97. self._extractPerPageFlag = true
  98. } else {
  99. self._extractPerPageFlag = false
  100. }
  101. }
  102. }