KMPDFEditExtractWindow.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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?.pageCnt = Int(document.pageCount)
  24. if (pageString.isEmpty == false) {
  25. self._selectedPagesName = pageString
  26. self._attribute?.bAllPage = false
  27. self._attribute?.pagesType = .custom
  28. self._attribute?.pagesString = pageString
  29. } else {
  30. self._attribute?.bAllPage = true
  31. self._attribute?.pagesType = .all
  32. }
  33. self._attribute?.filePath = document.documentURL.path
  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")
  56. self.window?.title = KMLocalizedString("Extract")
  57. self.cancelButton.title = KMLocalizedString("Cancel")
  58. self.extractButton.title = KMLocalizedString("Extract")
  59. self.deleteExtractButton.title = KMLocalizedString("Delete pages after extraction")
  60. self.deleteExtractButton.wantsLayer = true
  61. self.extractAsDocumentsPerPageButton.wantsLayer = true
  62. }
  63. @IBAction func buttonItemClicked_Cancel(_ sender: AnyObject) {
  64. self._pdfDocument = nil;
  65. guard let block = self.callback else {
  66. return
  67. }
  68. block(nil, nil, false, false)
  69. }
  70. @IBAction func buttonItemClicked_Extract(_ sender: AnyObject) {
  71. if let data = self._attribute?.fetchSelectPages().isEmpty, data {
  72. Task {
  73. _ = 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.")))
  74. }
  75. return
  76. }
  77. let pageCnt = self._pdfDocument?.pageCount ?? 0
  78. let pages = self._attribute?.fetchSelectPages() ?? []
  79. if (self.deleteExtractButton.state == .on && pageCnt == pages.count) {
  80. Task {
  81. _ = 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.")))
  82. }
  83. return
  84. }
  85. guard let block = self.callback else {
  86. return
  87. }
  88. block(self._pdfDocument, self._attribute, self._extractPerPageFlag, self.deleteExtractButton.state == .on)
  89. }
  90. @IBAction func buttonItemClicked_OnePDFPerPage(_ sender: AnyObject) {
  91. if (self.extractAsDocumentsPerPageButton.state == .on) {
  92. self._extractPerPageFlag = true
  93. } else {
  94. self._extractPerPageFlag = false
  95. }
  96. }
  97. }