KMBatchOperateConvertViewController.swift 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. //
  2. // KMBatchOperateConvertViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/11/1.
  6. //
  7. import Cocoa
  8. typealias detailInfoViewMouseDownCallback = (_ mouseDown: Bool) -> ()
  9. class KMDetailInfoView: NSView{
  10. var mouseDownCallback: detailInfoViewMouseDownCallback?
  11. override func mouseDown(with event: NSEvent) {
  12. super.mouseDown(with: event)
  13. guard let callBack = mouseDownCallback else { return }
  14. callBack(true)
  15. }
  16. }
  17. class KMBatchOperateConvertViewController: KMBatchOperateBaseViewController{
  18. var dataSourcesArray: [KMConvertWithPDFType]?
  19. @IBOutlet var bottomView: NSView!
  20. @IBOutlet var convertButton: NSButton!
  21. @IBOutlet var buttonBottemOffset: NSLayoutConstraint!
  22. @IBOutlet var detailInfoLabel: NSTextField!
  23. @IBOutlet var detailInfoBox: NSBox!
  24. @IBOutlet var separateStypeTopOffset: NSLayoutConstraint!
  25. @IBOutlet var separateStypeBox: NSBox!
  26. @IBOutlet var wordButtonOne: NSButton!
  27. @IBOutlet var wordButtonTwo: NSButton!
  28. @IBOutlet var excelStyleBox: NSBox!
  29. @IBOutlet var allContentMenu: NSPopUpButton!
  30. @IBOutlet var allContentMenu2: NSMenuItem!
  31. @IBOutlet var allContentMenu3: NSMenuItem!
  32. @IBOutlet var onlyTextBtn: NSButton!
  33. @IBOutlet var onlyTableBtn: NSButton!
  34. @IBOutlet var allContentBtn: NSButton!
  35. @IBOutlet var tableMenu1: NSMenuItem!
  36. @IBOutlet var tableMenu2: NSMenuItem!
  37. @IBOutlet var tableMenu3: NSMenuItem!
  38. @IBOutlet var tableMenu: NSPopUpButton!
  39. @IBOutlet var csvExtractButton: NSButton!
  40. @IBOutlet var ocrBox: NSBox!
  41. @IBOutlet var ocrLabel: NSTextField!
  42. @IBOutlet var ocrButton: NSButton!
  43. @IBOutlet var ocrLanguageBox: NSPopUpButton!
  44. @IBOutlet var ocrTopOffset: NSLayoutConstraint!
  45. @IBOutlet var headerView: KMConvertCollectionViewHeader!
  46. @IBOutlet var headerTopOffset: NSLayoutConstraint!
  47. @IBOutlet var formatLabel: NSTextField!
  48. @IBOutlet var formatSelectButton: NSPopUpButton!
  49. var excelContentOption: CPDFConvertExcelContentOptions?
  50. var excelWorksheetOption: CPDFConvertExcelWorksheetOptions?
  51. var haveFiles: Bool = false
  52. private var ocrLanguage_: COCRLanguage = .english
  53. deinit {
  54. NotificationCenter.default.removeObserver(self)
  55. }
  56. override var interfaceStatus: KMBatchOperateInterfaceStatus?{
  57. set{
  58. super.interfaceStatus = newValue
  59. self.convertButton.isEnabled = true
  60. if newValue == .Processing {
  61. formatSelectButton.isEnabled = false
  62. self.convertButton.title = NSLocalizedString("Cancel", comment: "")
  63. self.convertButton.tag = 2
  64. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color())
  65. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  66. if self.convertType == .WordStandard {
  67. self.convertButton.title = NSLocalizedString("Convert", comment: "")
  68. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  69. self.convertButton.isEnabled = false
  70. }
  71. } else {
  72. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  73. var files: [URL] = []
  74. for url in self.successFilePathURLArray! {
  75. if FileManager.default.fileExists(atPath: url.path) {
  76. files.append(url)
  77. }
  78. }
  79. if files.count > 0 {
  80. let workspace = NSWorkspace.shared
  81. workspace.activateFileViewerSelecting(files)
  82. }
  83. }
  84. formatSelectButton.isEnabled = true
  85. self.convertButton.title = NSLocalizedString("Convert", comment: "")
  86. self.convertButton.tag = 1
  87. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color())
  88. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  89. }
  90. }
  91. get{
  92. return super.interfaceStatus
  93. }
  94. }
  95. override var convertType: KMConvertWithPDFType?{
  96. set{
  97. super.convertType = newValue
  98. changeAllFilesToConvertType(convertType!)
  99. fetchFileListViewController()?.reloadConvertInterface(withType: convertType!)
  100. }
  101. get{
  102. return super.convertType
  103. }
  104. }
  105. func changeAllFilesToConvertType(_ type: KMConvertWithPDFType) {
  106. for i in 0..<self.files!.count {
  107. let file = self.files![i]
  108. file.convertType = type
  109. }
  110. }
  111. func switchToConvertType(_ convertType: KMConvertWithPDFType) {
  112. self.convertType = convertType
  113. self.manualSelectRow(convertType)
  114. self.updateBottomView()
  115. }
  116. func manualSelectRow(_ convertType: KMConvertWithPDFType) {
  117. var index = -1
  118. updateFormatView()
  119. for i in 0..<self.dataSourcesArray!.count {
  120. let number = self.dataSourcesArray![i]
  121. if number.rawValue == convertType.rawValue {
  122. index = i
  123. break
  124. }
  125. }
  126. if index != -1 {
  127. self.formatSelectButton.selectItem(at: index)
  128. }
  129. }
  130. func updateBottomView() {
  131. var isShowUpgrade = false
  132. #if VERSION_FREE
  133. #if VERSION_DMG
  134. // 桌机版
  135. if KMMemberInfo.shared.isMemberAllFunction {
  136. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  137. } else {
  138. isShowUpgrade = true
  139. }
  140. } else {
  141. isShowUpgrade = true
  142. }
  143. #else
  144. // 免费版
  145. if KMMemberInfo.shared.isMemberAllFunction {
  146. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  147. } else {
  148. isShowUpgrade = true
  149. }
  150. } else {
  151. isShowUpgrade = true
  152. }
  153. #endif
  154. #else
  155. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  156. } else {
  157. isShowUpgrade = true
  158. }
  159. #endif
  160. // if !isShowUpgrade {
  161. // headerView.isHidden = true
  162. // headerTopOffset.constant = 0
  163. // } else {
  164. // headerView.isHidden = false
  165. // headerTopOffset.constant = self.headerView.frame.height
  166. // }
  167. var moreLabelString = ""
  168. #if VERSION_FREE
  169. if !KMMemberInfo.shared.isMemberAllFunction {
  170. moreLabelString = String(format: "%@ %@", KMLocalizedString("The first 10 pages for free"), KMLocalizedString("Unlimited Convert"))
  171. } else {
  172. if !IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  173. if .WordAdvance == self.convertType ||
  174. .WordStandard == self.convertType ||
  175. .Excel == self.convertType ||
  176. .PowerPoint == self.convertType ||
  177. .CSV == self.convertType ||
  178. .RTF == self.convertType ||
  179. .GIF == self.convertType ||
  180. .TIFF == self.convertType ||
  181. .JPEG2000 == self.convertType ||
  182. .BMP == self.convertType ||
  183. .TGA == self.convertType ||
  184. .Json == self.convertType {
  185. moreLabelString = String(format: "%@ %@", KMLocalizedString("The first 10 pages for free"), KMLocalizedString("Unlimited Convert"))
  186. }
  187. }
  188. }
  189. #else
  190. // 付费版
  191. if !IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  192. if .WordAdvance == self.convertType ||
  193. .WordStandard == self.convertType ||
  194. .Excel == self.convertType ||
  195. .PowerPoint == self.convertType ||
  196. .CSV == self.convertType ||
  197. .RTF == self.convertType ||
  198. .GIF == self.convertType ||
  199. .TIFF == self.convertType ||
  200. .JPEG2000 == self.convertType ||
  201. .BMP == self.convertType ||
  202. .TGA == self.convertType ||
  203. .Json == self.convertType {
  204. moreLabelString = String(format: "%@ %@", KMLocalizedString("The first 10 pages for free"), KMLocalizedString("Unlimited Convert"))
  205. }
  206. }
  207. #endif
  208. self.detailInfoLabel.stringValue = moreLabelString
  209. if let range = moreLabelString.range(of: NSLocalizedString("Unlimited Convert", comment: "")) {
  210. let newR: NSRange = moreLabelString.nsRange(from: range)!
  211. let attributedStr = NSMutableAttributedString(string: moreLabelString)
  212. attributedStr.addAttribute(.foregroundColor, value: NSColor.labelColor, range: NSRange(location: 0, length: newR.location - 1))
  213. attributedStr.addAttribute(.foregroundColor, value: NSColor(red: 8/255, green: 124/255, blue: 1, alpha: 1), range: newR)
  214. attributedStr.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: newR)
  215. detailInfoLabel.attributedStringValue = attributedStr
  216. }
  217. if moreLabelString.count > 0 {
  218. detailInfoBox.isHidden = false
  219. buttonBottemOffset.constant = 45
  220. } else {
  221. detailInfoBox.isHidden = true
  222. buttonBottemOffset.constant = 10
  223. }
  224. if self.convertType == .WordAdvance {
  225. separateStypeBox.isHidden = false
  226. excelStyleBox.isHidden = true
  227. csvExtractButton.isHidden = true
  228. ocrBox.isHidden = false
  229. wordButtonOne.title = NSLocalizedString("Retain Flowing Text", comment: "")
  230. wordButtonTwo.title = NSLocalizedString("Retain Page Layout", comment: "")
  231. ocrTopOffset.constant = 15 + separateStypeBox.frame.height
  232. } else if self.convertType == .Excel {
  233. separateStypeBox.isHidden = true
  234. excelStyleBox.isHidden = false
  235. csvExtractButton.isHidden = true
  236. ocrBox.isHidden = false
  237. ocrTopOffset.constant = 15 + excelStyleBox.frame.height
  238. } else if .Json == convertType {
  239. wordButtonOne.title = NSLocalizedString("Extract text content", comment: "")
  240. wordButtonTwo.title = NSLocalizedString("Extract tables", comment: "")
  241. separateStypeBox.isHidden = false
  242. excelStyleBox.isHidden = true
  243. csvExtractButton.isHidden = true
  244. ocrBox.isHidden = false
  245. ocrTopOffset.constant = 15 + separateStypeBox.frame.height
  246. } else if (.PowerPoint == convertType ||
  247. .RTF == convertType ||
  248. .HTML == convertType ||
  249. .Text == convertType) {
  250. separateStypeBox.isHidden = true
  251. excelStyleBox.isHidden = true
  252. csvExtractButton.isHidden = true
  253. ocrBox.isHidden = false
  254. ocrTopOffset.constant = 15
  255. } else if self.convertType == .CSV {
  256. separateStypeBox.isHidden = true
  257. excelStyleBox.isHidden = true
  258. csvExtractButton.isHidden = false
  259. ocrBox.isHidden = true
  260. self.csvExtractButton.title = NSLocalizedString("Extract Tables Only", comment: "")
  261. } else {
  262. separateStypeBox.isHidden = true
  263. excelStyleBox.isHidden = true
  264. csvExtractButton.isHidden = true
  265. ocrBox.isHidden = true
  266. }
  267. }
  268. override func viewDidLoad() {
  269. super.viewDidLoad()
  270. self.prepareData()
  271. updateFormatView()
  272. self.view.wantsLayer = true
  273. convertButton.wantsLayer = true
  274. convertButton.font = NSFont.systemFont(ofSize: 13)
  275. convertButton.layer?.cornerRadius = 1.0
  276. interfaceStatus = .PrepareProcess
  277. allContentBtn.state = .on
  278. allContentMenu.isEnabled = true
  279. tableMenu.isEnabled = false
  280. excelContentOption = .allContent
  281. excelWorksheetOption = .forEachPage
  282. onlyTextBtn.title = NSLocalizedString("Only Text", comment: "")
  283. onlyTableBtn.title = NSLocalizedString("Only Table", comment: "")
  284. allContentBtn.title = NSLocalizedString("All Content", comment: "")
  285. onlyTextBtn.toolTip = NSLocalizedString("Only Text", comment: "")
  286. onlyTableBtn.toolTip = NSLocalizedString("Only Table", comment: "")
  287. allContentBtn.toolTip = NSLocalizedString("All Content", comment: "")
  288. tableMenu1.title = NSLocalizedString("Create Sheet for each Table", comment: "")
  289. tableMenu2.title = NSLocalizedString("Create Sheet for each Page", comment: "")
  290. allContentMenu2.title = NSLocalizedString("Create Sheet for each Page", comment: "")
  291. tableMenu3.title = NSLocalizedString("Create single Sheet for File", comment: "")
  292. allContentMenu3.title = NSLocalizedString("Create single Sheet for File", comment: "")
  293. formatLabel.stringValue = KMLocalizedString("Format:", nil)
  294. ocrBox.title = KMLocalizedString("OCR Settings", nil)
  295. ocrBox.titleFont = .SFProTextBoldFont(14.0)
  296. formatLabel.font = .SFProTextBoldFont(14.0)
  297. headerView.convertHeaderClickedCallBack = { [weak self] in
  298. self?.moreConvertInfoAction()
  299. }
  300. self.ocrButton.title = NSLocalizedString("Recognize text", comment: "")
  301. self.ocrLabel.stringValue = NSLocalizedString("Language Option", comment: "") + ":"
  302. ocrLanguageBox.selectItem(at: 2)
  303. ocrLabel.textColor = KMAppearance.Layout.h2Color()
  304. ocrLanguageBox.isEnabled = false
  305. ocrButton.state = .off
  306. self.updateViewColor()
  307. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: Notification.Name(rawValue: "KMBatchFilesCountNotification"), object: nil)
  308. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductPurchasedNotification(notification:)), name: NSNotification.Name("KMIAPProductPurchasedNotification"), object: nil)
  309. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductRestoreFinishedNotification(notification:)), name: NSNotification.Name("KMIAPProductRestoreFinishedNotification"), object: nil)
  310. // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  311. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  312. NotificationCenter.default.addObserver(self, selector: #selector(deviceActivateStatusChanged(notification:)), name: NSNotification.Name("kDeviceActivateNotification"), object: nil)
  313. }
  314. @objc func batchFilesCountNotification(notification: NSNotification) {
  315. let arr: Array? = notification.object as? [KMBatchOperateFile]
  316. self.files? = arr ?? []
  317. if files!.count > 0 {
  318. self.convertButton.setTitleColor(NSColor.white)
  319. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  320. self.haveFiles = true
  321. } else {
  322. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  323. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  324. self.haveFiles = false
  325. }
  326. }
  327. @objc func themeChanged(notification: NSNotification) {
  328. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  329. self.updateViewColor()
  330. }
  331. }
  332. @objc func IAPProductRestoreFinishedNotification(notification: NSNotification) {
  333. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  334. self.updateBottomView()
  335. self.prepareData()
  336. self.updateFormatView()
  337. }
  338. }
  339. @objc func IAPProductPurchasedNotification(notification: NSNotification) {
  340. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  341. self.updateBottomView()
  342. self.prepareData()
  343. self.updateFormatView()
  344. }
  345. }
  346. @objc func deviceActivateStatusChanged(notification: NSNotification) {
  347. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  348. self.updateBottomView()
  349. self.prepareData()
  350. self.updateFormatView()
  351. }
  352. }
  353. func updateViewColor() {
  354. self.view.wantsLayer = true
  355. if KMAppearance.isDarkMode() {
  356. self.view.layer?.backgroundColor = NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor
  357. } else {
  358. self.view.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  359. }
  360. }
  361. func convertActionVC() {
  362. var vc: KMToolCompareWindowController? = nil
  363. if(.WordStandard == self.convertType ||
  364. .WordAdvance == self.convertType) {
  365. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  366. } else if (.Excel == self.convertType) {
  367. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 2)
  368. } else if (.PowerPoint == self.convertType) {
  369. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 3)
  370. } else if (self.convertType == .JPG || self.convertType == .PNG || self.convertType == .GIF || self.convertType == .TIFF || self.convertType == .TGA || self.convertType == .BMP){
  371. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 4)
  372. } else {
  373. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 0)
  374. }
  375. vc?.showWindow(nil)
  376. }
  377. func prepareData() {
  378. let arr: [KMConvertWithPDFType] = [.Excel, .PowerPoint, .RTF, .CSV, .HTML, .Text, .JPEG, .JPG, .PNG, .GIF, .TIFF, .TGA, .BMP, .JPEG2000, .Json]
  379. self.dataSourcesArray = arr
  380. var needShowAdvance = true
  381. var needShowDefault = true
  382. #if VERSION_FREE
  383. // 桌机版
  384. if KMMemberInfo.shared.isMemberAllFunction {
  385. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  386. needShowAdvance = true
  387. needShowDefault = false
  388. }
  389. }
  390. #else
  391. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  392. needShowAdvance = true
  393. needShowDefault = false
  394. }
  395. #endif
  396. if needShowDefault {
  397. var wordType: KMConvertWithPDFType = .WordStandard
  398. self.dataSourcesArray?.insert(wordType, at: 0)
  399. }
  400. if needShowAdvance {
  401. var wordType: KMConvertWithPDFType = .WordAdvance
  402. self.dataSourcesArray?.insert(wordType, at: 0)
  403. }
  404. }
  405. func updateFormatView() {
  406. var isShowAdvanced = false
  407. #if VERSION_FREE
  408. #if VERSION_DMG
  409. // 桌机版
  410. isShowAdvanced = true
  411. if KMMemberInfo.shared.isMemberAllFunction {
  412. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  413. isShowAdvanced = false
  414. }
  415. }
  416. #else
  417. // 免费版
  418. isShowAdvanced = true
  419. if KMMemberInfo.shared.isMemberAllFunction {
  420. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  421. isShowAdvanced = false
  422. }
  423. }
  424. #endif
  425. #else
  426. isShowAdvanced = true
  427. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  428. isShowAdvanced = false
  429. }
  430. #endif
  431. self.formatSelectButton.removeAllItems()
  432. for i in 0..<self.dataSourcesArray!.count {
  433. let number = self.dataSourcesArray![i]
  434. let item = NSMenuItem()
  435. item.target = self
  436. if(number == .WordAdvance) {
  437. if isShowAdvanced {
  438. let originStr = " \(NSLocalizedString("Advanced", comment: "")) "
  439. let str = NSLocalizedString("Word (.docx)", comment: "")
  440. let attributedStr = NSMutableAttributedString(string: "\(str) \(originStr)")
  441. attributedStr.addAttribute(NSAttributedString.Key.backgroundColor, value: NSColor(red: 245.0/255.0, green: 142.0/255.0, blue: 38.0/255.0, alpha: 1.0), range: NSMakeRange(str.count + 2, originStr.count))
  442. attributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: NSColor.white, range: NSMakeRange(str.count + 2, originStr.count))
  443. item.attributedTitle = attributedStr
  444. } else {
  445. item.title = "Word (.docx)"
  446. }
  447. item.tag = KMConvertWithPDFType.WordAdvance.rawValue
  448. } else if(number == .WordStandard) {
  449. if isShowAdvanced {
  450. let originStr = " \(KMLocalizedString("Standard", nil)) "
  451. let str = KMLocalizedString("Word (.docx)", nil)
  452. let attributedStr = NSMutableAttributedString(string: "\(str) \(originStr)")
  453. attributedStr.addAttribute(NSAttributedString.Key.backgroundColor, value: NSColor(red: 40/255.0, green: 193.0/255.0, blue: 107.0/255.0, alpha: 1.0), range: NSMakeRange(str.count + 2, originStr.count))
  454. attributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: NSColor.white, range: NSMakeRange(str.count + 2, originStr.count))
  455. item.attributedTitle = attributedStr
  456. } else {
  457. item.title = "Word (.docx)"
  458. }
  459. item.tag = KMConvertWithPDFType.WordStandard.rawValue
  460. } else if(number == .Excel) {
  461. item.title = "Excel (.xlsx)"
  462. item.tag = KMConvertWithPDFType.Excel.rawValue
  463. } else if(number == .PowerPoint) {
  464. item.title = "PowerPoint (.pptx)"
  465. item.tag = KMConvertWithPDFType.PowerPoint.rawValue
  466. } else if(number == .RTF) {
  467. item.title = "RTF (.rtf)"
  468. item.tag = KMConvertWithPDFType.RTF.rawValue
  469. } else if(number == .CSV) {
  470. item.title = "CSV (.csv)"
  471. item.tag = KMConvertWithPDFType.CSV.rawValue
  472. } else if(number == .HTML) {
  473. item.title = "Html (.html)"
  474. item.tag = KMConvertWithPDFType.HTML.rawValue
  475. } else if(number == .Text) {
  476. item.title = "Text (.txt)"
  477. item.tag = KMConvertWithPDFType.Text.rawValue
  478. } else if(number == .JPEG) {
  479. item.title = "JPEG (.jpeg)"
  480. item.tag = KMConvertWithPDFType.JPEG.rawValue
  481. } else if(number == .JPG) {
  482. item.title = "JPG (.jpg)"
  483. item.tag = KMConvertWithPDFType.JPG.rawValue
  484. } else if(number == .PNG) {
  485. item.title = "PNG (.png)"
  486. item.tag = KMConvertWithPDFType.PNG.rawValue
  487. } else if(number == .GIF) {
  488. item.title = "GIF (.gif)"
  489. item.tag = KMConvertWithPDFType.GIF.rawValue
  490. } else if(number == .TIFF) {
  491. item.title = "TIFF (.tiff)"
  492. item.tag = KMConvertWithPDFType.TIFF.rawValue
  493. } else if(number == .TGA) {
  494. item.title = "TGA (.tga)"
  495. item.tag = KMConvertWithPDFType.TGA.rawValue
  496. } else if(number == .BMP) {
  497. item.title = "BMP (.bmp)"
  498. item.tag = KMConvertWithPDFType.BMP.rawValue
  499. } else if(number == .JPEG2000) {
  500. item.title = "JPEG-2000 (.jp2)"
  501. item.tag = KMConvertWithPDFType.JPEG2000.rawValue
  502. } else if(number == .Json) {
  503. item.title = "Json(.json)"
  504. item.tag = KMConvertWithPDFType.Json.rawValue
  505. }
  506. self.formatSelectButton.menu?.addItem(item)
  507. }
  508. }
  509. @IBAction func buttonClicked_Convert(_ sender: NSButton) {
  510. if !self.haveFiles { return }
  511. self.view.window?.makeFirstResponder(nil)
  512. for i in 0..<self.files!.count {
  513. let file = self.files?[i]
  514. // file?.excelParameter.allInOneSheet = true // 看代码是没有用
  515. file?.advanceWordParameter.isRetainLayout = (self.wordButtonTwo.state == .on) ? true : false
  516. // file?.excelParameter.isExtreactTabel = (self.extractButton.state == .on) ? true : false // 看代码是没有用
  517. file?.CSVParameter.isExtreactTabel = (self.csvExtractButton.state == .on) ? true : false
  518. file?.excelParameter.excelContentOption = self.excelContentOption
  519. file?.excelParameter.excelWorksheetOption = self.excelWorksheetOption
  520. file?.JSONParameter.isExtreactTabel = self.wordButtonTwo.state == .on
  521. var isAllowOCR = self.ocrBox.isHidden == false
  522. if isAllowOCR {
  523. isAllowOCR = self.ocrButton.state == .on
  524. }
  525. file?.isAllowOCR = isAllowOCR
  526. file?.ocrLanguage = isAllowOCR ? self.ocrLanguage_ : .chinese
  527. }
  528. if sender.tag == 1 {
  529. self.beginBatchOperation()
  530. } else {
  531. self.cancelBatchOperation()
  532. }
  533. }
  534. @IBAction func moreButtonAction(_ sender: NSButton) {
  535. self.moreConvertInfoAction()
  536. }
  537. @IBAction func layoutButtonAction(_ sender: Any) {
  538. }
  539. @IBAction func buttonClicked_Cancel(_ sender: Any) {
  540. let basePath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? ""
  541. let newPath = (basePath as NSString).appendingPathComponent(Bundle.main.bundleIdentifier ?? "")
  542. let filePath = newPath.stringByAppendingPathComponent("convert.pdf")
  543. if FileManager.default.fileExists(atPath: filePath) {
  544. try? FileManager.default.removeItem(atPath: filePath)
  545. }
  546. }
  547. func transform(withString string: String) {
  548. if string == NSLocalizedString("Create Sheet for each Table", comment: "") {
  549. self.excelWorksheetOption = .forEachTable
  550. } else if string == NSLocalizedString("Create Sheet for each Page", comment: "") {
  551. self.excelWorksheetOption = .forEachPage
  552. } else if string == NSLocalizedString("Create single Sheet for File", comment: "") {
  553. self.excelWorksheetOption = .forTheDocument
  554. }
  555. }
  556. @IBAction func buttonClicked_excelStyle(_ sender: NSButton) {
  557. if sender == onlyTextBtn {
  558. onlyTextBtn.state = NSControl.StateValue.on
  559. onlyTableBtn.state = NSControl.StateValue.off
  560. allContentBtn.state = NSControl.StateValue.off
  561. tableMenu.isEnabled = false
  562. allContentMenu.isEnabled = false
  563. excelContentOption = .onlyText
  564. excelWorksheetOption = .forEachTable
  565. } else if sender == onlyTableBtn {
  566. onlyTableBtn.state = NSControl.StateValue.on
  567. onlyTextBtn.state = NSControl.StateValue.off
  568. allContentBtn.state = NSControl.StateValue.off
  569. tableMenu.isEnabled = true
  570. allContentMenu.isEnabled = false
  571. excelContentOption = .onlyTable
  572. transform(withString: tableMenu.selectedItem?.title ?? "")
  573. } else if sender == allContentBtn {
  574. allContentBtn.state = NSControl.StateValue.on
  575. onlyTextBtn.state = NSControl.StateValue.off
  576. onlyTableBtn.state = NSControl.StateValue.off
  577. tableMenu.isEnabled = false
  578. allContentMenu.isEnabled = true
  579. excelContentOption = .allContent
  580. transform(withString: allContentMenu.selectedItem?.title ?? "")
  581. }
  582. }
  583. @IBAction func buttonClicked_tableMenu(_ sender: NSPopUpButton) {
  584. transform(withString: self.tableMenu.selectedItem!.title)
  585. }
  586. @IBAction func buttonClickec_allContentMenu(_ sender: NSButton) {
  587. transform(withString: self.allContentMenu.selectedItem!.title)
  588. }
  589. func intConvertType(num: Int) -> KMConvertWithPDFType {
  590. var type: KMConvertWithPDFType?
  591. switch num {
  592. case 0:
  593. type = .WordAdvance
  594. case 1:
  595. type = .WordStandard
  596. case 2:
  597. type = .Excel
  598. case 3:
  599. type = .PowerPoint
  600. case 4:
  601. type = .RTF
  602. case 5:
  603. type = .CSV
  604. case 6:
  605. type = .HTML
  606. case 7:
  607. type = .Text
  608. case 8:
  609. type = .JPEG
  610. case 9:
  611. type = .JPG
  612. case 10:
  613. type = .PNG
  614. case 11:
  615. type = .GIF
  616. case 12:
  617. type = .TIFF
  618. case 13:
  619. type = .TGA
  620. case 14:
  621. type = .BMP
  622. case 15:
  623. type = .JPEG2000
  624. case 16:
  625. type = .Json
  626. default:
  627. type = .WordAdvance
  628. }
  629. return type!
  630. }
  631. func moreConvertInfoAction() {
  632. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() == false {
  633. #if VERSION_DMG
  634. if KMMemberInfo.shared.isMemberAllFunction && IAPProductsManager.default().isAvailableAdvancedPDFToOffice() == false {
  635. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  636. limitWC.continueBlock = { windowController in
  637. }
  638. limitWC.window?.center()
  639. limitWC.showWindow(nil)
  640. }else{
  641. KMProductCompareWC.shared.orientation = false
  642. let winC = KMProductCompareWC.shared
  643. if let viewC = self.view.window?.contentViewController, viewC.kEventTag == 1 {
  644. winC.kEventName = "Onbrd_ConvertPDF_BuyNow"
  645. } else {
  646. if convertType == .WordStandard || convertType == .WordAdvance {
  647. winC.kEventName = "Onbrd_PDFtoWord_BuyNow"
  648. } else if convertType == .Excel {
  649. winC.kEventName = "Onbrd_PDFtoExcel_BuyNow"
  650. } else if convertType == .PowerPoint {
  651. winC.kEventName = "Onbrd_PDFtoPPT_BuyNow"
  652. }
  653. }
  654. winC.showWindow(nil)
  655. }
  656. #else
  657. if KMMemberInfo.shared.isMemberAllFunction == true {
  658. self.convertActionVC()
  659. } else {
  660. KMMemberInfo.shared.advancedFunctionUsage()
  661. }
  662. #endif
  663. }
  664. }
  665. @IBAction func buttonClicked_FormatSelect(_ sender: NSButton) {
  666. guard let item = self.formatSelectButton.selectedItem else { return }
  667. convertType = KMConvertWithPDFType(rawValue: item.tag) ?? .WordAdvance
  668. updateBottomView()
  669. }
  670. @IBAction func comboxClicked_OcrLanguage(_ sender: AnyObject?) {
  671. switch (ocrLanguageBox.indexOfSelectedItem) {
  672. case 0: //chinese
  673. self.ocrLanguage_ = .chinese
  674. break
  675. case 1: //chinese zht
  676. self.ocrLanguage_ = .chineseTraditional
  677. break
  678. case 2: //English
  679. self.ocrLanguage_ = .english
  680. break
  681. case 3:
  682. //Japanese
  683. self.ocrLanguage_ = .japanese
  684. break
  685. case 4:
  686. //Kroean
  687. self.ocrLanguage_ = .korean
  688. break
  689. case 5:
  690. //Serbian
  691. self.ocrLanguage_ = .serbian
  692. break
  693. case 6:
  694. //Occitan
  695. self.ocrLanguage_ = .occitan
  696. break
  697. case 7:
  698. //Danish
  699. self.ocrLanguage_ = .danish
  700. break
  701. case 8:
  702. //German
  703. self.ocrLanguage_ = .german
  704. break
  705. case 9:
  706. //French
  707. self.ocrLanguage_ = .french
  708. break
  709. case 10:
  710. //Italian
  711. self.ocrLanguage_ = .italian
  712. break
  713. case 11:
  714. //Spanish
  715. self.ocrLanguage_ = .spanish
  716. break
  717. case 12:
  718. //Portuguese
  719. self.ocrLanguage_ = .portuguese
  720. break
  721. case 13:
  722. //Maori
  723. self.ocrLanguage_ = .maori
  724. break
  725. case 14:
  726. //Malay
  727. self.ocrLanguage_ = .malay
  728. break
  729. case 15:
  730. //Malay
  731. self.ocrLanguage_ = .maltese
  732. break
  733. case 16:
  734. //Dutch
  735. self.ocrLanguage_ = .dutch
  736. break
  737. case 17:
  738. //Norwegian
  739. self.ocrLanguage_ = .norwegian
  740. break
  741. case 18:
  742. //Polish
  743. self.ocrLanguage_ = .polish
  744. break
  745. case 19:
  746. //Romanian
  747. self.ocrLanguage_ = .romanian
  748. break
  749. case 20:
  750. //Slovak
  751. self.ocrLanguage_ = .slovak
  752. break
  753. case 21:
  754. //Slovenian
  755. self.ocrLanguage_ = .slovenian
  756. break
  757. case 22:
  758. //Albanian
  759. self.ocrLanguage_ = .albanian
  760. break
  761. case 23:
  762. //Swedish
  763. self.ocrLanguage_ = .swedish
  764. break
  765. case 24:
  766. //Swahili
  767. self.ocrLanguage_ = .swahili
  768. break
  769. case 25:
  770. //Tagalog
  771. self.ocrLanguage_ = .tagalog
  772. break
  773. case 26:
  774. //Turish
  775. self.ocrLanguage_ = .turish
  776. break
  777. case 27:
  778. //Uzbek
  779. self.ocrLanguage_ = .uzbek
  780. break
  781. case 28:
  782. //Vietnamese
  783. self.ocrLanguage_ = .vietnamese
  784. break
  785. case 29:
  786. //Afrikaans
  787. self.ocrLanguage_ = .afrikaans
  788. break
  789. case 30:
  790. //Azerbaijani
  791. self.ocrLanguage_ = .azerbaijani
  792. break
  793. case 31:
  794. //Bosnian
  795. self.ocrLanguage_ = .bosnian
  796. break
  797. case 32:
  798. //Czech
  799. self.ocrLanguage_ = .czech
  800. break
  801. case 33: //Welsh
  802. self.ocrLanguage_ = .welsh
  803. break
  804. case 34: //Estonian
  805. self.ocrLanguage_ = .estonian
  806. break
  807. case 35: //Irish
  808. self.ocrLanguage_ = .irish
  809. break
  810. case 36: //Croatian
  811. self.ocrLanguage_ = .croatian
  812. break
  813. case 37: //Hungarian
  814. self.ocrLanguage_ = .hungarian
  815. break
  816. case 38: //Indonesian
  817. self.ocrLanguage_ = .indonesian
  818. break
  819. case 39: //Icelandic
  820. self.ocrLanguage_ = .icelandic
  821. break
  822. case 40: //Kurdish
  823. self.ocrLanguage_ = .kurdish
  824. break
  825. case 41: //Lithuanian
  826. self.ocrLanguage_ = .lithuanian
  827. break
  828. case 42: //Latvian
  829. self.ocrLanguage_ = .latvian
  830. break
  831. case 43: //Marathi
  832. self.ocrLanguage_ = .marathi
  833. break
  834. case 44: //Nepali
  835. self.ocrLanguage_ = .nepali
  836. break
  837. case 45: //Latvia
  838. self.ocrLanguage_ = .latvian
  839. break
  840. case 46: //Bihari
  841. self.ocrLanguage_ = .bihari
  842. break
  843. case 47: //Maithili
  844. self.ocrLanguage_ = .maithili
  845. break
  846. case 48: //Angika
  847. self.ocrLanguage_ = .angika
  848. break
  849. case 49: //Bhojpuri
  850. self.ocrLanguage_ = .bhojpuri
  851. break
  852. case 50: //Magahi
  853. self.ocrLanguage_ = .magahi
  854. break
  855. case 51: //Nagpur
  856. self.ocrLanguage_ = .nagpur
  857. break
  858. case 52: //Newari
  859. self.ocrLanguage_ = .newari
  860. break
  861. case 53: //GoanKonkani
  862. self.ocrLanguage_ = .goanKonkani
  863. break
  864. case 54: //SaudiArabia
  865. self.ocrLanguage_ = .saudiArabia
  866. break
  867. default:
  868. self.ocrLanguage_ = .english
  869. break
  870. }
  871. }
  872. @IBAction func comboxClicked_SupoortOCR(_ sender: AnyObject?) {
  873. if(ocrButton.state == .on) {
  874. ocrLabel.textColor = KMAppearance.titleColor()
  875. ocrLanguageBox.isEnabled = true
  876. } else {
  877. ocrLabel.textColor = KMAppearance.Layout.h2Color()
  878. ocrLanguageBox.isEnabled = false
  879. }
  880. }
  881. }