KMNSplitPDFWindowController.swift 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. //
  2. // KMNSplitPDFWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/24.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNSplitPDFWindowController: KMNBaseWindowController {
  10. @IBOutlet var progress: NSProgressIndicator!
  11. @IBOutlet var titleLabel: NSTextField!
  12. @IBOutlet var methodLabel: NSTextField!
  13. @IBOutlet var averagePerRadio: ComponentRadio!
  14. @IBOutlet var averagePernInputNumber: ComponentInputNumber!
  15. @IBOutlet var averagePernLabel: NSTextField!
  16. @IBOutlet var averageSplitRadio: ComponentRadio!
  17. @IBOutlet var averageSplitInputNumber: ComponentInputNumber!
  18. @IBOutlet var averageSplitLabel: NSTextField!
  19. @IBOutlet var splitRangeRadio: ComponentRadio!
  20. @IBOutlet var splitRangeSelect: ComponentSelect!
  21. @IBOutlet var fileTitleLabel: NSTextField!
  22. @IBOutlet var fileNameBox: NSBox!
  23. @IBOutlet var fileNameLabel: NSTextField!
  24. @IBOutlet var precedeCheckBox: ComponentCheckBox!
  25. @IBOutlet var tagCheckBox: ComponentCheckBox!
  26. @IBOutlet var tagInput: ComponentInput!
  27. @IBOutlet var separatorCheckBox: ComponentCheckBox!
  28. @IBOutlet var separatorInput: ComponentInput!
  29. @IBOutlet var cancelButton: ComponentButton!
  30. @IBOutlet var splitButton: ComponentButton!
  31. @IBOutlet var splitRangeHeightButton:NSLayoutConstraint!
  32. @IBOutlet var cancelWidthButton:NSLayoutConstraint!
  33. @IBOutlet var splitWidthButton:NSLayoutConstraint!
  34. @IBOutlet var averagePerRadioWidthButton:NSLayoutConstraint!
  35. @IBOutlet var averageSplitRadioWidthButton:NSLayoutConstraint!
  36. @IBOutlet var splitRangeRadioWidthButton:NSLayoutConstraint!
  37. @IBOutlet var tagCheckWidthButton:NSLayoutConstraint!
  38. @IBOutlet var separatorCheckWidthButton:NSLayoutConstraint!
  39. private var orgDocument:CPDFDocument?
  40. private var selectionIndexPaths: Set<IndexPath> = []
  41. private func viewFileAtFinder(_ filePath: String) {
  42. let workspace = NSWorkspace.shared
  43. let url = URL(fileURLWithPath: filePath)
  44. workspace.activateFileViewerSelecting([url])
  45. }
  46. override func updateUIThemeColor() {
  47. super.updateUIThemeColor()
  48. window?.contentView?.wantsLayer = true
  49. window?.contentView?.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/popup").cgColor
  50. averagePerRadio.reloadData()
  51. averageSplitRadio.reloadData()
  52. splitRangeRadio.reloadData()
  53. precedeCheckBox.reloadData()
  54. tagCheckBox.reloadData()
  55. separatorCheckBox.reloadData()
  56. cancelButton.reloadData()
  57. splitButton.reloadData()
  58. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  59. methodLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  60. fileTitleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  61. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  62. fileNameBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/4")
  63. updateButton()
  64. }
  65. override func updateUILanguage() {
  66. super.updateUILanguage()
  67. titleLabel.stringValue = KMLocalizedString("Split")
  68. methodLabel.stringValue = KMLocalizedString("Split Method")
  69. fileTitleLabel.stringValue = KMLocalizedString("File Name")
  70. averagePerRadio.properties.text = KMLocalizedString("Split by every")
  71. averageSplitRadio.properties.text = KMLocalizedString("Split averagely to")
  72. splitRangeRadio.properties.text = KMLocalizedString("Split by page range")
  73. averagePerRadio.reloadData()
  74. averageSplitRadio.reloadData()
  75. splitRangeRadio.reloadData()
  76. averagePernLabel.stringValue = KMLocalizedString("page(s)")
  77. precedeCheckBox.properties.text = KMLocalizedString("Keep the current file name in front of labels")
  78. tagCheckBox.properties.text = KMLocalizedString("Label")
  79. separatorCheckBox.properties.text = KMLocalizedString("Separator")
  80. precedeCheckBox.reloadData()
  81. tagCheckBox.reloadData()
  82. separatorCheckBox.reloadData()
  83. setUpPageSelctProperty()
  84. cancelButton.properties.buttonText = KMLocalizedString("Cancel")
  85. splitButton.properties.buttonText = KMLocalizedString("Split")
  86. cancelButton.reloadData()
  87. splitButton.reloadData()
  88. averagePerRadioWidthButton.constant = averagePerRadio.properties.propertyInfo.viewWidth
  89. averageSplitRadioWidthButton.constant = averageSplitRadio.properties.propertyInfo.viewWidth
  90. splitRangeRadioWidthButton.constant = splitRangeRadio.properties.propertyInfo.viewWidth
  91. tagCheckWidthButton.constant = tagCheckBox.properties.propertyInfo.viewWidth
  92. separatorCheckWidthButton.constant = separatorCheckBox.properties.propertyInfo.viewWidth
  93. cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth
  94. splitWidthButton.constant = splitButton.properties.propertyInfo.viewWidth
  95. let pageRangeSelectIndex = splitRangeSelect.indexOfSelect()
  96. if pageRangeSelectIndex == 0 {
  97. splitRangeSelect.properties.text = KMLocalizedString("Odd Pages Only")
  98. } else {
  99. if(orgDocument?.pageCount ?? 0 > 1 && pageRangeSelectIndex == 1) {
  100. splitRangeSelect.properties.text = KMLocalizedString("Even Pages Only")
  101. }
  102. }
  103. }
  104. override func initContentView() {
  105. super.initContentView()
  106. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  107. methodLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  108. fileTitleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  109. averagePerRadio.properties = ComponentCheckBoxProperty(size: .s,
  110. state: .normal,
  111. isDisabled: false,
  112. showhelp: false,
  113. text: KMLocalizedString("Split by every"),
  114. checkboxType: .normal)
  115. averagePernLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  116. averageSplitRadio.properties = ComponentCheckBoxProperty(size: .s,
  117. state: .normal,
  118. isDisabled: false,
  119. showhelp: false,
  120. text: KMLocalizedString("Split averagely to"),
  121. checkboxType: .normal)
  122. averageSplitLabel.stringValue = KMLocalizedString("PDF files")
  123. averageSplitLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  124. fileNameLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  125. splitRangeRadio.properties = ComponentCheckBoxProperty(size: .s,
  126. state: .normal,
  127. isDisabled: false,
  128. showhelp: false,
  129. text: KMLocalizedString("Split by page range"),
  130. checkboxType: .selected)
  131. precedeCheckBox.properties = ComponentCheckBoxProperty(size: .s,
  132. state: .normal,
  133. isDisabled: false,
  134. showhelp: false,
  135. text: KMLocalizedString("Keep the current file name in front of labels"),
  136. checkboxType: .selected)
  137. tagCheckBox.properties = ComponentCheckBoxProperty(size: .s,
  138. state: .normal,
  139. isDisabled: false,
  140. showhelp: false,
  141. text: KMLocalizedString("Label"),
  142. checkboxType: .selected)
  143. separatorCheckBox.properties = ComponentCheckBoxProperty(size: .s,
  144. state: .normal,
  145. isDisabled: false,
  146. showhelp: false,
  147. text: KMLocalizedString("Separator"),
  148. checkboxType: .selected)
  149. averagePernInputNumber.properties = ComponentInputNumberProperty(alignment: .left,
  150. size: .s,
  151. state: .normal,
  152. isError: false,
  153. showErrorInfo: false,
  154. isDisabled: true,
  155. showPrefix: false,
  156. showSuffix: false,
  157. minSize: 1,
  158. maxSize: Int(orgDocument?.pageCount ?? 1),
  159. text:"1")
  160. averagePernInputNumber.delegate = self
  161. averageSplitInputNumber.properties = ComponentInputNumberProperty(alignment: .left,
  162. size: .s,
  163. state: .normal,
  164. isError: false,
  165. showErrorInfo: false,
  166. isDisabled: true,
  167. showPrefix: false,
  168. showSuffix: false,
  169. minSize: 1,
  170. maxSize: Int(orgDocument?.pageCount ?? 1),
  171. text: "1")
  172. averageSplitInputNumber.delegate = self
  173. let inputTagProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
  174. state:.pressed ,
  175. isError: false,
  176. showPrefix: false,
  177. showSuffix: false,
  178. showClear: false,
  179. isDisabled: false,
  180. placeholder: "",
  181. text: "Part")
  182. tagInput.properties = inputTagProperty
  183. tagInput.delegate = self
  184. let inputSeparatorProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
  185. state:.pressed ,
  186. isError: false,
  187. showPrefix: false,
  188. showSuffix: false,
  189. showClear: false,
  190. isDisabled: false,
  191. placeholder: "",
  192. text: "-")
  193. separatorInput.properties = inputSeparatorProperty
  194. separatorInput.delegate = self
  195. splitRangeSelect.properties = ComponentSelectProperties(size: .s,
  196. state: .normal,
  197. isDisabled: false,
  198. isError: false,
  199. leftIcon: false,
  200. placeholder: nil,
  201. errorText: nil,
  202. creatable: false,
  203. text: KMLocalizedString("Odd Pages Only", comment: ""))
  204. splitButton.properties = ComponentButtonProperty(type: .primary,
  205. size: .s,
  206. state: .normal,
  207. isDisable: false,
  208. buttonText: KMLocalizedString("Split"),keepPressState: false)
  209. splitButton.setTarget(self, action: #selector(splitButtonClicked(_ :)))
  210. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary,
  211. size: .s,
  212. state: .normal,
  213. buttonText: KMLocalizedString("Cancel"))
  214. cancelButton.setTarget(self, action: #selector(cancelButtonClicked(_ :)))
  215. averagePerRadio.setTarget(self, action: #selector(splitMethodClicked(_:)))
  216. averageSplitRadio.setTarget(self, action: #selector(splitMethodClicked(_:)))
  217. splitRangeRadio.setTarget(self, action: #selector(splitMethodClicked(_:)))
  218. precedeCheckBox.setTarget(self, action: #selector(changeNameClick(_:)))
  219. tagCheckBox.setTarget(self, action: #selector(changeNameClick(_:)))
  220. separatorCheckBox.setTarget(self, action: #selector(changeNameClick(_:)))
  221. setUpPageSelctProperty()
  222. splitRangeSelect.delegate = self
  223. }
  224. func updateButton() {
  225. if(averageSplitRadio.properties.checkboxType == .selected) {
  226. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  227. averageSplitLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  228. } else if (splitRangeRadio.properties.checkboxType == .selected) {
  229. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  230. averageSplitLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  231. } else if (averagePerRadio.properties.checkboxType == .selected) {
  232. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  233. averageSplitLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  234. }
  235. }
  236. private func setUpPageSelctProperty() {
  237. var menuItemString:[String] = [KMLocalizedString("Odd Pages Only"),
  238. KMLocalizedString("Even Pages Only"),
  239. KMLocalizedString("e.g. 1,3-5,10")]
  240. if orgDocument?.pageCount ?? 0 <= 1 {
  241. menuItemString.removeObject(KMLocalizedString("Even Pages Only"))
  242. }
  243. var menuItemArr: [ComponentMenuitemProperty] = []
  244. for language in menuItemString {
  245. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  246. itemSelected: false,
  247. isDisabled: false,
  248. keyEquivalent: nil,
  249. text: language)
  250. menuItemArr.append(itemProperty)
  251. }
  252. splitRangeSelect.updateMenuItemsArr(menuItemArr)
  253. }
  254. override func windowDidLoad() {
  255. super.windowDidLoad()
  256. var isEditIndex:Int = 2
  257. if(orgDocument?.pageCount ?? 0 <= 1) {
  258. isEditIndex = 1
  259. }
  260. splitRangeSelect.selectItemAtIndex(isEditIndex)
  261. splitRangeSelect.properties.creatable = true
  262. splitRangeSelect.properties.text =
  263. KMNTools.parseIndexPathsSet(indexSets: selectionIndexPaths)
  264. splitRangeSelect.reloadData()
  265. updateFileName()
  266. progress.isHidden = true
  267. }
  268. func updateFileName() {
  269. let pdfName = orgDocument?.documentURL.deletingPathExtension().path.lastPathComponent ?? ""
  270. var splitPartString = ""
  271. var separatorString = ""
  272. if (tagCheckBox.properties.checkboxType == .selected) {
  273. splitPartString = tagInput.properties.text
  274. }
  275. if (separatorCheckBox.properties.checkboxType == .selected) {
  276. separatorString = separatorInput.properties.text
  277. }
  278. var partName = ""
  279. if (precedeCheckBox.properties.checkboxType == .selected) {
  280. if (separatorString.isEmpty == false) {
  281. if (partName.isEmpty == false) {
  282. partName.append(separatorString)
  283. } else {
  284. partName = separatorString
  285. }
  286. }
  287. if (splitPartString.isEmpty == false) {
  288. if (partName.isEmpty == false) {
  289. partName.append(splitPartString)
  290. } else {
  291. partName = splitPartString
  292. }
  293. }
  294. fileNameLabel.stringValue = String(format: "%@%@.pdf", pdfName, partName)
  295. } else {
  296. if (splitPartString.isEmpty == false) {
  297. if (partName.isEmpty == false) {
  298. partName.append(splitPartString)
  299. } else {
  300. partName = splitPartString
  301. }
  302. }
  303. if (separatorString.isEmpty == false) {
  304. if (partName.isEmpty == false) {
  305. partName.append(separatorString)
  306. } else {
  307. partName = separatorString
  308. }
  309. }
  310. fileNameLabel.stringValue = String(format: "%@%@.pdf", partName, pdfName)
  311. }
  312. }
  313. convenience init(_ document: CPDFDocument?, selectionIndexPaths: Set<IndexPath>) {
  314. self.init(windowNibName: "KMNSplitPDFWindowController")
  315. orgDocument = document
  316. self.selectionIndexPaths = selectionIndexPaths // 修正此行
  317. }
  318. convenience init(_ filePath: String,password:String?) {
  319. self.init(windowNibName: "KMNSplitPDFWindowController")
  320. let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
  321. if password != nil {
  322. document?.unlock(withPassword: password as String?)
  323. }
  324. orgDocument = document
  325. selectionIndexPaths = [IndexPath(item: 0, section: 0)] // 修正此行
  326. }
  327. //MARK: - Action
  328. @objc func cancelButtonClicked(_ sender: NSView) {
  329. own_closeEndSheet()
  330. }
  331. @objc func splitButtonClicked(_ sender: NSView) {
  332. let pageRangeSelectIndex = splitRangeSelect.indexOfSelect()
  333. let fileAttribute = KMNFileAttribute()
  334. if(splitRangeRadio.properties.checkboxType == .selected) {
  335. fileAttribute.password = orgDocument?.password ?? ""
  336. fileAttribute.pdfDocument = orgDocument
  337. fileAttribute.filePath = orgDocument?.documentURL.path ?? ""
  338. if(pageRangeSelectIndex == 0) {
  339. fileAttribute.bAllPage = false
  340. fileAttribute.pagesType = .OnlyOdd
  341. } else {
  342. fileAttribute.bAllPage = false
  343. if(orgDocument?.pageCount ?? 0 <= 1) {
  344. fileAttribute.pagesType = .PagesString
  345. fileAttribute.pagesString = splitRangeSelect.properties.text ?? ""
  346. } else {
  347. if(pageRangeSelectIndex == 1) {
  348. fileAttribute.pagesType = .OnlyEven
  349. } else {
  350. fileAttribute.pagesType = .PagesString
  351. fileAttribute.pagesString = splitRangeSelect.properties.text ?? ""
  352. }
  353. }
  354. }
  355. if (fileAttribute.fetchSelectPages().isEmpty) {
  356. splitRangeSelect.properties.isError = true
  357. splitRangeSelect.properties.errorText = KMLocalizedString("Invalid page range or the page number is out of range. Please try again.")
  358. splitRangeSelect.reloadData()
  359. splitRangeHeightButton.constant = 50
  360. return
  361. }
  362. }
  363. let panel = NSOpenPanel()
  364. panel.canChooseFiles = false
  365. panel.canChooseDirectories = true
  366. panel.canCreateDirectories = true
  367. panel.beginSheetModal(for: self.window!) { result in
  368. if (result == .OK) {
  369. let outputURL = panel.urls.first
  370. var tFolderPath = outputURL!.path
  371. var index = 0
  372. let folderName = self.fileNameLabel.stringValue.deletingPathExtension.lastPathComponent
  373. var folderPath = "\(tFolderPath)" + "/" + "\(folderName)"
  374. //创建目录
  375. while FileManager.default.fileExists(atPath: folderPath) {
  376. index += 1
  377. folderPath = "\(tFolderPath)" + "/" + "\(folderName)_\(index)"
  378. }
  379. tFolderPath = folderPath
  380. try?FileManager.default.createDirectory(atPath: tFolderPath, withIntermediateDirectories: true)
  381. self.progress.isHidden = false
  382. self.cancelButton.properties.isDisabled = true
  383. self.splitButton.properties.isDisabled = true
  384. self.cancelButton.reloadData()
  385. self.splitButton.reloadData()
  386. self.progress.startAnimation(nil)
  387. if(self.averagePerRadio.properties.checkboxType == .selected) {
  388. let index = Int(self.averagePernInputNumber.properties.text ?? "1")
  389. DispatchQueue.global().async {
  390. let successArray = self.orgDocument?.splitByPagesWith(index ?? 1, folerPath: tFolderPath, fileName: folderName) ?? []
  391. DispatchQueue.main.async {
  392. self.progress.isHidden = true
  393. self.cancelButton.properties.isDisabled = false
  394. self.splitButton.properties.isDisabled = false
  395. self.cancelButton.reloadData()
  396. self.splitButton.reloadData()
  397. if (successArray.isEmpty == false) {
  398. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Splitting completed. Tap 'OK' to open the output folder."), buttons: [KMLocalizedString("OK")])
  399. if (response == .alertFirstButtonReturn ) {
  400. self.own_closeEndSheet()
  401. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  402. let filePath = successArray.first ?? ""
  403. self.viewFileAtFinder(filePath)
  404. }
  405. }
  406. } else {
  407. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Failed to split!"))
  408. if (response == .alertFirstButtonReturn) {
  409. self.own_closeEndSheet()
  410. }
  411. }
  412. }
  413. }
  414. } else if(self.averageSplitRadio.properties.checkboxType == .selected) {
  415. let dex = Int(self.averageSplitInputNumber.properties.text ?? "1")
  416. DispatchQueue.global().async {
  417. let successArray = self.orgDocument?.splitByFileWith(dex ?? 1, folerPath: tFolderPath, fileName: folderName) ?? []
  418. DispatchQueue.main.async {
  419. self.progress.isHidden = true
  420. self.cancelButton.properties.isDisabled = false
  421. self.splitButton.properties.isDisabled = false
  422. self.cancelButton.reloadData()
  423. self.splitButton.reloadData()
  424. if (successArray.isEmpty == false) {
  425. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Splitting completed. Tap 'OK' to open the output folder."), buttons: [KMLocalizedString("OK")])
  426. if (response == .alertFirstButtonReturn) {
  427. self.own_closeEndSheet()
  428. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  429. let filePath = successArray.first ?? ""
  430. self.viewFileAtFinder(filePath)
  431. }
  432. }
  433. }else{
  434. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Failed to split!"))
  435. if (response == .alertFirstButtonReturn) {
  436. self.own_closeEndSheet()
  437. }
  438. }
  439. }
  440. }
  441. } else {
  442. var isSuccessfully:Bool = false
  443. DispatchQueue.global().async { [self] in
  444. var pdfDocument1 = CPDFDocument()
  445. var pdfDocument2 = CPDFDocument()
  446. let pageCnt = self.orgDocument?.pageCount ?? 0
  447. let pages = fileAttribute.fetchSelectPages()
  448. for i in 0 ..< pageCnt {
  449. var isSelected = false
  450. for number in pages {
  451. if number == i+1 {
  452. isSelected = true
  453. if let page = self.orgDocument?.page(at: i) as? CPDFPage {
  454. pdfDocument1?.insertPageObject(page, at: pdfDocument1!.pageCount)
  455. }
  456. }
  457. }
  458. if (!isSelected) {
  459. if let page = self.orgDocument?.page(at: i) as? CPDFPage {
  460. pdfDocument2?.insertPageObject(page, at: pdfDocument2!.pageCount)
  461. }
  462. }
  463. }
  464. if (pdfDocument1!.pageCount > 0) {
  465. let tpath = "\(tFolderPath)/\(folderName) 1.pdf"
  466. isSuccessfully = pdfDocument1?.write(toFile:tpath) ?? false
  467. }
  468. if (pdfDocument2!.pageCount > 0) {
  469. let tPath = "\(tFolderPath)/\(folderName) 2.pdf"
  470. pdfDocument2?.write(toFile: tPath)
  471. }
  472. DispatchQueue.main.async {
  473. self.progress.isHidden = true
  474. self.cancelButton.properties.isDisabled = false
  475. self.splitButton.properties.isDisabled = false
  476. self.cancelButton.reloadData()
  477. self.splitButton.reloadData()
  478. if (isSuccessfully) {
  479. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Splitting completed. Tap 'OK' to open the output folder."), buttons: [KMLocalizedString("OK")])
  480. if (response == .alertFirstButtonReturn) {
  481. self.own_closeEndSheet()
  482. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  483. let filePath = "\(tFolderPath)/\(folderName) 1.pdf"
  484. self.viewFileAtFinder(filePath)
  485. }
  486. }
  487. } else {
  488. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Failed to split!"))
  489. if (response == .alertFirstButtonReturn) {
  490. self.own_closeEndSheet()
  491. }
  492. }
  493. }
  494. }
  495. }
  496. }
  497. }
  498. }
  499. @objc func changeNameClick(_ sender: NSView){
  500. updateFileName()
  501. }
  502. @objc func splitMethodClicked(_ sender: NSView){
  503. var oldSelectRadioBtn:ComponentRadio? = nil
  504. if sender == averagePerRadio {
  505. if(averageSplitRadio.properties.checkboxType == .selected) {
  506. oldSelectRadioBtn = averageSplitRadio
  507. averageSplitInputNumber.properties.isDisabled = true
  508. averageSplitLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  509. averageSplitInputNumber.reloadData()
  510. } else if (splitRangeRadio.properties.checkboxType == .selected){
  511. oldSelectRadioBtn = splitRangeRadio
  512. splitRangeSelect.properties.isDisabled = true
  513. splitRangeSelect.reloadData()
  514. }
  515. averagePernInputNumber.properties.isDisabled = false
  516. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  517. averagePernInputNumber.reloadData()
  518. } else if sender == averageSplitRadio {
  519. if(averagePerRadio.properties.checkboxType == .selected) {
  520. oldSelectRadioBtn = averagePerRadio
  521. averagePernInputNumber.properties.isDisabled = true
  522. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  523. averagePernInputNumber.reloadData()
  524. } else if (splitRangeRadio.properties.checkboxType == .selected){
  525. oldSelectRadioBtn = splitRangeRadio
  526. splitRangeSelect.properties.isDisabled = true
  527. splitRangeSelect.reloadData()
  528. }
  529. averageSplitInputNumber.properties.isDisabled = false
  530. averageSplitLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  531. averageSplitInputNumber.reloadData()
  532. } else if sender == splitRangeRadio {
  533. if(averagePerRadio.properties.checkboxType == .selected) {
  534. oldSelectRadioBtn = averagePerRadio
  535. averagePernInputNumber.properties.isDisabled = true
  536. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  537. averagePernInputNumber.reloadData()
  538. } else if (averageSplitRadio.properties.checkboxType == .selected){
  539. oldSelectRadioBtn = averageSplitRadio
  540. averageSplitInputNumber.properties.isDisabled = true
  541. averageSplitLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  542. averageSplitInputNumber.reloadData()
  543. }
  544. splitRangeSelect.properties.isDisabled = false
  545. splitRangeSelect.reloadData()
  546. }
  547. if(oldSelectRadioBtn != nil) {
  548. oldSelectRadioBtn?.properties.checkboxType = .normal
  549. oldSelectRadioBtn?.reloadData()
  550. }
  551. }
  552. }
  553. //MARK: - ComponentInputNumberDelegate
  554. extension KMNSplitPDFWindowController: ComponentInputNumberDelegate {
  555. func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
  556. }
  557. }
  558. //MARK: - ComponentSelectDelegate
  559. extension KMNSplitPDFWindowController: ComponentSelectDelegate {
  560. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  561. if(view == splitRangeSelect) {
  562. let pageRangeSelectIndex = splitRangeSelect.indexOfSelect()
  563. var isEditIndex:Int = 2
  564. if(orgDocument?.pageCount ?? 0 <= 1) {
  565. isEditIndex = 1
  566. }
  567. if (pageRangeSelectIndex == isEditIndex) {
  568. splitRangeSelect.properties.text = ""
  569. splitRangeSelect.properties.placeholder = KMLocalizedString("e.g. 1,3-5,10")
  570. splitRangeSelect.properties.creatable = true
  571. } else {
  572. splitRangeSelect.properties.placeholder = ""
  573. splitRangeSelect.properties.creatable = false
  574. }
  575. splitRangeSelect.reloadData()
  576. }
  577. }
  578. func componentSelectTextDidChange(_ view: ComponentSelect) {
  579. if splitRangeSelect.properties.isError == true {
  580. splitRangeSelect.properties.isError = false
  581. splitRangeSelect.reloadData()
  582. splitRangeHeightButton.constant = 32
  583. }
  584. }
  585. }
  586. //MARK: - ComponentInputDelegate
  587. extension KMNSplitPDFWindowController: ComponentInputDelegate {
  588. func componentInputDidEndEditing(inputView: ComponentInput) {
  589. }
  590. func componentInputDidChanged(inputView: ComponentInput) {
  591. updateFileName()
  592. }
  593. }