KMBatchOperateImageToPDFViewController.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. //
  2. // KMBatchOperateImageToPDFViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/12/5.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMBatchOperateImageToPDFViewController: KMBatchOperateBaseViewController {
  10. @IBOutlet weak var headerBox: NSBox!
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var headerBottomLine: ComponentDivider!
  13. @IBOutlet var outputTypeLabel: NSTextField!
  14. @IBOutlet var ocrLabel: NSTextField!
  15. @IBOutlet weak var newPDFBox: NSBox!
  16. @IBOutlet weak var mergeAllBox: NSBox!
  17. @IBOutlet weak var appendBox: NSBox!
  18. @IBOutlet weak var appendBackBox: NSBox!
  19. @IBOutlet weak var ocrSelectBox: NSBox!
  20. @IBOutlet weak var languageBox: NSBox!
  21. @IBOutlet weak var ocrPlanBox: NSBox!
  22. @IBOutlet weak var ocrPlan2Box: NSBox!
  23. @IBOutlet weak var hLine: NSView!
  24. @IBOutlet weak var extractTextBox: NSBox!
  25. @IBOutlet weak var bottomBox: NSBox!
  26. @IBOutlet weak var bottomTopLine: ComponentDivider!
  27. @IBOutlet weak var applyButton: ComponentButton!
  28. var password: String = ""
  29. lazy var method: KMImageToPDFMethod = {
  30. let method = KMImageToPDFMethod()
  31. method.imageTopdfDelegate = self
  32. return method
  33. }()
  34. private lazy var topLine: NSView = {
  35. let view = NSView()
  36. view.wantsLayer = true
  37. return view
  38. }()
  39. private var createNewPDFRadio_: ComponentRadio = {
  40. let view = ComponentRadio()
  41. view.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: false, text: KMLocalizedString("New PDF Document", comment: ""), checkboxType: .normal)
  42. return view
  43. }()
  44. private lazy var mergeAllCheckBox_: ComponentCheckBox = {
  45. let view = ComponentCheckBox()
  46. view.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: false, text: KMLocalizedString("Merge All", comment: ""),checkboxType: .normal)
  47. return view
  48. }()
  49. private var appendRadio_: ComponentRadio = {
  50. let view = ComponentRadio()
  51. view.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: false, text: KMLocalizedString("Append To Existing File", comment: ""), checkboxType: .normal)
  52. return view
  53. }()
  54. private var appendBackInput: ComponentInputWithAddon = {
  55. let view = ComponentInputWithAddon()
  56. view.properties = ComponentInputWithAddonProperty(size: .s, isDisabled: true, addonType: .button)
  57. return view
  58. }()
  59. private lazy var appendBackInput_: ComponentInput = {
  60. let view = ComponentInput()
  61. return view
  62. }()
  63. private lazy var appendBackButtonInput_: ComponentInputAddon = {
  64. let view = ComponentInputAddon()
  65. return view
  66. }()
  67. private lazy var ocrCheckBox_: ComponentCheckBox = {
  68. let view = ComponentCheckBox()
  69. view.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: false, text: KMLocalizedString("Recognize text if needed", comment: ""), checkboxType: .normal)
  70. return view
  71. }()
  72. private lazy var languageView_: ComponentSelect = {
  73. let view = ComponentSelect()
  74. view.properties = ComponentSelectProperties(size: .s, state: .normal, isDisabled: false, isError: false, leftIcon: false, placeholder: nil, errorText: nil, creatable: false, text: KMLocalizedString("", comment: ""))
  75. view.delegate = self
  76. return view
  77. }()
  78. private var ocrPlanRadio_: ComponentRadio = {
  79. let view = ComponentRadio()
  80. view.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: true, text: KMLocalizedString("OCR Plan 1-Online", comment: ""), checkboxType: .normal)
  81. view.toolTip = KMLocalizedString("The OCR service works via an internet connection. We would suggest you to perform OCR using a VPN connection while the service is limited.")
  82. return view
  83. }()
  84. private var ocrPlanRadio2_: ComponentRadio = {
  85. let view = ComponentRadio()
  86. view.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: true, text: KMLocalizedString("OCR Plan 2-Online", comment: ""), checkboxType: .normal)
  87. view.toolTip = KMLocalizedString("The OCR service works via an internet connection. We would suggest you to perform OCR using a VPN connection while the service is limited.")
  88. return view
  89. }()
  90. private var vLine_: ComponentDivider = {
  91. let view = ComponentDivider()
  92. view.properties = ComponentDividerProperty(type: .horizontal, dash: false)
  93. return view
  94. }()
  95. private lazy var extractTextCheckBox_: ComponentCheckBox = {
  96. let view = ComponentCheckBox()
  97. view.properties = ComponentCheckBoxProperty(size: .s, state: .normal, isDisabled: false, showhelp: false, text: KMLocalizedString("Extract Text (.txt)", comment: ""), checkboxType: .normal)
  98. return view
  99. }()
  100. override var interfaceStatus: KMBatchOperateInterfaceStatus?{
  101. set{
  102. super.interfaceStatus = newValue
  103. if newValue == .PrepareProcess {
  104. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  105. let files = NSMutableArray()
  106. for url in self.successFilePathURLArray! {
  107. if FileManager.default.fileExists(atPath: url.path) {
  108. files.add(url)
  109. }
  110. }
  111. if files.count > 0 {
  112. let workspace = NSWorkspace.shared
  113. workspace.activateFileViewerSelecting(files as! [URL])
  114. }
  115. }
  116. self._updateActionButtonEnable(true)
  117. } else {
  118. self._updateActionButtonEnable(false)
  119. }
  120. }
  121. get{
  122. return super.interfaceStatus
  123. }
  124. }
  125. private func _updateActionButtonEnable(_ enabled: Bool) {
  126. applyButton.properties.isDisabled = !enabled
  127. applyButton.reloadData()
  128. }
  129. override func viewDidLoad() {
  130. super.viewDidLoad()
  131. headerBox.borderWidth = 0
  132. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  133. createNewPDFRadio_.properties.checkboxType = .selected
  134. createNewPDFRadio_.reloadData()
  135. let color = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorBorder-dis")
  136. appendBackBox.borderColor = color
  137. let inputWithAddonProperty = ComponentInputProperty(size: .s,
  138. state: .normal,
  139. isError: false,
  140. showPrefix: false,
  141. showSuffix: false,
  142. showClear: false,
  143. isDisabled: true,
  144. placeholder: KMLocalizedString("Select File..."),
  145. text: "",
  146. creatable: false)
  147. appendBackInput_.properties = inputWithAddonProperty
  148. appendBackInput_.properties.propertyInfo.cornerRadius_topRight = 0
  149. appendBackInput_.properties.propertyInfo.cornerRadius_bottomRight = 0
  150. appendBackInput_.properties.isDisabled = true
  151. appendBackInput_.reloadData()
  152. appendBackButtonInput_.properties = ComponentInputAddonProperty(size: .s,
  153. state: .normal,
  154. addOnBefore: false,
  155. onlyRead: false,
  156. addonType: .imageWithColor,
  157. iconImage: NSImage(named: "KMFolderIcon"))
  158. appendBackButtonInput_.properties.isDisabled = true
  159. appendBackButtonInput_.reloadData()
  160. appendBackButtonInput_.setTarget(self, action: #selector(buttonItemClicked_AppendOtherPDF))
  161. appendBackBox.contentView?.addSubview(appendBackInput_)
  162. appendBackBox.contentView?.addSubview(appendBackButtonInput_)
  163. let boxH: CGFloat = 30
  164. appendBackInput_.frame = NSMakeRect(0, 0, 204, boxH)
  165. appendBackButtonInput_.frame = NSMakeRect(NSMaxX(appendBackInput_.frame), 0, boxH+2, boxH)
  166. let hView = NSView()
  167. appendBackBox.contentView?.addSubview(hView)
  168. hView.frame = NSMakeRect(NSMaxX(appendBackInput_.frame), 0, 1, 32)
  169. hView.wantsLayer = true
  170. hView.layer?.backgroundColor = color.cgColor
  171. ocrLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  172. ocrSelectBox.borderWidth = 0
  173. ocrSelectBox.contentView = ocrCheckBox_
  174. ocrCheckBox_.setTarget(self, action: #selector(buttonClicked_OCRSelect))
  175. languageView_.properties.isDisabled = true
  176. languageView_.reloadData()
  177. ocrPlanRadio_.setTarget(self, action: #selector(ocrPlanAction))
  178. ocrPlanRadio_.properties.isDisabled = true
  179. ocrPlanRadio_.properties.checkboxType = .selected
  180. ocrPlanRadio_.reloadData()
  181. ocrPlanRadio2_.setTarget(self, action: #selector(ocrPlanAction))
  182. ocrPlanRadio2_.properties.isDisabled = true
  183. ocrPlanRadio2_.reloadData()
  184. extractTextCheckBox_.properties.isDisabled = true
  185. extractTextCheckBox_.reloadData()
  186. bottomBox.borderWidth = 0
  187. applyButton.properties = ComponentButtonProperty(type: .primary, size: .m, onlyIcon: false, buttonText: KMLocalizedString("Apply"))
  188. applyButton.setTarget(self, action: #selector(buttonClicked_ImageToPDF))
  189. view.addSubview(topLine)
  190. topLine.km_add_top_constraint()
  191. topLine.km_add_height_constraint(constant: 1)
  192. topLine.km_add_left_constraint()
  193. topLine.km_add_right_constraint()
  194. self.localizedLanguage()
  195. self.configuUI()
  196. if let cnt = self.files?.count, cnt > 0 {
  197. self._updateActionButtonEnable(true)
  198. } else {
  199. self._updateActionButtonEnable(false)
  200. }
  201. }
  202. override func addNotifations() {
  203. super.addNotifations()
  204. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: NSNotification.Name("KMBatchFilesCountNotification"), object: nil)
  205. }
  206. override func updateUILanguage() {
  207. super.updateUILanguage()
  208. KMMainThreadExecute {
  209. self.titleLabel.stringValue = KMLocalizedString("Image to PDF")
  210. self.ocrLabel.stringValue = KMLocalizedString("Text Recognition Settings")
  211. }
  212. }
  213. override func updateUIThemeColor() {
  214. super.updateUIThemeColor()
  215. KMMainThreadExecute {
  216. self.topLine.layer?.backgroundColor = KMNColorTools.colorBorder_divider().cgColor
  217. let titleColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  218. self.titleLabel.textColor = titleColor
  219. self.headerBottomLine.properties = ComponentDividerProperty()
  220. self.ocrLabel.textColor = titleColor
  221. self.bottomTopLine.properties = ComponentDividerProperty()
  222. self.updateViewColor()
  223. }
  224. }
  225. func localizedLanguage() {
  226. self.outputTypeLabel.stringValue = KMLocalizedString("Output")
  227. let languages = KMGOCRManager.languages() as NSArray
  228. let strings = languages.value(forKeyPath: KMGOCRLanguageStringKey) as? [String]
  229. var menuItemArr: [ComponentMenuitemProperty] = []
  230. for language in strings ?? [] {
  231. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  232. itemSelected: false,
  233. isDisabled: false,
  234. keyEquivalent: nil,
  235. text: language)
  236. menuItemArr.append(itemProperty)
  237. }
  238. languageView_.updateMenuItemsArr(menuItemArr)
  239. languageView_.selectItemAtIndex(0)
  240. languageView_.properties.creatable = false
  241. languageView_.properties.text = menuItemArr.first?.text
  242. languageView_.reloadData()
  243. self.updateLanguageButton((KMGOCRManager.default().selectedLanguages?.value(forKeyPath: KMGOCRLanguageStringKey) as? [String]))
  244. self.OCRSelectedPlanChangeAction()
  245. }
  246. func configuUI() {
  247. self.view.wantsLayer = true
  248. newPDFBox.borderWidth = 0
  249. newPDFBox.contentView = createNewPDFRadio_
  250. createNewPDFRadio_.setTarget(self, action: #selector(buttonClicked_CreateNewPDF))
  251. mergeAllBox.borderWidth = 0
  252. mergeAllBox.contentView = mergeAllCheckBox_
  253. appendBox.borderWidth = 0
  254. appendBox.contentView = appendRadio_
  255. appendRadio_.setTarget(self, action: #selector(buttonClicked_AppendOtherPDF))
  256. languageBox.borderWidth = 0
  257. languageBox.contentView = languageView_
  258. ocrPlanBox.borderWidth = 0
  259. ocrPlanBox.contentView = ocrPlanRadio_
  260. ocrPlan2Box.borderWidth = 0
  261. ocrPlan2Box.contentView = ocrPlanRadio2_
  262. hLine.addSubview(vLine_)
  263. vLine_.frame = hLine.bounds
  264. vLine_.autoresizingMask = [.width, .height]
  265. extractTextBox.borderWidth = 0
  266. extractTextBox.contentView = extractTextCheckBox_
  267. }
  268. func updateViewColor() {
  269. self.view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg-layout-middle").cgColor
  270. }
  271. func updateLanguageButton(_ languages: [String]?) {
  272. if languages?.count ?? 0 < 1 {
  273. self.languageView_.properties.text = " " + KMLocalizedString("Auto Detection")
  274. self.languageView_.reloadData()
  275. return
  276. }
  277. var languageName: String = ""
  278. if languages?.count ?? 0 > 0 {
  279. for i in 0..<(languages?.count ?? 0) {
  280. let language = languages?[i]
  281. if i == 0 {
  282. languageName = language ?? ""
  283. } else {
  284. languageName = languageName.appendingFormat(",%@", language ?? "")
  285. }
  286. }
  287. } else {
  288. languageName = ""
  289. }
  290. self.languageView_.properties.text = " " + languageName
  291. self.languageView_.reloadData()
  292. }
  293. func converArrType(arr: Array<KMBatchOperateFile>, keyString: String) -> [String] {
  294. let newArr = NSMutableArray()
  295. for item in arr {
  296. newArr.add(item.filePath)
  297. }
  298. return newArr as! [String]
  299. }
  300. func isConnectionAvailable() -> Bool {
  301. if Reachability.forInternetConnection().currentReachabilityStatus().rawValue == 0 {
  302. return false
  303. }
  304. return true
  305. }
  306. func beginImageToPDF() {
  307. if self.files?.count ?? 0 < 1 {
  308. return
  309. }
  310. let photoArray = converArrType(arr: self.files!, keyString: "")
  311. var path: String = ""
  312. var isMerge = false
  313. var isCreatNewPDF = false
  314. var isOCR = false
  315. if ocrCheckBox_.properties.checkboxType == .selected {
  316. isOCR = true
  317. }
  318. var isSaveAs = false
  319. if extractTextCheckBox_.properties.checkboxType == .selected {
  320. isSaveAs = true
  321. }
  322. let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey")
  323. if isOCR && !self.isConnectionAvailable() && plan == 0 {
  324. let alert = NSAlert()
  325. alert.alertStyle = .critical
  326. alert.messageText = KMLocalizedString("Connection Error", comment: "")
  327. alert.informativeText = KMLocalizedString("Please make sure your internet connection is available.", comment: "")
  328. alert.addButton(withTitle: KMLocalizedString("OK", comment: ""))
  329. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  330. alert.beginSheetModal(for: self.view.window!, completionHandler: nil)
  331. } else {
  332. alert.runModal()
  333. }
  334. return
  335. }
  336. if createNewPDFRadio_.properties.checkboxType == .selected {
  337. if (self.choosePath.count < 1) {
  338. let alert = NSAlert()
  339. alert.alertStyle = .critical
  340. alert.messageText = String(format: KMLocalizedString("Output Folder cannot be empty.", comment: ""))
  341. alert.runModal()
  342. return
  343. }
  344. path = self.choosePath
  345. if mergeAllCheckBox_.properties.checkboxType == .selected {
  346. isMerge = true
  347. }
  348. isCreatNewPDF = true
  349. } else {
  350. let appenString = appendBackInput_.properties.text
  351. if appenString.isEmpty {
  352. let alert = NSAlert()
  353. alert.alertStyle = .critical
  354. alert.messageText = String(format: KMLocalizedString("Select a File", comment: ""))
  355. alert.runModal()
  356. return
  357. }
  358. path = appenString
  359. isMerge = true
  360. isCreatNewPDF = false
  361. }
  362. let planDisabled = ocrPlanRadio_.properties.isDisabled
  363. ocrPlanRadio_.properties.isDisabled = true
  364. ocrPlanRadio_.reloadData()
  365. ocrPlanRadio2_.properties.isDisabled = true
  366. ocrPlanRadio2_.reloadData()
  367. self.method.password = self.password
  368. self.interfaceStatus = .Processing
  369. self.method.saveAsTestPath = self.choosePath
  370. self.method.exportPDFFile(fileArray: photoArray, savePath: path, isOCR: isOCR, isCreatPDF: isCreatNewPDF, isMerge: isMerge, isSaveAsText: isSaveAs) { [weak self] savePath, errorArr, errorOCRArray in
  371. self?.ocrPlanRadio_.properties.isDisabled = planDisabled
  372. self?.ocrPlanRadio_.reloadData()
  373. self?.ocrPlanRadio2_.properties.isDisabled = planDisabled
  374. self?.ocrPlanRadio2_.reloadData()
  375. self?.applyButton.properties.isDisabled = false
  376. self?.applyButton.properties.state = .normal
  377. self?.applyButton.reloadData()
  378. self?.interfaceStatus = .PrepareProcess
  379. if errorArr.count > 0 {
  380. let dict: [String: Any] = ["isMerge": false, "isSuccess": false]
  381. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMBatchOperateImageToPDFSuccessNotification"), object: self, userInfo: dict)
  382. let alert = NSAlert()
  383. alert.messageText = KMLocalizedString("Conversion Failed", comment: "")
  384. alert.addButton(withTitle: KMLocalizedString("OK", comment: ""))
  385. alert.informativeText = "\(errorArr)"
  386. alert.alertStyle = .informational
  387. alert.runModal()
  388. } else {
  389. if errorOCRArray.count > 0 {
  390. var contextString = KMLocalizedString("Some problems occurred during the last operation:", comment: "")
  391. for filePath in errorOCRArray {
  392. contextString += "\n" + (filePath as AnyObject).lastPathComponent
  393. }
  394. let alert = NSAlert()
  395. alert.messageText = KMLocalizedString("Converted Successfully", comment: "")
  396. alert.addButton(withTitle: KMLocalizedString("OK", comment: ""))
  397. alert.informativeText = contextString
  398. alert.alertStyle = .informational
  399. let response = alert.runModal()
  400. if response == .OK {
  401. self?.viewFileAtFinder(fileName: savePath)
  402. }
  403. } else {
  404. self?.viewFileAtFinder(fileName: savePath)
  405. }
  406. }
  407. }
  408. }
  409. func viewFileAtFinder(fileName: String) {
  410. let dict = ["isMerge": true, "isSuccess": true]
  411. NotificationCenter.default.post(name: Notification.Name("KMBatchOperateImageToPDFSuccessNotification"), object: self, userInfo: dict)
  412. let workspace = NSWorkspace.shared
  413. let url = URL(fileURLWithPath: fileName)
  414. workspace.activateFileViewerSelecting([url])
  415. }
  416. // MARK: - Actions
  417. @objc func ocrPlanAction(_ sender: ComponentRadio) {
  418. for radio in [ocrPlanRadio_, ocrPlanRadio2_] {
  419. if radio.isEqual(to: sender) {
  420. radio.properties.checkboxType = .selected
  421. radio.reloadData()
  422. KMGOCRManager.default().ocrType = .google
  423. KMDataManager.ud_set(0, forKey: "KMOCRCurrentPlanKey")
  424. } else {
  425. radio.properties.checkboxType = .normal
  426. radio.reloadData()
  427. KMGOCRManager.default().ocrType = .apple
  428. KMDataManager.ud_set(1, forKey: "KMOCRCurrentPlanKey")
  429. }
  430. }
  431. self.OCRSelectedPlanChangeAction()
  432. }
  433. @IBAction func buttonClicked_CreateNewPDF(_ sender: ComponentRadio) {
  434. if (sender.properties.checkboxType == .selected) {
  435. mergeAllCheckBox_.properties.isDisabled = false
  436. mergeAllCheckBox_.reloadData()
  437. appendRadio_.properties.checkboxType = .normal
  438. appendRadio_.reloadData()
  439. appendBackInput_.properties.isDisabled = true
  440. appendBackInput_.reloadData()
  441. appendBackButtonInput_.properties.isDisabled = true
  442. appendBackButtonInput_.reloadData()
  443. }
  444. }
  445. @IBAction func buttonClicked_AppendOtherPDF(_ sender: ComponentRadio) {
  446. if (sender.properties.checkboxType == .selected) {
  447. createNewPDFRadio_.properties.checkboxType = .normal
  448. createNewPDFRadio_.reloadData()
  449. mergeAllCheckBox_.properties.checkboxType = .normal
  450. mergeAllCheckBox_.properties.isDisabled = true
  451. mergeAllCheckBox_.reloadData()
  452. appendBackInput_.properties.isDisabled = false
  453. appendBackInput_.reloadData()
  454. appendBackButtonInput_.properties.isDisabled = false
  455. appendBackButtonInput_.reloadData()
  456. }
  457. }
  458. @IBAction func buttonClicked_OCRSelect(_ sender: ComponentCheckBox) {
  459. //MARK: 判断是否付费用户
  460. if IAPProductsManager.default().isAvailableAllFunction() == false {
  461. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  462. ocrCheckBox_.properties.checkboxType = .normal
  463. ocrCheckBox_.reloadData()
  464. return
  465. }
  466. if (sender.properties.checkboxType == .selected) {
  467. languageView_.properties.isDisabled = false
  468. languageView_.reloadData()
  469. ocrPlanRadio_.properties.isDisabled = false
  470. ocrPlanRadio_.reloadData()
  471. ocrPlanRadio2_.properties.isDisabled = false
  472. ocrPlanRadio2_.reloadData()
  473. extractTextCheckBox_.properties.isDisabled = false
  474. extractTextCheckBox_.reloadData()
  475. } else {
  476. languageView_.properties.isDisabled = true
  477. languageView_.reloadData()
  478. ocrPlanRadio_.properties.isDisabled = true
  479. ocrPlanRadio_.reloadData()
  480. ocrPlanRadio2_.properties.isDisabled = true
  481. ocrPlanRadio2_.reloadData()
  482. extractTextCheckBox_.properties.isDisabled = true
  483. extractTextCheckBox_.reloadData()
  484. }
  485. }
  486. @IBAction func buttonClicked_ImageToPDF(_ sender: ComponentButton) {
  487. //MARK: 判断是否付费用户,展示iap界面
  488. if self.files?.count ?? 0 < 1 {
  489. return
  490. }
  491. if IAPProductsManager.default().isAvailableAllFunction() == false && self.files?.count ?? 0 > 1{
  492. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  493. return
  494. }
  495. if sender.properties.state == .pressed {
  496. self.choosePath = ""
  497. var hasTask = false
  498. for i in 0..<(self.files?.count ?? 0) {
  499. let file = self.files?[i]
  500. file?.currentOperateInfo?.resetState()
  501. if file?.fileType == .Image {
  502. hasTask = true
  503. }
  504. }
  505. if !hasTask {
  506. NSSound.beep()
  507. return
  508. }
  509. var isOCR = false
  510. if ocrCheckBox_.properties.checkboxType == .selected {
  511. isOCR = true
  512. }
  513. var isSaveAs = false
  514. if extractTextCheckBox_.properties.checkboxType == .selected {
  515. isSaveAs = true
  516. }
  517. let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey")
  518. if isOCR && !isConnectionAvailable() && plan == 0 {
  519. let alert = NSAlert()
  520. alert.alertStyle = .critical
  521. alert.messageText = KMLocalizedString("Connection Error", comment: "")
  522. alert.informativeText = KMLocalizedString("Please make sure your internet connection is available.", comment: "")
  523. alert.addButton(withTitle: KMLocalizedString("OK", comment: ""))
  524. if let window = self.view.window {
  525. alert.beginSheetModal(for: window, completionHandler: nil)
  526. } else {
  527. alert.runModal()
  528. }
  529. return
  530. }
  531. if createNewPDFRadio_.properties.checkboxType != .selected {
  532. let appenString = appendBackInput_.properties.text
  533. if appenString.isEmpty {
  534. let alert = NSAlert()
  535. alert.alertStyle = .critical
  536. alert.messageText = KMLocalizedString("Select a File", comment: "")
  537. alert.runModal()
  538. return
  539. }
  540. }
  541. let openPanel = NSOpenPanel()
  542. openPanel.canChooseFiles = false
  543. openPanel.canChooseDirectories = true
  544. openPanel.canCreateDirectories = true
  545. openPanel.beginSheetModal(for: self.view.window!) { (result) in
  546. if result == .OK {
  547. for fileURL in openPanel.urls {
  548. self.choosePath = fileURL.path
  549. self.beginImageToPDF()
  550. }
  551. }
  552. }
  553. } else { // Do something else }
  554. }
  555. }
  556. @IBAction func buttonItemClicked_AppendOtherPDF(_ sender: NSButton) {
  557. let isDisabled = appendBackButtonInput_.properties.isDisabled
  558. if isDisabled {
  559. return
  560. }
  561. let openPanel = NSOpenPanel()
  562. openPanel.allowedFileTypes = ["pdf"]
  563. openPanel.canChooseDirectories = false
  564. openPanel.allowsMultipleSelection = false
  565. openPanel.beginSheetModal(for: self.view.window!) { (result) in
  566. if result == .OK {
  567. guard let url = openPanel.url else { return }
  568. guard let document = PDFDocument(url: url) else {
  569. let alert = NSAlert()
  570. alert.alertStyle = .critical
  571. alert.messageText = KMLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  572. alert.runModal()
  573. return
  574. }
  575. if !document.allowsCopying || !document.allowsPrinting {
  576. let alert = NSAlert()
  577. alert.alertStyle = .critical
  578. alert.messageText = KMLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
  579. alert.runModal()
  580. return
  581. }
  582. if document.isLocked {
  583. KMBaseWindowController.checkPassword(url: url, type: .owner) { [weak self] success, resultPassword in
  584. if success {
  585. self?.password = resultPassword
  586. self?.appendBackInput_.properties.text = url.path
  587. self?.appendBackInput_.reloadData()
  588. }
  589. }
  590. } else {
  591. self.appendBackInput_.properties.text = url.path
  592. self.appendBackInput_.reloadData()
  593. }
  594. }
  595. }
  596. }
  597. // "Choose automatic language detection for better OCR results."
  598. //MARK: -Notification
  599. @objc func batchFilesCountNotification(notification: Notification) {
  600. let files: Array? = notification.object as? [KMBatchOperateFile]
  601. self.files? = files ?? []
  602. let cnt = self.files?.count ?? 0
  603. if cnt > 0 {
  604. self._updateActionButtonEnable(true)
  605. } else {
  606. self._updateActionButtonEnable(false)
  607. }
  608. }
  609. func OCRSelectedPlanChangeAction() {
  610. KMGOCRManager.default().selectedLanguages = NSMutableArray()
  611. self.updateLanguageButton(KMGOCRManager.default().selectedLanguages?.value(forKeyPath: KMGOCRLanguageStringKey) as? [String])
  612. }
  613. }
  614. // MARK: - KMImageToPDFMethodDelegate
  615. extension KMBatchOperateImageToPDFViewController: KMImageToPDFMethodDelegate {
  616. func imageToPDFMethod(_ method: KMImageToPDFMethod, progress: Float) {
  617. }
  618. }
  619. // MARK: - ComponentSelectDelegate
  620. extension KMBatchOperateImageToPDFViewController: ComponentSelectDelegate {
  621. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  622. let idx = view?.indexOfSelect() ?? 0
  623. let languages = KMGOCRManager.languages() as NSArray
  624. let lanuage = languages[idx]
  625. KMGOCRManager.default().selectedLanguages = [lanuage]
  626. }
  627. }